Technical Interview

C# Interview Questions



Several jobs require candidates to have a profound knowledge of C#. These C# Interview Questions have been designed specially to get you acquainted with the nature of questions that you may encounter during your interview for the subject of C#.

1. What is C#?

C# is a general purpose, modern and object-oriented programming language. It was developed by Microsoft within its .NET initiative led by Anders Hejlsberg and his team. It was approved by European Computer Manufacturers Association (ECMA) and International Standards Organization (ISO).


2. What are the features of C#?

Some important features of C# are given below:

  • Simple Language - C# is a simple language. It is very easy to understand and learn.
  • Case-Sensitive Language - Like C, C++ and Java, C# is a case sensitive language and treats the uppercase and lowercase characters in a different manner.
  • Object-Oriented Programming Language – C# is an object oriented programming language. This is one of the most important feature of C#. C# lets a user to implement real-time problems based on data abstraction, data encapsulation, inheritance, data hiding, and polymorphism.
  • Powerful & Fast Language - C# is a fast language as it takes very less time in compilation and execution.
  • Rich Library Support – C# provides lots of inbuilt functions which makes programming faster and easier.
  • Structured Programming Language – C# is a structured programming language that means any C# program can be achieved in parts using functions, classes and objects. This makes any C# program easy to understand and modify.
  • Scalable & Update-able Language – C# is a automatically scalable and update-able language. To scale any application, simply delete older files and update it newer files.
  • Interoperability – C# supports interoperability, hence in a C# application, a developer can do almost anything that can be done in a C++ application, e.g. in C#, pointers can be used as unsafe code blocks to manipulate old codes.
  • Type Safe Language – C# is a type safe language and hence only permissible memory location can be accessed to execute. Therefore it improves security of the program.

3. What are the C# access specifiers?

Access specifiers defines the access type of the fields and methods of the class and there are six types of access specifier in C#.

  • public: Member fields and methods of the class are accessible from everywhere.
  • protected: Member fields and methods of the class are accessible in the same class or struct and by derived class.
  • private: Member fields and methods of the class are accessible within the same class or struct only.
  • internal: Member fields and methods of the class are accessible within the same assembly only.
  • protected internal: Member fields and methods of the class are accessible by any code in the assembly in which it's declared, or from within a derived class in another assembly.
  • private protected: Member fields and methods of the class are accessible only within its declaring assembly, by code in the same class or in a type that is derived from that class.

4. What is a constructor?

A constructor is a special method of a class which automatically executed when a new object of the class is created. It allows the class to initialize member fields and allocate memory.

The constructor function is declared just like a regular member method except the class name and method name should be same without any return type.

When a constructor is not specified in a class, compiler generates a default constructor and inserts it into the code. However, it does not initialize member fields.

//Creating class constructor
className(parameters) {
  statements;
}

5. What is a destructor?

A destructor is a special method of a class which destructs or deletes an object and automatically executed when the object goes out of scope. An object goes out of scope when:

  • the method containing object ends.
  • the program ends.
  • a block containing local object variable ends.
  • a delete operator is called for an object.

To create a destructor in C#, there are certain rules which need to be followed:

  • Destructor should begin with tilde sign(~) followed by class name.
  • A class can have only one destructor.
  • Unlike constructors that can have parameters, destructors do not allow any parameter.
  • Destructor do not have any return type, just like constructors.
  • When a destructor is not specified in a class, compiler generates a default destructor and inserts it into the code.
//Creating class destructor
~className() {
  statements;
}

6.What is constructor overloading?

With constructor overloading feature in C#, two or more constructors can be created in the same class with different definitions like different number and types of parameters. The compiler automatically calls the constructor which matches the argument passed while creating the object.


7. What is method overloading?

With method overloading feature in C#, two or more methods can have the same name but different parameters. It allows the programmer to write methods to perform conceptually the same thing on different types of data without changing the name.

In the example below, two methods with the same name func are created. First method takes two integer numbers as arguments and returns sum of these numbers. The second method takes two float numbers as arguments and returns sum of these numbers.

using System;

class MyProgram {
  static int func(int a, int b) {
    return a + b;
  }
  static float func(float a, float b) {
    return a + b;
  }

  static void Main(string[] args) {
    int a = 100, b = 10;
    float p = 500.5f, q = 50.05f;

    Console.WriteLine("func(int, int) is used. Result: " + func(a,b)); 
    Console.WriteLine("func(float, float) is used. Result: " + func(p,q)); 
  }
}

The output of the above code will be:

func(int, int) is used. Result: 110
func(float, float) is used. Result: 550.55