C#  

C# Interview Question Answers

In this series of Interview questions, I tried to cover a very minor questions which sometimes gives out of syllabus feeling during interview. This is the first part of the series. Hope you find it very helpful.

What is C#?

C# is an object-oriented language that is invented by Microsoft in 2000. It is a part of .net framework. It is used to build many types of applications such as websites, mobile applications, desktop applications and cross platform form applications. Initially it was not open source but now Microsoft made it open source. It’s very first version was launched in 2002.

What is open source?

Open-source term is used to describe a software/component whose code is publicly available and anyone can modify it and use it for their own purpose. It also boosts the flexibility of the software as anyone can contribute towards the betterment of the software.

What is .Net framework?

.NET framework is the collection of Microsoft technologies under one package which is used to create robust and scalable various types of applications. Net stands for Network Enabled Technology. >net supports 60+ languages, 9 are designed by Microsoft and rest are non-Microsoft. It initially focused on windows only but now days (updated version .NET core) it focusses on cross platform operating systems. It consists of 2 components

  • Class libraries (CL)

  • Common Language Runtime (CLR)

What are the Class Libraries?

Class Libraries are the pre-built written code libraries by Microsoft for user convenience. User can import those libraries and can-do specific action. It is basically collection of various data types and namespaces.

What is CLR?

The CLR is the heart of .net environment that helps the execution of user written code. It converts the MSIL (Microsoft Intermediate language) to the native code and provides the runtime environment to execute the code. It also provides services like thread management, type safety and exception handling.

ChatGPT Image Apr 20, 2026, 05_20_53 PM

What is MSIL/IL?

The MSIL/IL is half compiled code which is CPU independent and can’t be run on any OS. CLR takes it and gives to JIT (Just In Time) compiler and converts it into machine language according to the environment.

What are the main parts of CLR?

There are following parts of CLR

  • Security Manager

  • JIT Compiler

  • Memory Manager

  • Garbage Collector (GC)

  • Exception Manager

  • Common Language Specifications (CLS)

  • Common Type System (CTS)

What is Garbage Collector?

Garbage Collector is automatic memory collection facility provided by CLR. The GC handles the memory allocation and memory release by itself. The GC uses heap memory for memory calculations. Developer can also call the GC by calling GC.Collect() method where needed, but this is not recommended for production as it can cause some unexpected behavior of the application.

What are Common Language Specifications?

CLS is the set of syntactical rules and restrictions which are used to write code for different languages. Every single code language has its own rules to write code and during compilation, compiler use these rules to convert the code into MSIL. The CLS also helps is cross language integration in case of multiple languages are involved in a project.

What is Common Type System?

CTS is related to the data types of different languages. During the time of compilation, a language’s data types are converted into CLR’s own data types which are known as CTS. CTS provides common data types in case of multiple languages which is very beneficial for compiler to understand the code and execute in effective manner.

What is object-oriented language in simple practical manner?

Object can be anything such as a person or any feature of a particular thing. Let’s take an example of an Appointment Scheduling software of a hospital. In this software there will be multiple sections that will be work as an object such as an applicant, a doctor, an application of the applicant and many more. These objects will communicate with each other and make a productive decision as a result.

What is IDE?

IDE is Integrated Development Environment a software used to write and debug code and run applications. There are many IDE available such as Visual Studio for C# and many other languages, Android Studio for Android development etc.

What is Managed and Unmanaged code?

The code written under .net framework is known as Managed code and the code which is not written under .net framework language is known as Unmanaged code. The Unmanaged code can’t be run under CLR and executed by any OS directly.

Is C# a case sensitive language?

Yes, C# is a case sensitive language. The class defined with MyClass name will not accept as myclass name. Compiler will treat it as different class.

What is difference between WriteLine() and Write() methods?

Both methods are used to write some message in respective application. WriteLine method add a newline character after the end of message while Write method keeps the control in same line.

Picture1Picture2

What are various types of comments in C#?

