Home » C# Interview Questions and Answers

C# Interview Questions and Answers

In this article we have gathered the latest and most asked C# Interview Questions and Answers for both freshers and 2/3/5/10 years experience candidates. These C# (Sharp) Interview Questions will get you familiar with the types of questions that you will likely come across during your interview.

C# (C-Sharp) is one of the most popular programming languages. It is a high level language which is user friendly to learn. Developed by Microsoft in 2000, it is used to create mobile applications, Windows desktop applications, games, and more. C# is an object-oriented, type-safe and managed language compiled by the .Net Framework to generate the Microsoft Intermediate Language.

C# Interview Questions and Answer

Here are the top commonly asked C# interview questions and answers for both freshers and experienced candidates. These C# Interview Questions will help job seekers to prepare well for the job interview and cross the panel discussion easily.

So let’s get started :

1. What is C#?
2. What are the features of C#?
3. What is the difference between public, static, and void?
4. What are the different types of constructors in C#?
5. What is method overloading in C#?
6. What are the differences between a struct and a class in C#?
7. What is array?
8. What is Jagged Arrays?
9. What is the lock statement in C#?
10. What is method overloading?
11. What is garbage collection in C#?
12. What is the difference between dispose() and finalize() methods in C#?
13. Explain Reflection in C#.
14. Write down the C# syntax to catch an exception.
15. What is object pool in .Net?
16. What are generics in C#.NET?
17. Name the access modifiers and accessibility levels in C#.
18. How do you inherit a class into other class in C#?
19. What is Hashtable?
20. How we can create an array with non-default values?
21. What is difference between IS and AS operators in c#?
22. How to implement a singleton design pattern in C#?
23. What are C# attributes and its significance?

You May Also Like :   DevOps Interview Questions and Answers

1. What is C#?

C# is a simple, modern, general-purpose programming language. It is an object oriented programming language developed by Microsoft. It is a secure and managed language that is compiled by the .NET Framework to generate Microsoft Intermediate Language (Machine Code).


2. What are the features of C#?

Following are some of the key features of C# :

  • Simple language with a structured approach
  • Supports language interoperability
  • Type-safe
  • Object-oriented programming language
  • Supports many modem features, such as error handling features and modern debugging features
  • Scalable
  • Fast compilation and execution speed
  • Has a rich library
  • Component Oriented

3. What is the difference between public, static, and void?

Variables or methods declared as Public are accessible anywhere in the application. Variables or methods declared Static are globally accessible without creating an instance of the class. And Void is a type modifier which tells that the method or variable does not return any value.


4. What are the different types of constructors in C#?

Basically, there are five types of constructors:

  • Static constructor
  • Private constructor
  • Copy constructor
  • Default constructor
  • Parameterized constructor

5. What is method overloading in C#?

Method overloading is the mechanism of creating multiple methods in the same class with the same name and unique signature. When you go to compile, the compiler uses overload resolution to determine the specific method to invoke.


6. What are the differences between a struct and a class in C#?

Following are the differences between Structure and Class in C#:

StructClass
It is a value type that inherits from System.Value TypeClass is a reference type that inherits from the System.Object Type
A struct can create an instance, with or without a new keyword.It uses a new keyword for creating instances
Each variable contains its own copy of the dataTwo variables can contain the reference of the same object
Struct can’t be inherited from other typesIt can be inherited from other classes
Used for smaller dataUsed for large data

7. What is array?

Array is a set of related instances either value or reference types.

You May Also Like :   Java Interview Questions and Answers

There are three types of array supported by C#:

  • Single Dimensional Array: It contains a single row. It is also known as vector array.
  • Multi Dimensional Array: It is rectangular and contains rows and columns.
  • Jagged Array: It also contains rows and columns but it has an irregular shape.

8. What is Jagged Arrays?

An array which contains elements of array type is called jagged array. Elements can be of different dimensions and sizes. We can also call jagged array as an array of arrays.


9. What is the lock statement in C#?

Lock statements are used to ensure that one thread does not enter a critical section of code while another thread is in the critical section. If another thread tries to enter the locked code it will wait or block until the object is released.


10. What is method overloading?

Method overloading is creating multiple methods with the same name with unique signature in the same class. When we compile, the compiler uses overload resolution to determine the specific method to be invoked.


11. What is garbage collection in C#?

Garbage collection is an automatic memory manager. It is the process of managing the allocation and deallocation of memory. It manages the allocation and release of memory. Garbage collection retrieves objects that are no longer used, cleans up their memory and keeps the memory available for future allocations.


12. What is the difference between dispose() and finalize() methods in C#?

Dispose() method is called explicitly to free the resources unmanaged by the user like files, database connections etc. whereas finalize() method is implicitly called by the garbage collector to free unmanaged resources like file, database connection etc.

Dispose() method belongs to IDisposable interface whereas finalize() method belongs to Object class.


13. Explain Reflection in C#.

Reflection allows us to dynamically inspect the metadata of types in our program. This enables us to obtain information about loaded assemblies and the types defined in them at runtime. To add reflection to the .NET Framework and retrieve the type, use the System.Reflection namespace in your program.

You May Also Like :   Desktop Support Interview Questions and Answers

14. Write down the C# syntax to catch an exception.

To catch an exception, we use a try-catch block. The catch block can have a single parameter of System.Exception Type. For example:

try {
    GetAllData();
} 
catch (Exception ex) {
}

15. What is object pool in .Net?

An object pool is a container for ready to use objects. This reduces the overhead of creating a new object.


16. What are generics in C#.NET?

Generics are used to create reusable code classes to reduce code redundancy, increase type safety and performance. By using generics we can create collection class. To create generic collections, the System.Collections.Generic namespace should be used instead of classes such as ArrayList in the System.Collections namespace. Generics promote the use of parameterized types.


17. Name the access modifiers and accessibility levels in C#.

In C#, there are four access modifiers namely public, private, protected and internal. Accessibility levels include:

  • public
  • private
  • protected
  • internal
  • private protected
  • protected internal

18. How do you inherit a class into other class in C#?

Colon is used as inheritance operator in C#. Just put a colon and then the class name.

public class DerivedClass : BaseClass

19. What is Hashtable?

A hashtable is a collection of key/value pairs. It contains values based on keys.


20. How we can create an array with non-default values?

We can create an array with non-default values using Enumerable.Repeat.


21. What is difference between IS and AS operators in c#?

The IS operator is used to check the compatibility of an object with a given type and returns the result as a boolean.
The AS operator is used to cast an object to a type or class.


22. How to implement a singleton design pattern in C#?

In Singleton pattern, there can be only one instance of a class and provides a single access point to it globally. For example:

Public sealed class Singleton
{
Private static readonly Singleton _instance = new Singleton();
}

23. What are C# attributes and its significance?

C# provides a way for developers to define declarative tags on certain entities, eg. Class, method etc. are called qualities. Attribute information can be retrieved at runtime using reflection.


We hope these C# Interview Questions help you in cracking your next job Interview easily.

All The Best!

4.8/5 - (5 votes)
Scroll to Top