C# interview questions and answers

C# aka. "C sharp" is a programming language runs on the .NET Framework and used for building a variety of applications. C# is type-safe, powerful and object-oriented.

What is an object?

An object is an instance of a class using which we can access the properties, methods of that class. An object is created using "New" keyword. An object in memory contain the information about the methods, properties and behavior of that class.

What is Constructors?

Constructor is special method of the class which comes with same name as class.Constructor will be automatically invoked when an instance of the class is created. Constructors are mainly used to initialize private fields of the class while creating an instance for the class. If class does not contain a constructor, then compiler will automatically create a default constructor in the class that initializes all numeric fields in the class to zero and all string and object fields to null. Syntax:
[Access Modifier] ClassName([Parameters])
{
}
Example of default constructor:

public class MyClass
{
    public MyClass()
    {
    }
}


Types of Constructors

Below are the constructor types:
Default Constructor
Parameterized Constructor
Copy Constructor
Static Constructor
Private Constructor

What is the difference between ref & out parameters?

An argument passed as ref must be initialized before passing to the method whereas out parameter needs not to be initialized before passing to a method.

What is difference between constants and read-only

The "readonly" keyword is different than that of const keyword.
A const field can only be initialized at the declaration of the field. A "Const" field is a compile-time constant.

A readonly field can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used. The readonly field can be used for runtime constants.

What are value types and reference types?

Variables those holds actual data and stored on stack are "Value" types.
Ex. int, enum , byte, decimal, double, float, long

Variables those holds reference to the actual data are of "Reference" types.
Ex. string , class, interface, object

What are sealed classes in C#?

In C#, the "sealed" modifier prevents other classes to be inheriting from it.You can also use the sealed modifier on a method or property that overrides a virtual method or property in a base class. This enables you to allow classes to derive from your class and prevent them from overriding specific virtual methods or properties

What is method Overloading?

Overloading is when you have multiple methods in the same scope, with the same name but different signatures. Also known as Compile Time Polymorphism. Ex.
//Overloading
public class OverloadClass
{
    public void someMethod(int id)
    {}
    public void someMethod(string name)
    {}
}

What is method Overriding?

Functions in the extended class with same name and same parameters as in the base class, but with different behaviors. Ex.
//Overriding
public class OverridingClass
{
   public virtual someMethod(int id)
   {
      //Get stuff default location
   }
}

public class test2 : test
{
   public override someMethod(int id)
   {
       //base.getStuff(id);
       //or - Get stuff new location
   }
}

What is the difference between Array and Arraylist?

In an array, we can have items of the same type only. The size of the array is fixed. An ArrayList hold data type of an object, So using array with different types can cause runtime error. It does not have a fixed size.

Which are Access Modifiers available in C#?

All types and type members have an accessibility level, which controls whether they can be used from other code in your assembly or other assemblies.
You can use the following access modifiers to specify the accessibility of a type or member when you declare it:

public: The type or member can be accessed by any other code in the same assembly or another assembly that references it.
private: The type or member can be accessed only by code in the same class or struct.
protected: The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.
internal: The type or member can be accessed by any code in the same assembly, but not from another assembly.

What is static constructor?

Static constructor is used to initialize static data members as soon as the class is referenced first time. A static constructor does not take access modifiers or have parameters and can't access any non-static data member of a class.

What is Reference Type in C# ?

Let us explain this with the help of an example. In the code given below,
Employee emp1;
Employee emp2 = new Employee();
emp1 = emp2;
Here emp2 has an object instance of Employee Class. But emp1 object is set as emp2. What this means is that the object emp2 is referred in emp1, rather than copying emp2 instance into emp1. When a change is made in emp2 object, corresponding changes can be seen in emp1 object.

What is Abstract Class in C#?

If we don't want a class to be instantiated, define the class as abstract. An abstract class can have abstract and non abstract classes. If a method is defined as abstract, it must be implemented in derived class. For example, in the classes given below, method DriveType is defined as abstract.
abstract class Car
{
 public Car()
 {
  Console.WriteLine("Base Class Car");
 }
 public abstract void DriveType();
}

class Ford : Car
{
 public void DriveType()
 {
  Console.WriteLine("Right Hand ");
 }
}
Method DriveType get implemented in derived class.

What is Sealed Classes in c# ?

If a class is defined as Sealed, it cannot be inherited in derived class. Example of a sealed class is given below.
public sealed class Car
{
 public Car()
 {
  Console.WriteLine("Base Class Car");
 }

 public void DriveType()
 {
  Console.WriteLine("Right Hand ");
 }
} 

What is the use of var keyword in C#?

This is the new feature in C# 3.0. This enable us to declare a variable whose type is implicitly inferred from the expression used to initialize the variable.
eg.
var age = 10;
Because the initialization value (10) of the variable age is integer type of age will be treated as integer type of variable. There are few limitation of the var type of variables.