C# Interview Questions and Answers (958) - Page 34

Which of the following statement is correct with respect to Common Language Specification?

NOTE: This is objective type question, Please click question title for correct answer.
How to count numbers of characters in a string?

With the help of string'Length property ,we can count numbers of string characters.

For Example:-
string name = "Rajesh";

int char_count = name.Length;
MessageBox.Show(char_count.ToString());


Output would be:6
How to get values from Enum?

Suppose i have below Enum,

public enum DB_Mode

{
New,
Edit,
Update
};

string mode_value = Enum.GetName(typeof(DB_Mode),
DB_Mode.New);
MessageBox.Show(mode_value);


Output would be:- New
How does C# support properties of array types?

Yes. Here's a simple example:
using System;
class Class1
{
private string[] MyField;
public string[] MyProperty
{
get { return MyField; }
set { MyField = value; }
}
}
class MainClass
{
public static int Main(string[] args)
{
Class1 c = new Class1();
string[] arr = new string[] {"apple", "banana"};
c.MyProperty = arr;
Console.WriteLine(c.MyProperty[0]); // "apple"
return 0;
}
}
Is there a possibility to create multilanguage, multifile assembly? If so How?

Unfortunately, this is currently not supported in the IDE. To do this from the command line, you must compile your projects into netmodules (/target:module on the C# compiler), and then use the command line tool al.exe (alink) to link these netmodules together.
How to remove white spaces or empty spaces from within String?

We use Following method to remove white spaces or empty spaces from within String.

1). Trim():- It removes both leading and trailing spaces from string.

2). TrimStart():- It removes only leading space from string.

3). TrimEnd():- It removes only trailing space from string.

Actually these are static method provided by Dot Net.
How to remove leading space from below string? string name = " Rajesh";

We will use TrimStart() method to remove spaces from above string.

string name = " Rajesh";
name = name.TrimStart();
What do we mean by Static Class?

An static class has static keyword.A static classes are the classes whose object can not be created and we can not use New keyword to instantiate object.
It has private access modifiers.All the methods, properties, and variables are declared inside Static Classes are static meaning that we have to give static keyword in all methods,fields.

Syntax:

Public Static class projects

{
public static int project_id;

public static Get_Project_Details(int project_id)
{
}
}

Can we declare non-static method inside static classes?

NO,we can not use non-static methods inside static class.Because as we know that all the methods,variables defined inside static class must be static.If we write non-static method,then it will give compile-time error.For static class,everything must be declared as static.
How to access static methods or static variables from within static and non-static class?

We access static methods or static variables from within static and non-static class with class name directly.Means no need to create object of class and access method.

Class_name.Method_name();

Class_name.Variable_name();

What will happen if we create object of an static class?

First of all,we can not create static class object.
But if we explicitly create object,then at compile-time,it will give error as:

Static classes cannot have instance constructors

Meaning that,static class do not have public constructors.It always as private constructors.So this is the reason,we can not create object of static classes.
What will happen if we write non-static method inside static class?

First of all we can not write non-static methods inside static class,but if we try to write non-static methods inside static classes,then we will get compile-time error as:

'Method': cannot declare instance members in a static class

Meaning that if we write non-static methods,then we have to access this method by creating object of class,and as static classes do not have instance constructor.So how can we access non-static methods.that's why we can not write non-static methods inside static class.
Give a simple example of Static class?

public static class static_class

{
public static string status;//Static variable

public static void print() //static method
{
}
}

public class non_static_class
{
//we must not need to create object of static_class
string Status = static_class.status;
static_class.print();
}

What are the type-conversion methods available in Dot Net?

Following are the type-conversion methods which are used to convert to any datatype.

ToBoolean: Converts a type to a Boolean value,where possible.
ToByte: Converts a type to a byte.
ToChar: Converts a type to a single Unicode character, where possible.
ToDateTime: Converts a type (integer or string type) to date-time structures.
ToDecimal: Converts a floating point or integer type to a decimal type.
ToDouble: Converts a type to a double type.
ToInt16: Converts a type to a 16-bit integer.
ToInt32: Converts a type to a 32-bit integer.
ToInt64: Converts a type to a 64-bit integer.
ToSbyte: Converts a type to a signed byte type.
ToSingle: Converts a type to a small floating point number.
ToString: Converts a type to a string.
ToType: Converts a type to a specified type.
ToUInt16: Converts a type to an unsigned int type.
ToUInt32: Converts a type to an unsigned long type
ToUInt64: Converts a type to an unsigned big integer.
What is the use of StreamReader in C#?

StreamReader is a class used in Dot Net to read Text files.As name suggests it reads data from a byte stream.Actually it implements a System.IO.TextReader that reads characters from a byte stream in a particular encoding.

We have to give a proper file path in its constructor otherwise it will give FileNotFound error.

Syntax:-
StreamReader sr = new StreamReader("path of the file");

Which namespace do we use to work with StreamReader class?

We have to import System.IO namespace to work with StreamReader class.

Actually System.IO namespace is used to work with File handling operations like reading files and writing into files.
It contain types that support input and output.
The System.IO namespace has various classes that are used for performing various operation with files such as creating and deleting files,reading from or writing to a file,closing a file.

In short,whenever we want to work with files,then we have to use System.IO namespace.
Give a Simple example of StreamReader class?

using System.IO;

string file_contents;
using (StreamReader sr = new StreamReader("C:\\rpt.txt"))
{
file_contents = sr.ReadLine();
}

if (!String.IsNullOrEmpty(file_contents))
{
MessageBox.Show(file_contents);
}

What is the use of ReadLine() method in StreamReader class.

ReadLine is a StreamReader method which returns the next text line from the text file.It reads line one by one from the text file.

For Example:-

string file_contents;

ArrayList arr_list = new ArrayList();

using (StreamReader sr = new StreamReader("C:\\rpt.txt"))
{
while ((file_contents = sr.ReadLine()) != null)
{
arr_list.Add(file_contents);
}
}

What is the use of Close() method of StreamReader class?

Close() method releases all the objects which are used by StreamReader object and also closes the StreamReader objects.

string file_contents;

using (StreamReader sr = new StreamReader("C:\\rpt.txt"))
{
file_contents = sr.ReadToEnd();
sr.Close();
}

What are the classes available in System.IO namespaces? Explain few of them.

It has several classes like below:

BinaryReader: Reads primitive data types as binary values in a specific encoding.
BinaryWriter: Writes primitive data in binary format.
Directory: Used for creating,moving and manupulating Directory.
FileStream: Used to read from and write to any location in a file.
MemoryStream: Used for random access to streamed data stored in memory.
Path: It is used to get file path.
StreamWriter: It is used for writing characters to a stream in a particular encoding.
Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories