C# is one of the widely popular programming languages and as few years Microsoft released .NET Core and made .NET cross platform, C# demand is increased more and more, so does C# jobs. So, in this article, I have provided commonly and widely asked C# interview questions, which should be read by every professional C# developer  or beginner developer to increase there C# knowledge. Let's get started, we will start from basic and move on to advanced question/answers.

c-sharp-interview-questions-topinterviewquestion-min.png

What is C#?

C# is a general-purpose, modern, object-oriented based programming language developed by Microsoft in 2000. C# is primary language to create software or web-application using .NET framework.

C# is designed for Common Language Infrastructure (CLI), which consists of the executable code and runtime environment that allows use of various high-level languages on different computer platforms and architectures.

Some features of C#:

  • Object-oriented programming (OOP) concept based.
  • Structured language
  • Automatic Garbage Collection
  • Fast and scalable
  • Simple and easy to learn

What are Jagged Arrays in C#?

A jagges array is an array of arrays in C#. Here is an example of Jagged array in C#

string[][] jaggedArray = new string [2][] {
          new string[] {"banana", "mango"},
          new string[] {"orange", "apple", "watermelon"}
};

In the above example, we defined a Jagged array with name "jaggedArray" with size 2 and then inside the curly bracket we defined and declared its constituent arrays.

Explain about Class and Objects in C#?

C# is object-oriented programming language, that is, it si dependent on class and objects.

Class: We can consider class as a template, declaration or blueprint that is used for classifying the object. It contains variable members, functions, structure, properties and many more components. It is the basic building block of object-oriented programming. In order to create a class, the class keyword is used.

Objects: Object is an instance of a class, we create object to access methods, variable etc of class.

Here is an complete C# Class/Objects example:

using System;
	
public class Student
{
    //class variable
    public string name {get;set;}
    public int age {get;set;}

   //class methods
   public void PrintName()
   {	
       Console.WriteLine("Student name: "+name);
       Console.WriteLine("Student age: "+age);
   }
}
public class Program
{
	public static void Main()
	{
		//create Student class object (student)
		Student student = new Student();
		
		//assign value to student class variables, using object
		student.age=15;
		student.name="John";
		
		//call student class method using object.
		student.PrintName();
	}
}

When calling a function, are parameters passed by value or by reference?

In C#, arguments can be passed to parameters either by value or by reference. Passing by reference enables function members, methods, properties, indexers, operators, and constructors to change the value of the parameters and have that change persist in the calling environment.

To pass a parameter by reference with the intent of changing the value, use the ref, or out keyword.

Example:

class Program
{
    static void Main(string[] args)
    {
        int arg;

        // Passing by value.
        // The value of arg in Main is not changed.
        arg = 5;
        squareValue(arg);
        Console.WriteLine(arg);
        // Output: 5

        // Passing by reference.
        // The value of arg in Main is changed.
        arg = 5;
        squareReference(ref arg);
        Console.WriteLine(arg);
        // Output: 25
    }

    static void squareValue(int valParameter)
    {
        valParameter *= valParameter;
    }

    // Passing by reference
    static void squareReference(ref int refParameter)
    {
        refParameter *= refParameter;
    }
}

What is the difference between ref and out parameters?

Output parameters are similar to reference parameters, except that they transfer data out of the method rather than into it.

Reference parameter copies the reference to the memory location of an argument into the formal parameter. This means that changes made to the parameter affect the argument.

Can you return multiple values from a function in C#?

Yes, using output parameters we can return multiple values from function in C#.

A Tuple can also be used to return multiple values from a method in C# 7 or above. Example

  class MultipleReturn
    {
        public static void Main()
        {
            (int Id, string StudentName) = GetStudent();
            Console.WriteLine("Id: " + Id + ", Name: " + StudentName);

        }
        static (int, string) GetStudent()
        {
            return (Id: 1, StudentName: "Ramesh");
        }
    }

Output:

Id: 1, Name: Ramesh

Explain all types of Classes in C#

There can be 4 types of classes in C#:

  • Abstratc Class: Abstract Class is declared using "abstract" keyword, this class have methods and functions defined as abstract or non-abstract but we cannot create object of this class.
  • Partial Class: A partial class is a special type of class where a single class can be split into multiple .cs file and while compilation, all the functionalities of same partial classes written in different .cs files will be merged and then compile it.
  • Sealed Class: Sealed classes are used to restrict the inheritance feature of object oriented programming. It cannot be inherited in another class. The variable & methods in sealed class can be accessed in main(). 
  • Static Class: It is the type of class that cannot be instantiated, in other words, we cannot create an object of that class using the new keyword and the class members can be called directly using their class name.

What is the difference between a class and a struct?

Here is the difference between both:

Class

  • It can support inheritance
  • Classes are reference type
  • It can have null reference
  • It has memory overhead per new instance

Struct

  • Doesn't support inheritance
  • Passed by Value and are value type
  • It cannot have null reference unless Nullable is used
  • Doesn't have any memory allocation

Both have common:

  • Can have methods
  • Can be inherited
  • Both are compound data-type

What are the different types of collections in .Net?

There are two main types of collections: generic collections and non-generic collections.

Generic collections were added in the . NET Framework 2.0 and provide collections that are type-safe at compile time. Because of this, generic collections typically offer better performance. Here are the type of Generic collecitons

  • Dictionary<TKey,TValue>
  • List<T>
  • Queue<T>
  • Stack<T>
  • HashSet<T>
  • LinkedList<T>
  • SortedList<TKey,TValue>

Non-generic collections store items as Object, require casting. It is a general-purpose data structure that works on object references, so it can handle any type of object, but not in a safe-type manner. Here are the type of non-generic collections

  • ArrayList
  • HashTable
  • Queue
  • Stack

What is boxing and unboxing in C#?

When a value type is converted to object type, it is called boxing.

When an object type is converted to a value type, it is called unboxing.