Following are some Interview Questions and Answers set of C# 2.0.
What is the difference between ToString() and Convert.ToString()?
Convert.ToString() can handle null values
string str1 = null;
string str = Convert.ToString(str1);
This code will execute. ToString() cannot handle null
string str1 = null;
string str = str1.ToString();
This code will throw error
Object reference not set to an instance of an object
As far as performance is concerned ToString() is better than Convert.ToString() If you are sure that your object is not null then it is better to use ToString()
How do you mark a method obsolete?
We can mark the method obsolete so that when the code tries to use that method compiler issues a warning.
[Obsolete]
public string Test()
{
return "this is obsolete method";
}
When the code use the method like
string str = Test();
compiler will warn that this method is obsolete.
Can we inherit struct?
You can’t inherit struct from a struct or a class. But struct can implement interface
public struct MyStruct : MyInterface
{
public void Display()
{
//DO something
}
}
public interface MyInterface
{
void Display();
}
What is hiding in C#?
Hiding means giving a new implementation to the base class member without overriding the member. You can hide the member of base class in the derived class using the “new”.
Consider an example
public class class123
{
public string display()
{
return "Hi";
}
}
public class class456 : class123
{
public new string display()
{
return "Hello";
}
}
In this example I have given a new implementation to the base class function display.
Difference between out and ref?
In case of Ref initialization of variable is necessary before pass it to the function.
Let us consider an example
protected void Page_Load(object sender, EventArgs e)
{
int a , b;
Increment(ref a, ref b);
}
public void Increment(ref int x, ref int y)
{
x= 0;
y= 0;
x++;
y++;
}
This program will give error
Use of unassigned local variable ‘a’
Use of unassigned local variable ‘b’
Because in this program a and b are not initialized before pass it to the function. In case of out no initialization is required before passing it to the function.
protected void Page_Load(object sender, EventArgs e)
{
int a , b;
Increment(out a, out b);
}
public void Increment(out int x, out int y)
{
x= 0;
y= 0;
x++;
y++;
}
This program will work fine.
Is it possible for a class to implement 2 interfaces that contain a member with the same signature?
Consider 2 interfaces I1 and I2 having Display() function. Signature of Display function in both the interfaces are same.
public interface I1
{
string Display();
}
public interface I2
{
string Display();
}
Now I declare a class that implements both the interfaces:
public class Class1 : I1,I2
{
string I1.Display()
{
return "Hello";
}
string I2.Display()
{
return "hi";
}
}
Now declare the object of the class Class1
Class1 obj = new Class1();
Declare the objects of interface I1and I2
I1 obj1 = (I1)obj;
string str1 = obj1.Display(); //Will execute the Display function of interface I1
I2 obj2 = (I2)obj;
string str2 = obj2.Display(); //Will execute the Display function of interface I2
What is the sealed class in C#?
The sealed modifier is used to prevent your class from being inherited and becoming a base class for some other classes. An error occurs if you specified the sealed class as the base class of another class.
public sealed class MyClass
{
public void Get()
{ }
}
public class SecondClass : MyClass
{
public virtual void print()
{
MessageBox.Show("Print");
}
}
This program will give error as you cannot inherit the sealed class.
What’s the difference between Page.RegisterClientScriptBlock and Page.RegisterStartupScript ?
RegisterStartupScript method places the JavaScript at the bottom of the ASP.NET page right before the closing form element. RegisterClientScriptBlock method places the JavaScript directly after the opening form element in the page.
Consider a code
<form id="frmTest" runat="server">
<div>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</div>
</form>
In the code behind file I have called RegisterClientScriptBlock function to call a JavaScript code.
Page.RegisterClientScriptBlock("Test", "<script type='text/javascript'>document.frmTest.txtName.focus();</script>");
This script will give me error
document.frmTest.txtName is null or not an object
The reason for this error is browser will encounter the JavaScript code before the TextBox
But in case of RegisterStartupScript
Page.RegisterStartupScript("Test", "<script type='text/javascript'>document.frmTest.txtName.focus();</script>");
This script will work fine because when the browser encounter the JavaScript code the object txtName has been created.
What are virtual property or methods?
A virtual property or method has an implementation in the base class, and can be overridden in the derived classes.
Consider an example
public class MainClass
{
public virtual void Test()
{ }
}
public class SecondClass : MainClass
{
public override void Test()
{ }
}
Here we are overriding the Test() method in derived class so we have to declare the method Test() as virtual in base class.
Note → To override a method of the base class, the base class method should be either virtual, override or abstract.
- You can’t declare a method of base class as abstract if the class is non abstract
- If the base class is at level 0 i.e the class is not derived from any class then you can’t write method as override
Give reason
int i =0;
byte j =0;
Is i.Equals(j) true;
Is j.Equals(i) false;
i.Equals(j) where i is int and j is byte. byte’s range is smaller than int, so byte will be implicitly changed to int. As the value of both the variables are same so the output of this expression is true.
In case of j.Equals(i) int’s range is larger than byte so i cannot implicitly change into j. Now they have the same value but different type hence the output of this expression is false.