DEV Community

Wasim Malik
Wasim Malik

Posted on


C# Interview Notes



Constant are defined at compile time. They cannot be re-assigned.

const int pi=3.14; 


Read-only values can be assigned at runtime. Once assigned they cannot be re-assigned anywhere in code. To assign a Read-only value constructor is used.

readonly int LicenceLimit;
public Print( int PrintLimitForUser)
 LicenceLimit = PrintLimitForUser;

Static ReadOnly

It is same as read only however its value can be only assigned in static constructor.

Value Data Types

Data is directly stored inside variables. They are stored in stack memory of program.

//Eg int, float and char
int a=10;
int b=a;
b=15;// a will retain it own copy of data

Reference Data Types

Address is stored in variable.They are stored in heap.

Example Array and List


Boxing is when value type converted into Object type

int i=1;
object obj=i;

Unboxing is when Object type is converted back to value type

int k=(int) obj;


Array has a fixed size. All elements have a same datatype. Having its size fixed it does have a fast performance because memory is allocated once array is created. If data is not inserted in some of indexes it will still consume program memory. If you know size in advance always prefer array over collection.

int [] marks=new int[] {20,19,18,22};


Collection is dynamic. Element can have different data type. It size can be changed at runtime. It does have performance issue because of allocation of memory accordingly on runtime if consecutive slot is not available. It uses less memory because it is not reserving any memory locations which are empty. It performance is low then arrays.


Arraylist, List

String vs StringBuilder

String is immutable. It can not be change every assignment or concatenation operation create a new variable in memory. If this is done inside long loop it can slow down program performance significantly. Stringbuilder should be used in this case because it can be modified without creating new variables in memory.



Static keyword is used to make variable static. Static variables are class level variable they are same for all objects of class they share common data between all instances.


High Score in multiplayer game should be static so if anyone changes all player can see updated highscore.

  • Static Constructor is used to initialize static data of class.
  • Static class can not have not static members or methods
  • Nonstatic methods of normal classes can not access static members
  • Static classes are best for utility functions like logging services
  • Object of Static class can not be created you have to use class name followed by dot to access static members and functions


Over Loading

Multiple functions having same name but different arguments is called overload. It is also called static polymorphism. It can be done by different number of parameters, Different type of parameters and different order of parameters. It can not be done on Different return type. It will cause problem for compiler to pick correct method.

Function Parameter Types

Value type: They are normal parameters that can be passed by writing a value or assigning a simple variable

Reference Type: ref keyword is used before passing of variable

  • Ref variable must be declared and initialized before passing to function
  • Ref should be used when you want to send some value to function and then you want to get some value in return in same variable. If you don’t have to send any value then it is better to use out parameter type.
  • Ref variable must be initialized in calling method before method call

Out Type: out keyword is used before variable name. Variable must be initialized inside body of method

Optional Parameter: They always come at the end of parameter list. They are defined by assigned default values to them. If you provide value then that value is used otherwise default value is used. It is helpful in reducing number of overloaded functions

Named Parameters: They are helpful in increasing readability of code. In named parameters sequence of parameter does not matter. It is helpful if you want to call specific optional parameter.


Intermediate Language

All code in .Net language is converted to Intermediate Language. This Intermediate Language code actually provide machine independence. Intermediate Language is converted into Machine by .Net Framework

Garbage Collector

Garbage Collector is responsible for keeping record of variables and objects. If there is any variable that can not be access anymore due to it scope ended. Garbage collector will run periodically to claim that memory back so it can be used for others variables and objects in program.

Force Garbage Collector

GC.Collect(); is used to called garbage collector explicitly


Finalize is a method used to free up memory like files, database connections etc. It is run by Garbage collector and programmer have no control over its call.


Dispose method is similar to finalize it is also used to free up memory but this can be called by programmer manually. IDisposable interface should be implemented by class in order to use dispose function.

Managed Code

Managed code is a code that directly run under Common Language Runtime CLR. This code is not dependent on Machine configurations. It is code that is converted into Intermediate language and can run on multiple machine configurations. Its memory management is done by CLR.

Unmanaged Code

