C# Interview Question and Answers with Example - Part 3


Below is top C# interview question and answers with examples for freshers and intermediate developers.

What is Default Constructor?

A constructor without any parameter is called as default constructor. Default constructor initializes numeric fields to zero and string and object fields to null. Ex.

class Program
{
    int a;
    int b;
    public Program()
    {
        b = 10;
    }
    static void Main(string[] args)
    {
        Program p = new Program();
        Console.WriteLine(p.a); // output: 0  ,here default value is automatically initialized to zero
        Console.WriteLine(p.b); // output: 10  ,here we have initialized with value 10
        Console.ReadKey();
    }
}

What is Parameterized Constructor?

A constructor with at least one parameter is called as parameterized constructor. Using parameterized constructor we can override default values with own values. Ex.

class Program
{
    int a;
    int b;
    public Program(int p, int q)
    {
        a = p;
        b = q;
    }
    static void Main(string[] args)
    {
        Program p = new Program(10, 20);
        Console.WriteLine(p.a); // output: 10  
        Console.WriteLine(p.b); // output: 20 
        Console.ReadKey();
    }
}

Parameterized and Default Constructor together

Here in below example we have used parameterized and default constructor together. But, while creating object, we are calling parameterized constructor to initialized value of variable ‘a’ and ‘b’. While debugging application, default constructor is not called and still value of variable ‘c’ is initialized to ‘zero’. Ex.

class Program
{
    int a;
    int b;
    int c;
    public Program(int p, int q)
    {
        a = p;
        b = q;
    }
    public Program()
    {
        a = 30;
        b = 40;
        c = 50;
    }
    static void Main(string[] args)
    {
        Program p = new Program(10, 20);
        Console.WriteLine(p.a); // output: 10  
        Console.WriteLine(p.b); // output: 20 
        Console.WriteLine(p.c); // output: 0 
        Console.ReadKey();
    }
}

What is Copy Constructor?

C# doesn’t provide a copy constructor for objects, but you can write one yourself.
A copy constructor is a type of parameterized constructor where we pass parameter of same class type. Copy constructor is used to initialize new instance by using existing instance. Ex.

class Program
{
    int a;
    int b;
    public Program(int p, int q)
    {
        a = p;
        b = q;
    }
    public Program(Program p)
    {
        a = 10 + p.a; // adding 10
        b = 10 + p.b;
    }
    static void Main(string[] args)
    {
        Program p1 = new Program(10, 20);
        Console.WriteLine(p1.a + " and " + p1.b); // output: 10 and 20
        // here passing existing object p1
        Program p2 = new Program(p1);
        Console.WriteLine(p2.a + " and " + p2.b); // output: 20 and 30
        Console.ReadKey();
    }
}

What is Static Constructor?

A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced.
A static constructor does not take access modifiers or have parameters. Key Points: A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
A static constructor cannot be called directly.
The user has no control on when the static constructor is executed in the program.
A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.
If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running.
Ex.

class Program
{
    public Program()
    {
        Console.WriteLine("Default constructor is called");
    }
    static Program()
    {
        Console.WriteLine("Static constructor is called only once and also before first instance is created");
    }
    static void Main(string[] args)
    {
        // here static constructor is called before object creation
        Program p1 = new Program(); // default constructor is called
        Program p2 = new Program(); // default constructor is called again
        Console.ReadKey();
    }
}
// Output:
// Static constructor is called only once and also before first instance is created
// Default constructor is called
// Default constructor is called

What is Private Constructor?

We will update soon.....

What is extension method in C#?

Extension method is used to add or extend functionality of own class or in-built classes of .net. Here, in below example we have created a static class MyStaticClass and static method RemoveDuplicateChars which removes duplicate characters of specified string. This method can be applied on string data type.

Ex:
class Program
{
    static void Main(string[] args)
    {
        string output = "aabbcdxbdcx";
        Console.WriteLine("String without duplicates char: " + output.RemoveDuplicateChars());
        Console.ReadKey();
    }
}
public static class MyStaticClass
{
    public static string RemoveDuplicateChars(this string inputString)
    {
        string newString = string.Empty;
        var unique = new HashSet<char>(inputString);
        foreach (char c in unique)
        {
            newString += c.ToString();
        } 
        return newString;
    }
}
// OUTPUT:
abcdx

What is difference between int.Parse and Convert.ToInt32?

1) Both are used to convert string to number in C#.
2) Convert.ToInt32 returns 0 when argument is null where as int.Parse returns Argument Null Exception.

What is difference between int.Parse and int.TryParse?

1) Both are used to convert string to number in C#.
2) int.Parse method throws excepton if string is not valid integer.
3) int.TryParse returns true along with integer value as out parameter if string is valid number.
4) int.TryParse returns false and integer value to 'zero' as out parameter if string is not a valid number.

What is implicit and explicit conversion?

Implicit conversion:
1) Implicit conversion is done automatically by compiler when we assign lower data type to higher data type.
2) There is no loss of data while conversion.
ex.
int a = 10;
double b = a;
Explicit conversion:
1) When we assign higer data type to lower data type then explicit conversion is required.
2) There may be loss of data while conversion.
ex.
//successful conversion
double a = 10;
int b = (int)a; // output : 10

//failed conversion
double a = 1023545788555;
int b = (int)a; // output : -2147483648 it returns min value of int

What is difference between int and int32?

Both are same. int is an alias for Int32.

Difference between bool and boolean in C#

bool is an alias for the Boolean class.

What is difference between signed int and un-signed int?

1) signed int stores negative as well as positive value.
Ex.
sign int (32 bit):
int a = -2147483648; // min value;
int b = 2147483647; // max value;

2) un-signed int only stores positive value.
Ex.
unsign int (32 bit):
uint a = 0; // min value;
uint b = 4294967295; // max value;

What are different types of comment in C#?

There are 3 types of comment in C#.
1) Single line // single line comment
2) Multiline /*This is multiline commentt*/
3) Xml comment
/// <summary>
/// about class definition.
///</summary>

What is difference between method parameter and method argument?

When we declare method with some input or output parameter then it is called as method parameter.
When we actual pass the data to the method using input or output then it is called as method argument.
Basically value of parameter is called as argument.

Which version of C# is supported in visual studio 2013?

C# version 5.0 in .net framework 4.5
C# version 6.0 in .net framework 4.6

What are the types of parameter that can be passed to a method?

passing value type, passing ref type, passing out type, passing using [params array].

What is delegate in C#?

A delegate is a type safe pointer to a function.
This means delegate signature should match with method signature that are being reference.
Delegate is a reference type.
Delegates are mostly used in call back mechanism.
Many methods can be registered with delegate and can be executed at run time when respective event occurs.


Page 3 of 7