Top 50 C# Interview Q&A
Top 50 C# Interview Q&A
Top 50 C# Interview Q&A
Example:
//This is a single line comment
ii. Multiple line (/* */)
Example:
/*This is a multiple line comment
We are in line 2
Last line of comment*/
iii. XML Comments (///).
Eg:
/// summary;
/// Set error message for multilingual language.
/// summary
3. Can multiple catch blocks be executed?
No, Multiple catch blocks of similar type can’t be executed. Once the proper
catch code executed, the control is transferred to the finally block, and then
the code that follows the finally block gets executed.
5. What is an object?
6. Define Constructors
A constructor is a member function in a class that has the same name as its
class. The constructor is automatically invoked whenever an object class is
created. It constructs the values of data members while initializing the class.
pTutor.SetTutorial(1,".Net by Guru99");
Console.WriteLine(pTutor.GetTutorial());
Console.ReadKey();
}
}
}
14. What are value types and reference types?
A value type holds a data value within its own memory space. Example
int a = 30;
Reference type stores the address of the Object where the value is being
stored. It is a pointer to another memory location.
string b = "Hello Guru99!!";
15. What are Custom Control and User Control?
Custom Controls are controls generated as compiled code (Dlls), those are
easier to use and can be added to toolbox. Developers can drag and drop
controls to their web forms. Attributes can, at design time. We can easily add
custom controls to Multiple Applications (If Shared Dlls). So, If they are
private, then we can copy to dll to bin directory of web application and then
add reference and can use them.
User Controls are very much similar to ASP include files, and are easy to
create. User controls can’t be placed in the toolbox and dragged – dropped
from it. They have their design and code-behind. The file extension for user
controls is ascx.
Eg:
try {
GetAllData();
}
catch (Exception ex) {
}
In the above example, we can omit the parameter from catch statement.
34. What is the base class in .net from which all the classes
are derived from?
System.Object
35. What is the difference between method overriding and
method overloading?
In method overriding, we change the method definition in the derived class
that changes the method behavior. Method overloading is creating a method
with the same name within the same class having different signatures.
36. What are the different ways a method can be
overloaded?
Methods can be overloaded using different data types for a parameter,
different order of parameters, and different number of parameters.
Eg:
public int this[int index] // Indexer declaration
46. What is difference between the “throw” and “throw ex”
in .NET?
“Throw” statement preserves original error stack whereas “throw ex” have the
stack trace from their throw point. It is always advised to use “throw” because
it provides more accurate error information.
Eg:
Public sealed class Singleton
{
Private static readonly Singleton _instance = new Singleton();
}
49. What is the difference between directcast and ctype?
DirectCast is used to convert the type of object that requires the run-time type
to be the same as the specified type in DirectCast.
Ctype is used for conversion where the conversion is defined between the
expression and the type.
namespace DemoApplication
{
class Program
{
static void Main(string[] args)
{
Queue qt = new Queue();
qt.Enqueue(1);
qt.Enqueue(2);
qt.Enqueue(3);