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

Can we define generics for any class?

No, generics is not supported for the classes which deos not inherits collect class.
What is the difference between Parse and TryParse method?

Parse method is used to parse any value to specified data type.

For example

string test = "42";
int32 Result;
Result = Int32.Parse(test);

This will work fine bt what if when you are aware about the value of string variable test.

if test="abc"....

In that case if u try to use above method, .NET will throw an exception as you are trying to convert string data to integer.

TryParse is a good method if the string you are converting to an interger is not always numeric.

if(!Int32.TryParse(test,out iResult))
{
//do something
}


The TryParse method returns a boolean to denote whether the conversion has been successfull or not, and returns the converted value through an out parameter.

**declare variable iResult of Int32.
What is the difference between a.Equals(b) and a == b?

For value types : “==” and Equals() works same way : Compare two objects by VALUE
Example:
int i = 5;
int k= 5;
i == k > True
i.Equals(k) > True

For reference types : both works differently :
“==” compares reference – returns true if and only if both references point to the SAME object while
"Equals" method compares object by VALUE and it will return true if the references refers object which are equivalent
Example:
StringBuilder sb1 = new StringBuilder(“Mahesh”);
StringBuilder sb2 = new StringBuilder(“Mahesh”);
sb1 == sb2 > False
sb1.Equals(sb2) > True

But now look at following issue:
String s1 = “Mahesh”;
String s2 = “Mahesh”;
In above case the results will be,
s1 == s2 > True
s1.Equals(s2) > True
String is actually reference type : its a sequence of “Char” and its also immutable but as you saw above, it will behave like Value Types in this case.
Exceptions are always there ;)

Now another interesting case:
int i = 0;
byte b = 0;
i == b > True
i.Equals(b) > False
So, it means Equals method compare not only value but it compares TYPE also in Value Types.

Recommendation :
For value types: use “==”
For reference types: use Equals method.
What is optional and named parameter in C#4.0?

Optional parameter allows omitting arguments to function while named parameters allow passing arguments by parameter name.
By declaring below variable you are assigning default values to second and third parameter of 2 and 3 respectively (param2 and param3).
public void optionalParam(int Param1, int param2 = 2, int param3 = 3);

After this you can write,
optionalParam(1); //which will be equivalent to optionalParam(1,2,3);

Sometimes, you may need to not to pass param2. But, optionalParam(1,,3) is not valid statement with C#. At this point, named parameter comes to the picture.

You can specify arguments like,
optionalParam(1, param3:10); //which will be equivalent to
optionalParam(1,2,10);
Can you have different access modifiers on the get/set methods of a property in C#?

No. it's not possible. The access specifier has to be same for get and set.
What is obsolete method?

Obsolete means old or no longer in use. We can define a method as obsolete using Obsolete keyword/attribute.

[Obsolete] 

public int Fun1()
{

//Code goes here.

}


or

[Obsolete("Any user define message")] public int Fun1() 

{

//Code goes here..

}


One thing to note here is O is always capital in Obsolete.
What first action compiler will take on detection of iterator ?

As soon as compiler will detect iterator, it will automatically generate current, MoveNext and Disposemethods of the IEnumerator or IEnumerator(T) type.
With yield break statement, control gets …

NOTE: This is objective type question, Please click question title for correct answer.
Restrictions of yield in try-catch.

While using yield keyword, mainly two restrictions are observed.
First is , we can’t use yield in finally.
Second is , we can’t place yield keyword in the catch block if try contains more than one catch blocks.
What are anonymous methods?

Anonymous methods are another way to declare delegates with inline code except named methods.
What will happen if you declare a variable named "checked" with any data type?

Compiler will throw an error as checked is a keyword in C# So It cannot be used as variable name. Checked keyword is used to check the overflow arithmetic checking.
What is dynamic keyword ?

Its newly introduced keyword of C#4.0. To indicate that all operations will be performed runtime.
What is difference between var and Dynamic ?

Var word was introduced with C#3.5(specifically for LINQ) while dynamic is introduced in C#4.0. variables declared with var keyword will get compiled and you can have all its related methods by intellisense while variables declared with dynamic keyword will never get compiled. All the exceptions related to dynamic type variables can only be caught at runtime.
What is the use of unsafe keyword in C#?

In C# the value can be directly referenced to a variable, so there is no need of pointer. Use of pointer sometime crashes the application. But C# supports pointer, that means we can use pointer in C#.

The use of pointer in C# is defined as a unsafe code. So if we want to use pointer in C# the pointer must be present in the body of unsafe declaration. But pointer does not come under garbage collection.

Example:-

unsafe
{
int a, *b;
a = 25;
b = &a;
Console.WriteLine("b= {0}",b);//returns b= 25
}
How can you make your machine shutdown from your program?

Process.Start("shutdown", "-s -f -t 0");
int? d = 1; Type testType = d.GetType(); will result…

NOTE: This is objective type question, Please click question title for correct answer.
I need to restrict a class by creating only one object throughout the application. How can I achieve this?

We can declare the constructor of the class as either protected or as private
How do we retrieve day, month and year from the datetime value in C#?

Using the methods Day,Month and Year as follows

int day = dobDate.Day;

int month = dobDate.Month;i
nt year = dobDate.Year;

What will be the length of string type variable which is just declared but not assigned any value?

As variable is not initialized and it is of reference type. So it's length will be null and it will throw a run time exception of "System.NullReferenceException" type, if used.
C# delegate keyword is derived form which namespace?

C# delegate keyword is derived form System.MulticastDelegate.
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