Single Line: Single line comment used to comment code for a single line and denoted with double slashes (//) used just before the starting of code. It also can be used for multi lines.

Multiline Comments: Multiline Comments are used to comment code for multi lines. It starts with /* and end with */

Picture3

What is a variable?

A variable is a container to store value. There are following type of variables available in C#.

  • Int, long: whole numbers (123, -123)

  • double, float: decimals (0.22)

  • char: single character (‘a’)

  • string: full text (“This is string”)

  • bool: true or false

  • DateTime: date time of different time zones

What is a literal?

Literal is the fixed (or hard coded) value given to a variable and this value can’t be modified during the execution of the program.

  
    int x = 100;
  

here x is variable name and 100 is a literal.

What is a Constant?

Constant means unchangeable and read-only. Constant is a keyword used to assign pre-defined value such as pi. The constant is only used when we already know the value.

  
    const double pi = 3.14;
  
  • A value must be assigned to a constant variable otherwise it will throw error as its evaluation is done at compile time.

Picture4
  • It can be declared inside the method.

  • Its value can’t be changed.

  • It can’t be used with static keyword.

What is ReadOnly?

A ReadOnly variable is used to set read only value at compile time. Its value can be changed after declaration.

  • As its evaluation is done at runtime its value can be assigned while declaring and in constructor.

  • It can be used with static keyword.

  • It is commonly used to read config files and database connections.

How we can define multiple variables?

There are 2 ways to define multiple variables:

  • Using single variable declaration

  
    int a = 0;
 int b = 1;
  
  • Using multiple variable declaration

  
    int a = 0, b=1;
  

What is var keyword and why it is used?

Var keyword introduced in C# 3.0 used to declare variable without declaring any explicit data type i.e. especially when we don’t know what would be the data type of that variable. The type of the variable is recognized at compile time.

  
    var x = 90;
  
  • The var keyword variable must be initialized in same statement as it has early binding type behavior.

  
    var x;  //error var must be initialized
 var x = 0;  //correct way
  
  • The var keyword variable can’t be initialized with null.

  
    var x = null;  // error
  
  • Multiple variables can’t be declared using var keyword.

  
    var x = 0, y = 1, z = 2;  //error
  
  • Var keyword variable is only allowed to declare inside a method not at class level.

  • It performs better in most of cases as it does not require boxing and unboxing.

What is dynamic keyword and how it is different from var keyword?

Dynamic keyword was introduced in C# 4.0. It is also giving a free hand to developer to define variable without any explicit data type. Since the type is assigned by compiler at runtime developer can initialized it without any value. The initial IntelliSense help (VS show its methods) is not available for dynamic variable but available for var keyword variable.

  
    dynamic x; // no error
 dynamic x = 0; // no error
  

What operators are used in C#?

Operators are used to define some specific action between variables and iterations. There are following types of operators available in C#:

  • Arithmetic operator : Addition (+), Subtraction (-), Multiply (*), Divide (/), Modulus (%)

  • Assignment operator: Assignment (=), +=, -=, *=, /=

  • Comparison operator: Equal to (==), Not equal to (!=), Greater than (>), Less than (<)

  • Logical operator : AND (&&), OR (||), and NOT (!)

  • Unary operator : ++, --

  • Ternary operator : ? !

What is Type casting?

Type Casting is the process of converting one data type to another. There are 2 types of Type casting as follows:

  • Implicit (Automatically)

  
    int i = 9;
 double j = i;
 
//output
Console.WriteLine(i); // 9
Console.WriteLine(j); // 9
  
  • Explicit (Manually)

  
    double i = 9.44;
 int j = (int)i;
 //output
 Console.WriteLine(i); // 9.44
 Console.WriteLine(j); // 9
  

What is an Enum?

Enum are Enumerations. Enums are strongly type name constant which are used to assign a value to each member of enum. The default type of an enum is int. Enums are sealed and can’t be inherit. Classes are not allowed to inherit enum. Enums are very useful for comparison purpose. For example, in a project developer can declare a Status enum and use it anywhere where status is involved. One major benefit of enum is that it is easy to handle as its value can be changed at one place, you do not need to change the value where status is used in the project.

  
    public enum Status  
{
   Active,
   Inactive
}
  

To consume enum it can be access directly like Status.Active. It also can be convert into int i.e. Convert.ToInt(Status.Active), it will return 0 as value. The 0 value is assigned by default. Developer can assign any value like

  
    public enum Status  
{
   Active = 100,
   Inactive = 200
}
  

What are Escape Characters?

Escape characters are used to represent some special character in the sentence. They are start with back slash (\).

Escape characterNameDescription
\'Single Quoteused for char data type to show single quote in the code
\"Double Quoteused for string data type to show double quote in the code
\\Backslashused to show single slash is code
\nNew Lineused to start a new line
\rCarriage Returnused to return the cursor to the beginning of the line.
\tHorizontal Tabinsert a tab space
\0Nullrepresents Unicode null character
\eEscaperepresents escape (ESC) character

What is String Interpolation?

String Interpolation is the way to represent a string with dynamic value in readable syntax form. It was introduced in C# 6. The $ sign is used to write the syntax.

  
    string s = $“My name is {firstName} {lastName}”;
  

What are Access Modifiers?

Access modifiers are used to set access level of a class, method and variable. There are following types of Access modifiers in C#:

  • Public: This means respective module is public for all.

  • Private: Only accessible within same module.

  • Protected: Accessible in same module or in that module which is inherit from parent module.

  • Internal: Accessible within own assembly.

  • Protected Internal: Accessible in current Assembly and its inherited assembly.

What is a property?

Property is a field that is a combination of variable and get set methods. Properties are used to carry data from one module to another.

  
    public string Name
{
    get {  return name;  }
    set { name = value; }
}
  

What is an Assembly?

An Assembly is the building block of .Net. It is compiled managed code library whom developer can import and use it according to the requirement. It can be directly run under CLR. It has 2 forms either in .dll extension or in .exe extension.

The exe file is executable file that can be run directly on Windows, whereas DLL is Dynamic Link Library which can be imported into any VS project.

What is checked and unchecked keywords and how they work?

The check and uncheck keywords are used for arithmetic conversions. The check keyword is used to explicit enable arithmetic overflow, whereas unchecked keyword is used to suppress arithmetic conversion overflow.

  
    class program 
{
     static void Main() 
     {
          int a = int.MaxValue;
          int b = int.MaxValue;
          int c = checked(a + b); // error arithmetic overflow
          int d = unchecked(a + b); // -2
     }
}
  

What is a Class?

A Class is a template for objects and object is an object is an instance of a class. A Class is act like group of something (i.e. object) that act like a blueprint for whole group. And an object is the replica of a class that will be used for some specific tasks.

What are the types of Classes?

There are following types of Classes in C#

  • Concrete

  • Abstract

  • Partial

  • Sealed

  • Static

What is Concrete Class?

A concrete class is standard class defined with class keyword and user can create it instances multiple time. There is no restriction on it, it can be instantiated and fully inherited.

  
    public class User
{
     public string Name { get; set; }
     public int Age { get; set; }
}
  

 What is Abstract Class?

An Abstract class is a restricted class that can’t be used to create objects, which is especially used to act as a base class for another class. It is used for Abstraction purpose i.e. the process of showing essential information to end user. This class is defined by abstract keyword. Few points about abstract class

  • An abstract class can’t be instantiated.

  • An abstract class can have abstract as well as non-abstract methods.

  • An abstract class can’t be sealed.

  • If any abstract method is declared in an abstract class, then in derived class it should be implemented.

  • An abstract class can be inherited into another abstract class, in this case implementation of abstract methods of base class is optional in derived class.

  • An abstract class can have constructor and destructor.

  • An abstract class can’t be private.

  
    public abstract class SomeActions 
{
     public abstract void ICanSee();
     public void ICanHear()  
     {
         Console.WriteLine(“I Can Hear”);
     }
}
  

What is Partial Class?

A Partial class allows dividing the properties, methods and events of a class into multiple source files i.e. a partial class can be defining multiple times. At compile time, these files are combined into one class and executed by compiler. Few points about Partial class:

  • All parts of a partial class are prefixed by partial keyword.

  • A partial class can’t be inherited.

  • All parts should have same name, namespace and must be in same assembly.

  • All parts should have same access level.

  • If you make partial class sealed or abstract in one part, then it will become sealed or abstract in all parts.

  
    public partial class User
 {
     public string Name { get; set; }
     public int Age { get; set; }
 }
  

What is Sealed Class?

Sealed class is used to restrict the Inheritance i.e. a Sealed class can’t be inherited further. Sealed keyword can also be used in methods.

  
    public sealed class UserTax
 {
      public string Name { get; set; }
      public int TaxID { get; set; }
 }
 public class x
 {
      sealed protected void f() {  ….   }
 }
 public class y: x
 {
      protected override void f() { …… } // error due to sealed method
 }
  

What is Static Class?

A Static class is used to store utility methods, constant data and other type of data which is required frequently without creating the object of the class. This is the benefit of Static class. Few more points about Static class are:

  • A static class can’t be instantiated i.e. you can’t create the instance of static class.

  • All the members (methods and properties) of a static call will be static.

  • Static class is sealed i.e. you can’t inherit static class.

  • Static class can have a static constructer only to initialized static properties.

  
    using System;
 public static class Calc
 {
    public static string Description = "Calc Helper";
    public static int Add(int a, int b)
    {
        return a + b;
    }
    static Calc()
    {
        Console.WriteLine("Welcome to Calc”);
               }
}
  class Program
  {
             static void Main()
            {
                        int sum = Calc.Add(5, 10);
                       Console.WriteLine($"{Calc.Description} Result: {sum}");
                       // The following would cause a COMPILE ERROR:
                      // Calc calc = new Calc();
            }
}
  

What is an Interface?

An Interface is a fully unimplemented class that is used to declaring a set of specific methods for an object. Basically, an Interface is a pure abstract class with only abstract methods and some properties. It is designed to solve the ambiguity problem in C# to handle multiple inheritance.

  
    public interface ITest 
{
     public string name { get; set;}
     public void test1();
}
  

Can we define an Interface without I initials?

Yes, we can. I letter is added to interface just for naming convention purpose, it is not a specification rule for the language.

Define the basic principle of OOPS?

OOPS have basically following principles:

  • Encapsulation

  • Inheritance

  • Polymorphism

  • Abstraction

What is Abstraction & Encapsulation?

Abstraction is the process of hiding certain details and showing only relevant information to the end user, whereas Encapsulation is the process of binding the data and methods into a single unit (class). They are very related to each other; in simple words we can say that an Abstraction is the logical thinking and Encapsulation is its physical implementation.

What is Inheritance?

Inheritance is the process of inheriting fields and methods from one class to another. It is very useful for code reusability. Parent class who is to be inherit is called Base class and other class which is inheriting that class (Base) is called derived class. It is done using colon (:) sign.

  
    using System;
 class BaseClass 
 {
     public void BaseClassMethod() 
     {
         Console.WriteLine("This is Base Class method.");
     }
  }
 class DerivedClass : BaseClass 
 {
     public void DerivedClassMethod () 
     {
         Console.WriteLine("This is Derived Class method.");
     }
 }
  
 class Program 
 {
     static void Main() 
     {
         DerivedClass d = new();
         d.BaseClassMethod ();   // Inherited method
         d.DerivedClassMethod ();  // Derived class method
     }
  }
  

What are the types of Inheritance?

There are following types of Inheritance in C#

  • Single Inheritance

  • Multiple Inheritance

  • Multilevel Inheritance

  • Hybrid Inheritance

  • Hierarchical Inheritance

ChatGPT Image Apr 20, 2026, 03_17_13 PM

Why are Multiple and Hybrid inheritance not supported in C#?

Due to Ambiguity problem in C#, Multiple and Hybrid inheritance is not supported through classes, but it is possible through interfaces. Ambiguity problem is related to Classes. As a class can’t have same method name with same signature. Same name method with same signature create confusion to compiler and compiler throws the error.

  
    public class InheritanceDemo 
{
     public void Test()  {  ….   } 
     public void Test()  {  ….   }  // compiler will throw error
}
  

Here Interface comes in the light. Interface provide the methods to a derived class for implementation, but not for consumption. Declaring and consuming method creates ambiguity problem not implementation.  

Can a derived class consume private members of a base class?

No, due to access level private members of base class can’t be inherited in derived class.

Why inheritance is not possible if base class don’t have public constructor?

Because when we create the derived class object, it first goes and calls the base class constructor. If no public constructor is present the compiler gets confused and throw the error.     

Can a base class access the members of a derived class?

No, base class can never access the members of derived class.

Can base class members be initialized by using a derived class instance?

Yes, it is possible. Because when a base class is initialized with derived class instance then it takes the reference from derived class. Now base class is act like derived class and can access base and derived class members.

  
    public class BaseClass 
{
     public void BaseMethod1() 
     {
           Console.WriteLine(“Base Class Method1”);
     }
}
  
public class DerivedClass : BaseClass 
{
      public void DerivedClassMethod1() 
      {
           Console.WriteLine(“Derived Class Method1”);
      }
}
  
static void Main() 
{
      //case 1
      BaseClass bc;
      bc.BaseMethod1(); // use of unassigned local variable bc //error
  
      //case 2
      BaseClass bc;
      DerivedClass dc = new();
      bc = dc;
      bc.BaseMethod1(); // this will work
  
      //case 3
      bc.DerivedClassMethod1 (); //this will not work due to derived method
}
   

What is Polymorphism?

Polymorphism means many forms. When a same entity can perform different operations in different scenarios then this process is known as polymorphism.

  
    public class Poly 
{
    public void Welcome() 
    {
         Console.WriteLine(“Hello”);
    }
    public void Welcome(string userName) 
    {
         Console.WriteLine(“Hello ” + userName);
    }
}
  

What are various types of Polymorphism in C#?

There are 2 types of Polymorphism in C#:

  • Early Binding (Compile Time): Behavior of method is decided at Compile time. C# compiler binds method call with method definition. Method Overloading and Operator Overloading are the example of Early binding. It is also known as Static Polymorphism.

  • Late Binding (Runtime): Behavior of method is decided at Runtime, when method is called. Method Overriding is the example of late binding. It is also known as Dynamic Polymorphism.