Unmanaged Code run directly by operating system. This is used to do low level system call. It run outside CLR so it is complete responsibility of developer to take care of memory management and memory leaks.

Common Language Runtime

It must be installed on machines in order to run .Net programs. It is responsible for memory management, Garbage collection, Type Checking, Exception Handling, and compilation of Intermediate Language Code to Machine code by using Just In Time Compiler JIT


Namespace is a considered as a container that help in avoiding name collision. It is used to separate group related files in container.It can be taught a folder for files.



Class describe a template for real world object also known as entities.It contains properties and methods.


Electronics is one class it will have properties like electronic name and color and it will have method like turnoff and turn on


Abstraction is feature supported by object oriented paradigm. Classes by design support abraction with help of private members and methods. Only required information in exposed to other classes. Internal details are not visible to other classes. Eg Torch contain on off button which abstract it inner working details.

Web api also provide abstraction.


Inheritance is feature supported in object oriented programming languages. Which is used for reusability of code. It is used to represent parent child relationship. In inheritance private members and methods are not inherited. C# do not support multiple inheritance due to diamond problem.Suppose there is Class A contains virtual method called Sum. Then class B and C inherit from A and overrides Sum method. Then there is Class D inherits from B and C both have sum overridden function. If D tries to call Sum function compiler will be confused in call of B or C method so this is why multiple inheritance is not supported in C#.


Fan and Calculator can inherit from electronics class so they will have basic properties and method in them due to inheritance they just have to provide their own properties and method and they are ready to be used in program.

Fan is a electronic it is used is a relationship


Instance of a one class “has a” reference of another class or another instance of same class.It is used to describe has a relationship. It supports lazy loading. It is also known as aggregation.

Eg: A “university” has several “departments”. Without existence of “university” there is no chance for the “departments” to exist. Hence “university” and “departments” are strongly associated and this strong association is known as composition.

Eg: A “department” has several “professors”. Without existence of “departments” there is good chance for the “professors” to exist. Hence “professors” and “department” are loosely associated and this loose association is known as Aggregation.

Access Modifiers

Private Only accessible within class Members(Methods and properties) are private by default

Public Accessible from everywhere

Protected Accessible within class and Derived class

Internal Available in same assembly Class Type is default internal

Protected Internal Accessible in Derived Classes which are in same assembly


Interface contains abstract methods only. All methods must be implemented by derived class. If we don’t know anything about implementation and only have requirement specification we can use Interface. Every method is public and abstract by default. Interface methods can not be private or protected. Every variable is public static and readonly by default. Every interface variable must be initialized. Interface can not declare constructors

Abstract Class

Abstract Class contains one or more abstract methods. All abstract methods have to be implemented by derived class. If we know partial implementation of class we can use abstract class. It can also have concrete methods.Abstract class can have a constructor.

Abstract methods

Abstract methods can not be declared sealed because they are made for overriding. Abstract methods can not be declared static because object of interface and abstract class can never be created directly and static methods can be access without creating object. If is was allowed static then if user tries to access method there is no body of method declared.


Sealed Keyword is used to stop inheritance. Methods can be sealed to avoid further overriding.


Involves creation of method with same signature in child class. Method must be declared virtual in parent class. Override keyword is used in child class to do overriding. Pure virtual is C++ terminology which is equivalent to abstract in C#

Partial Classes

Partial Classes allow us to split definition of class to more than one files. It is helpful when you are working in a team. Two or more member have to work on same class. If two or more partial classes have method with exact same name in two different files. It will be a compile time error.



Delegate is a pointer to function. Delegates allows methods to be passed as a parameters. Multicast delegate is a pointer to multiple functions.

Volatile Keyword

Volatile Keyword indicates that field might be modified by multiple threads at a same time.

Anonymous Type

Anonymous Type allows user to create new type without defining them. Select keyword in in LINQ create anonymous type object so that all properties can be viewed even though it may not be defined in any class.

var Student=new {Name=Wasim.Age=30};


Generics allow you to delay the specification of the data type of programming elements in a class or a method, until it is actually used in the program

T sum<T>(T FirstNumber,T SecondNumber)
return FirstNumber+SecondNumber;

Top comments (0)