This C# Tutorial is focused on C# Interview Questions with practical and detailed answers. We already have published detailed Technical Tutorials on ASP.NET, ASP.NET MVC, Entity Framework, ASP.NET Web API and other related Interview Questions tutorial series to clarify all related concepts. Also, one can find Online Practice tests and exams on C# and related. We are focused to provide more practical details with real time scenarios and complete source code in order for user to grasp C# language concepts.
Checkout Best 100+ Free Online Programming Courses including C#, Python, C++, Java from top universities like MIT, Harvard, Princeton, Michigan and more.
Following Technical Interview Questions and Answers will also be helpful.
- MUST Have C# Interview Questions and Answers - Part2
- ASP.NET Interview Questions Part-1 [Basics, Controls, State Management]
- ASP.NET Interview Questions Part-2 [Advanced Controls, Globalization and Localization]
- ASP.NET Interview Questions Part-3 [Caching & Security etc.]
- ASP.NET Interview Questions Part-4 [Advance Security]
- ASP.NET Interview Questions Part-5 [ASP.NET AJAX]
- ASP.NET Interview Questions Part-6 [ASP.NET Web API]
- ASP.NET MVC Interview Questions
- Entity Framework Interview Questions
Complete C# Interview Questions List
- Whats new in C# version 8?
- Explain an Interface and a Class in C#?
- What are the Value Types and Reference Types in C#?
- What is a Sealed Class?
- Explain Method Overloading in C# with the help of an example?
- Briefly explain Access Modifiers in C#?
- What is Static Keyword in c#?
- What is Serialization and De-Serialization? Explain Serializable Attribute in C#?
- What is a Jagged Array in C#?
- What are Nullable Types?
- What is a circular reference?
- What is an Object Pool?
- What is a Multicast Delegate? Explain with Example.
- What are Extension Methods in C#?
Whats new in C# version 8?
Read-only members:
You can put on the readonly modifier to members of a struct. It shows that the member doesn’t modify state. It’s more granulated than applying the readonly modifier to a struct declaration.
Default interface methods:
You can now add associates to interfaces and provide an operation for those members. This language feature allows API authors to add methods to an interface in later versions without breaking source or binary compatibility with current implementations of that interface. Existing implementations inherit the default implementation. This feature also enables C# to inter operate with APIs that target Android or Swift, which support similar features. Defaulting interface methods also enable scenarios similar to a “traits” language feature.
Using declarations:
A using declaration is a variable declaration headed by the using keyword. It tells the compiler that the variable being declared should be liable at the end of the enclosing scope.
Static local functions:
You can now add the static modifier to local functions to guarantee that local function doesn’t capture (reference) any variables from the enclosing scope. Doing so creates CS8421, “A static local function can’t contain a reference to <variable>.
Here is some more new features and enhancements
- Disposable ref structs
- Null-able reference types
- Indices and ranges
- Un-managed constructed types
- Asynchronous Streams
Explain an Interface and a Class in C#?
Class in C#:
A C# class is a group of methods and variables enclosed by pair of curly braces. Class is also defined through properties and methods used in OOPS. In other words, we can say it acts as a template for creating objects.
Following is a Class (i.e. Employee) written in C#:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using System; public class Employee { //Fields or data members public int id; public String name; public float salary; //Parameterised Constructor public Employee(int i, String n,float s) { id = i; name = n; salary = s; } //Method public void display() { Console.WriteLine(id + " " + name + " " + salary); } } class TestEmployee { public static void Main(string[] args) { Employee e1 = new Employee(1, "Ahmad", 50000f); Employee e2 = new Employee(102, "Hamza", 40000f); e1.display(); e2.display(); } } |
Interface in C#:
In C# Interface look like the class but it only contains method’s signature instead of actual implementations. Unlike the class, Interface cannot contain method body because all the method inside the interface are declared abstract.
Interface is mainly used for achieving the multiple inheritances that cannot be possible by using class. It is also used for achieving fully abstraction.
It is compulsory to define interface method in inherited class with public keyword and as we know interface is not class then it’s impossible to create instance of interface.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
using System; public interface Drawable { Void drawRectangle(); Void drawCircle(); } public class Rectangle : Drawable { // implementation of drawRactangle method public void drawRectangle() { Console.WriteLine("drawing rectangle shape..."); } } public class Circle : Drawable { // implementation of drawRactangle method public void drawCircle() { Console.WriteLine("drawing circle shape..."); } } public class TestInterface { public static void Main() { Drawable d; d = new Rectangle(); d.drawRectangle(); d = new Circle(); d.drawCircle(); } } |
C# Program Output:
|
1 2 |
drawing rectangle shape... drawing circle shape... |
What are the Value Types and Reference Types in C#?
In .NET, data types are define in two ways.
- Value Type
- Reference Type
Value Data Type:
The value data type has its own default value and a storage range for each member. Just like int has default value 0 and float has default value 0.0f.
| Predefined Data Types | User Defined Data Types |
| int, char, float, Boolean etc | Structure, Enumeration |
Reference Data Type:
The reference data type store only a reference of a memory location that contain actual data. It cannot contain the actual data stored in variable, so we can say that a reference behaves like a key to referenced data.
| Predefined Data Types | User defined Data Types |
| Object, String | Class, Interface, Array & Delegates |
What is a Sealed Class?
Sealed is a keyword which used to apply the restriction on the class and method.
- If we use sealed on the class it cannot be inherited or derived.
- If we use sealed on the method it cannot be overridden.
Sealed Class Example:
Consider the below example, where Bike is a sealed class. If we try to override any of it’s method, it will give a compile time error.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
using System; //Here Bike is a sealed class sealed class Bike { public void run() { Console.WriteLine("Running..."); } } public class Honda : Bike { public void run() { Console.WriteLine("Honda is running safely..."); } } public class SealedExample { public static void Main() { Honda obj= new Honda(); obj.run(); obj.run(); } } |
Note: sealed keyword applied on Bike class hence it cannot be inherited by child class or derived class.
Explain Method Overloading in C# with the help of an example?
Method overloading means using two methods with the same name in a single class. In C# method overloading also known as Compile Time Polymorphism that means we overload during compile time.
In C# there is three way to overload a method
- Change the signature of method
- Change the data types in parameterized method
- Change numbers or the data type in parameterized method.
Method Overloading Example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
using System; public class moverload { //class method1 public void mexp() { Console.WriteLine("Hello This is method overloading"); } //overloaded method with int Parameter public void mexp(int x) { Console.WriteLine("Hello This is method overloading first way"); } //overloaded method with string Parameter public void mexp(string x) { Console.WriteLine("Hello This is method overloading first way 1.1"); } // This is 2nd way public void mexp(string x,string y) { Console.WriteLine("Hello This is method overloading 2nd way"); } // This is third way public void mexp(string x,int x) { Console.WriteLine("Hello This is method overloading third way"); } } |
Briefly explain Access Modifiers in C#?
Access modifiers are keywords which used to specify the protection or security level of the class or access of a class.
There are 5 access modifiers in C#.
- public
- private
- protected
- internal
- protected internal
public Access Modifier:
It is the most common access modifier of all; it can be access anywhere within the class or outside of the class and within the assembly or outside the assembly.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Class A { // public mehtod public void methodA() { //Definition } } Class B { static void main(string[] args) { A a = new A(); a.methodA(); //accessing method from outside class } } |
private Access Modifier:
It allows restricting the use of the method and data member only within the class.
- That’s mean if we declare any method or field as a private it can be accessible only within the class.
- If we declare any constructor as private then you cannot create an object of the class outside of that class, its mean main method must be inside the same class in which constructor declared as private.
- If we declare any class as private, it is mandatory to declare the main method in the same class to create the object of that class.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class A { private int a; //private data member private A() //private constructor { //Initialisation of data member } private void method() //private method { // definition } } Class B { Static void main(String[] args) //main method outside of the class { A obj = new A(); obj.methodA(); Console.Write(obj.a); } } |
Solution to above Issue:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class A { private int a; private A() { //Initialisation of data member } private void method() { // definition …… } //main method inside the class Static void main(String[] args) { A obj=new A(); obj.methodA(); Console.Write(obj.a); } } |
protected Access Modifier:
Protected modifier allows to accessing data member and method within the same class as well as outside of its derived class within the same assembly. That’s mean it can be accessible outside of the class only using inheritance.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class A { protected int a; //protected data member protected private A() //protected constructor { //Initialisation of data member } protected void method() //protected method { // definition } } Class B:A //class B is the sub-class of class A { static void main(String[] args) //main method outside of the class { A obj=new A(); obj.methodA(); Console.Write(obj.a); } } |
internal Access Modifier:
It allows accessing method and data member within the class as well as outside of the class even without using inheritance that’s mean no matter that another class is derived or not. The only thing that matters, is that classes must lie within the same assembly.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class A { internal int data; internal void methodA() { // definition } } class B { Static void main(String[] args) { A a=new A(); a.methodA(); Console.WriteLine(a.data); } } |
protected internal Access Modifier:
It allows accessing the data member and method within the class and outside of the class. Here assembly doesn’t matter, it can be accessed outside the assembly also but only inside the derived class. That’s mean another assembly should contains derived class of that class.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class A { string name; protected internal void methodA() { Console.WriteLine(“My name is ” + name); } } class B { static void main(String[] args) { A a=new A(); a.name = "Ahmad"; a.methodA(); } } |
What is Static Keyword in C#?
Static is keyword or modifier that can be used with data members, methods and also with class. It is mainly used for memory management. It will not get memory each time when the instance is created; we don’t need to create the instance of the class for accessing the static member of the class because it belongs to the class area rather than an instance of the class.
static as field:
- If you declare the static keyword with the data members, it is called static data member or static field.
- Unlike the instance data member, it will not get memory each time when the instance of the class is created.
- There is only one copy of it is created in memory which is used to share the common property to all the objects.
- It can be changed only inside the static method.
static as Method:
If we declare any method as a static then,
- It belongs to the class rather than instance of the class
- It can access only static data members.
- It can call only other static member and cannot call a non-static method.
- It can access directly by the class name, don’t need to create an object.
- It cannot refer to this and super keyword.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
class Employee { int id; String name; static string company = "SofTech Solution"; //static data member static void change() // static method where we can change the value of data member { company = "WebDevTutorials"; } Employee(int r, String n) { id = r; name = n; } void display () { System.out.println(id + " "+ name + " " + company); } static void main(String args[]) { Employee.change(); Empoyee e1 = new Employee (1,"Ahmad"); Empoyee e2 = new Employee (2,"Hamza"); s1.display(); s2.display(); } } |
|
1 2 |
1 Ahmad WebDevTutorials 1 Hamza WebDevTutorials |
static with Class:
When a class declared as a static then,
- It can contain only static members and it cannot be instantiated.
- It is sealed.
- Cannot contain instance constructor.
- Provides us guarantee that instance cannot be created of static class.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
using System; public static class Calculate //static class { public static float PI=3.14f; //static data member public static int cube(int n) // static method { return n*n*n; } } class CalculateMain { public static void Main(string[] args) { Console.WriteLine("Value of PI is: "+Calculate.PI); Console.WriteLine("Cube of 5 is: " + Calculate.cube(5)); //access directly by class name } } |
|
1 2 |
Value of PI is: 3.14 Cube of 5 is: 125 |
What is Serialization and De-Serialization? Explain Serializable Attribute in C#?
Serialization is a mechanism of converting the object into byte stream so that it can be saved to memory, file or database and it’s reverse mechanisms called de-serialization.
SerializableAttribute:
To serialized an object necessary to use SerializableAttribute attribute to type.
Serialization Exception is thrown at the runtime if we don’t apply the SerializableAttribute.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary; [Serializable] // SerializableAttribute class Employee { int id; string name; public Employee(int id, string name) { this.id = id; this.name = name; } } public class SerializeExample { public static void Main(string[] args) { FileStream stream = new FileStream("d:\\note.txt", FileMode.OpenOrCreate); BinaryFormatter formatter=new BinaryFormatter(); Employee s = new Employee(1, "Ahmad"); // serialized the object of class here formatter.Serialize(stream, s); stream.Close(); } } |
Now the serialized data will stored in the file. If we want to get data (or file), we have to perform deserialization.
Note: here, we are using BinaryFormatter.Serialize(stream, reference) method to serialize the object.
Deserialization:
Deserialization is the reverse process of serialization that’s mean it convert the byte stream data into the object. Now we will use Binary.Formatter.Deserialize(Stream).
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary; [Serializable] class Employee { public int id; public string name; public Employee(int id, string name) { this.id = id; this.name = name; } } public class DeserializeExample { public static void Main(string[] args) { FileStream stream = new FileStream("d:\\note.txt", FileMode.OpenOrCreate); BinaryFormatter formatter=new BinaryFormatter(); Employee e=(Employee)formatter.Deserialize(stream); Console.WriteLine("Employee Id: " + e.id); Console.WriteLine("Employee Name: " + e.name); stream.Close(); } } |
Output: 1 Ahmad
What is a Jagged Array in C#?
A jagged array is an array whose elements are itself arrays. That means jagged is an array of array. Also, the elements of an array can be different in size and dimension.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
using System; namespace jagged array { class JArrayProgram { static void Main(string[] args) { //Declare jagged array of 3 elements(rows) int[][] jgArray = new int[3][]; //Initialized the elements jgArray [0] = new int[2] { 1, 3 }; jgArray [1] = new int[4] { 5, 6, 8, 12 }; jgArray [2] = new int[6] { 2, 5, 7, 8, 10, 11 }; jgArray [3] = new int[3] { 5, 4, 7 }; //retrieving element to display for (int i = 0; i < jgArray.Length; i++) { System.Console.Write("Element({0}): " , i + 1); for (int j = 0; j < jgArray[i].Length; j++) { System.Console.Write(jgArray[i][j] + "\t"); } System.Console.WriteLine(); } Console.ReadLine(); } } } |
Output:
|
1 2 3 4 |
Element(1): 1 3 Element(2): 5 6 8 12 Element(3) :2 5 7 8 10 11 Element(4): 5 4 7 |
Note: Jagged array can contain the elements in the form of single & multidimensional array. Below example is a single dimension jagged array contains multidimensional array’s elements.
|
1 2 3 4 5 6 |
int[][,] jgArray = new int[3][,] { new int[,] { {1,2}, {3,4} }, new int[,] { {5,6}, {7,8}, {9,10} }, new int[,] { {11,12}, {13,14} }, }; |
What are Nullable Types?
Nullable is a special type of variable provide by c#, in which you can store the normal range values as well as null value.
- You can store any numeric data type in range from -2,147,483,648 to 2,147,483,647
- You can store null value <data type>?<variable name>=null;
- You can store Boolean data bool? Value=new bool?();
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; namespace Application1; { class NullableExample { static void Main(string[] args) { int? a=null; int? b=217; double? c= new double?(); double? d=5.14 bool? e=new bool?(); Console.WriteLine("Numeric values are: {0},{1},{2},{4}",a,b,c,d); Console.WriteLine("Boolean vaule: {0}",e); } } } |
What is a circular reference?
A circular reference is a series of references when a formula refers back to its own cell, either directly or indirectly and last object references the first, resulting in a closed loop. Also it is run around where in two resources independent on each other.
If a group of objects contain references to each other, but none of these objects are referenced directly or indirectly from stack or shared variables, then garbage collection will automatically reclaim the memory.
The methods to deal with circular references are:
- Weighted references counting
- Indirect reference counting
There are some ways to handle the problem of detecting and collecting circular references with the help of garbage collection.
- The system may explicitly forbid reference cycles.
- Systems ignore cycles if it have small amount of garbage collector cycles.
- You can also periodically use a tracing garbage collector cycles.
Weighted References Counting:
- An object has a weight
- Assign weight to each pointer
- Object weight=sum of the pointer’s weight
- When the pointer is copied, evenly distribute the weight
- When a pointer is deleted, the weight of the pointer is subtracted from the weight of the object
- An object is garbage collected when its weight becomes to 0.
- Learn ASP NET MVC 5 step by step [Maruti Makwana, Corporate Trainer]
28 Lectures, 2.5 Hours Video, Intermediate Level
Very easy to learn video series on Asp.Net MVC 5 Specially for those who are familiar with Asp.Net Web forms. - AngularJS for ASP.NET MVC Developers [Brett Romero]
10 Lectures, 1 hour video, Intermediate Level
The Fastest Way For .NET Developers To Add AngularJS To Their Resume - ASP.NET with Entity Framework from Scratch [Manzoor Ahmad, MCPD | MCT]
77 Lectures, 10 hour video, All Level
Latest approach of web application development - Comprehensive ASP.NET MVC [3D BUZZ]
34 lectures, 14 Hours Video, All Levels
From zero knowledge of ASP.NET to deploying a complete project to production.
Continue with More C# Interview Questions here:
What is an Object Pool?
An object pool is container object that contain the list of other objects which are ready to be used.
- It contain the record of currently used object
- It contain the number of objects in the pool
- It reduces for creating and recreating object each time by re-using objects from the pool.
- Each time it will check in the pool (Queue) either the object is available or not, it reuses the object if available in the pool otherwise will create the new object.
What is a Multicast Delegate? Explain with Example.
Delegate is a reference to the method that is work like function pointer in C & C++ but it is objected, oriented, secured and type safe than function pointer.
A multicast delegate is a extension of normal delegates and use to call the more than one method at a single time using delegate object.
- It encapsulates method only for the static method and for the instance method, it encapsulates both the method as well as instance.
- It is more helpful to use in event.
- It resolve the problem of heavy coupling at time of adding of new method in business layer.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
public partial class Application1 : App1 { //delegate method public delegate void MyDelegate(); private void methodOne() { MessageBox.Show(“MethodOne is Invoked”); } private void methodTwo() { MessageBox.Show(“MethodTwo is Invoked”); } private button_Click(objec sender,EvantArgs e) { //creates delegate reference MyDelegate md=null; md+=this.methodOne; //invoke the both method using reference md+=this.methoTwo; md.Invoke(); } public App1() { InitializeComponent(); } } |
What are Extension Methods in C#?
An extension method is a special type of method that allows you to add method of existing types without creating a new derived type, recompiling or otherwise modifying the original type. They are called if they were instance method on the extended type.
It is the static method of static class, where the modifier is applied on the first parameter and the type of first parameter will be the type that is extended.
Way of using extension method
- Extension method cannot be used to override the existing method
- The concept of extension methods cannot be applied on the field, properties or event.
- It must be define in a top level static class.
- It shout not be same name & signature like instance method.
Back to top
This C# Tutorial Series is continued and you can find more C# Interview Questions in Next Web Development Tutorial here.
Top Technical Interview Questions and Answers Series:
- Top 20 AngularJS Interview Questions
- Advanced Angular2 Interview Questions
- Top 15 Bootstrap Interview Questions
- ReactJS Interview Questions and Answers
- Top 10 HTML5 Interview Questions
- Must Have NodeJS Interview Questions
- BackboneJS Interview Questions
- Top 10 WCF Interview Questions
- Comprehensive Series of WCF Interview Questions
- Java Spring MVC Interview Questions





