CSharpInterviewQuestionsandAnswersUpdatedJan2025
CSharpInterviewQuestionsandAnswersUpdatedJan2025
Contents
Question 1 :- Explain difference between .NET and C# ? ......................................................................... 5
Question 2 :- .NET Framework vs .NET Core vs .NET 5.0 .......................................................................... 5
Question 3 :- What is IL ( Intermediate Language) Code ? ....................................................................... 5
Question 4 :- What is the use of JIT ( Just in time compiler) ?.................................................................. 5
Question 5 :- Is it possible to view IL code ? ............................................................................................. 5
Question 6 :- What is the benefit of compiling in to IL code ? ................................................................. 5
Question 7 :- Does .NET support multiple programming languages ?...................................................... 5
Question 8 :- What is CLR ( Common Language Runtime) ? ..................................................................... 5
Question 9 :- What is managed and unmanaged code ? .......................................................................... 6
Question 10 :- Explain the importance of Garbage collector ?................................................................. 6
Question 11 :- Can garbage collector claim unmanaged objects ?........................................................... 6
Question 12 :- What is the importance of CTS(Common Types System) ? ............................................... 6
Question 13 :- Explain CLS ? ...................................................................................................................... 6
Question 14 :- Difference between Stack vs Heap ? ................................................................................. 6
Question 15 :- What are Value types & Reference types? ....................................................................... 7
Question 16 :- Explain boxing and unboxing ? .......................................................................................... 7
Question 17 :- What is consequence of boxing and unboxing ?............................................................... 7
Question 18 :- Explain casting, implicit casting and explicit casting ? ...................................................... 7
Question 19 :- What can happen during explicit casting ? ....................................................................... 7
Question 20 :- Differentiate between Array and ArrayList ? .................................................................... 7
Question 21 :- Whose performance is better array or arraylist ?............................................................. 7
Question 22 :- What are generic collections ? .......................................................................................... 8
Question 23 :- What are threads (Multithreading)? ................................................................................. 8
Question 24 :- How are threads different from TPL(Task Parallel Library) ? ............................................ 8
Question 25 :- How do we handle exceptions in C#(try/catch)? .............................................................. 8
Question 26 :- What is the need of finally? .............................................................................................. 8
Question 27 :- Why do we need the out keyword ? ................................................................................. 8
Question 28 :- What is the need of Delegates ? ....................................................................................... 8
Question 29 :- What are events ? ............................................................................................................. 9
Question 30 :- Whats the difference between Abstract class and interface ? ......................................... 9
Question 31 - What is a Delegate and how to create a Delegate? ........................................................... 9
Question 32 - Where have you used Delegates? ...................................................................................... 9
Question 33 - What is a Multicast Delegates? .......................................................................................... 9
Question 34 - What is an Event?............................................................................................................... 9
Question 35 - How to Create an Event?.................................................................................................. 10
Question 36 - Delegate VS Events........................................................................................................... 10
Question 37 :- Why do we need Object Oriented Programming (OOP) ? .............................................. 10
Question 38 :- What are the important pillars of OOPs ? ....................................................................... 10
Question 39 :- What is a class and object ? ............................................................................................ 10
Question 40 :- Abstraction vs Encapsulation? ............................................................................................ 10
Question 41 :- Explain Inheritance ? ........................................................................................................... 10
Question 42 :- Explain virtual keyword ? .................................................................................................... 11
Question 43 :- What is overriding ? ............................................................................................................ 11
Question 44 :- Explain overloading ? .......................................................................................................... 11
Question 45 :- Overloading vs Overriding ? ................................................................................................ 11
Question 44 :- What is polymorphism ? ..................................................................................................... 11
Question 45 :- Can polymorphism work with out inheritance ?................................................................. 11
Question 46 :- Explain static vs dynamic polymorphism ? ......................................................................... 11
Question 47 :- Explain operator overloading ? ........................................................................................... 11
Question 48 :- Why do we need Abstract classes ? .................................................................................... 11
Question 49 :- Are Abstract methods virtual ? ........................................................................................... 12
Question 50 :- Can we create a instance of Abstract classes ? ................................................................... 12
Question 51 :- Is it compulsory to implement Abstract methods ? ............................................................ 12
Question 52 :- Why can’t simple base class replace Abstract class ? ......................................................... 12
Question 53 :- Explain interfaces and why do we need it ? ........................................................................ 12
Question 54 :- Can we write logic in interface ? ......................................................................................... 12
Question 55 :- Can we define methods as private in interface ?................................................................ 12
Question 56 :- If I want to change interface what's the best practice ? ..................................................... 12
Question 57 :- Explain Multiple inheritance in Interface ? ......................................................................... 12
Question 58 :- Explain Interface Segregation principle ? ............................................................................ 12
Question 59 :- Can we create instance of interface ? ................................................................................. 13
Question 60 :- Can we do Multiple inheritance with Abstract classes ? .................................................... 13
Question 61:- Abstract Class vs Interface ................................................................................................... 13
Question 62 :- Why do we need constructors ? ......................................................................................... 13
Question 63 :- In parent child which constructor fires first ? ..................................................................... 13
Question 64 :- How are initializers executed ? ........................................................................................... 13
Question 65 :- How are static constructors executed in Parent child ? ..................................................... 13
Question 66 :- When does static constructor fires ? .................................................................................. 13
Question 127: - Explain Garbage collector (GC)?........................................................................................ 14
Question 128:- How does Garbage collector know when to clean the objects ? ....................................... 14
Question 149:- Explain weak and strong references ? ............................................................................... 17
Question 150 :- When will you use weak references ?............................................................................... 17
Question 151:- What are design patterns?................................................................................................. 17
Question 152 :- Which are the different types of design patterns? ........................................................... 18
Question 153 :- Explain structural , Behavioral and Creational design pattern ?....................................... 18
Question 154 :- Which design pattern have you used in your project? ..................................................... 19
Question 154 :- Explain Singleton Pattern and the use of the same? ........................................................ 20
Question 155 :- How did you implement singleton pattern? ..................................................................... 20
Question 156 :- Can we use Static class rather than using a private constructor? .................................... 21
Question 157:- Static vs Singleton pattern? .............................................................................................. 21
Question 158:- How did you implement thread safety in Singleton? ........................................................ 21
Question 159:- What is double null check in Singleton? ............................................................................ 22
Question 160:-Can Singleton pattern code be made easy with Lazy keyword?......................................... 22
Question 161:-Can we rid of this double null check code? ........................................................................ 23
Question 162:-What is the use of repository pattern? ............................................................................... 23
Question 163:-Is Dal (Data access Layer) and Repository same? ............................................................... 24
Question 164:-What is Generic repository pattern ? ................................................................................. 24
Question 165:-Is abstraction the only benefit of Repository?.................................................................... 24
Question 166:-How to implement transaction in repository?.................................................................... 25
Question 167:-What is Unit of work design pattern? ................................................................................. 25
Question 168:-Do we need repository pattern as EF does almost the same work? .................................. 25
Question 169:-Did you do unit testing with Repository ? .......................................................................... 27
Question 170:-How does repository pattern make unit testing easy?....................................................... 27
Question 171:-How can we do mock testing with Repository?.................................................................. 27
Question 172 :- What is Factory pattern and how does it benefit? .......................................................... 28
Question 173 :- How does centralizing object creation helps in loose coupling ? ..................................... 29
Question 174 :- What is IOC and DI ?.......................................................................................................... 29
Question 175 :- DI vs IOC ? ......................................................................................................................... 29
Question 176 :- What is a service locator ? ............................................................................................... 29
Question 177:- Service Locator vs DI ? ..................................................................................................... 30
Question 178 :- Which is good to use Service Locator or DI ? .................................................................. 30
Question 179 :- Can not we use a simple class rather than interface for DI ? ........................................... 30
Question 180 :- Is DI a Factory Pattern? ..................................................................................................... 30
Question 181 :- So If you just centralize object creation is it Factory pattern? ......................................... 30
Question 182 :- Static DI and Dynamic DI ? ............................................................................................... 30
Question 183 :- In which scenarios to use Static DI vs Dynamic DI ?......................................................... 31
Question 184 :- The real Factory pattern ? ................................................................................................ 31
Question 185 :- Factory Method vs Factory pattern ? ............................................................................... 31
Question 186 :- How are new behaviors created in Factory pattern ?...................................................... 31
Question 187 :- What is Abstract Factory Pattern ? .................................................................................. 32
Question 188 :- Does Abstract Factory Pattern use FP inside ?................................................................. 32
Question 189 :- Simple Factory vs Factory (Factory Method) vs Abstract Factory ? ................................. 33
Question 190 :- How to remove IF conditions from Simple Factory?........................................................ 33
Question 1 :- Explain difference between .NET and C# ?
Yes .NET support multiple programming languages like C#, F#, C++, Visual Basic etc.
➢ Code that executes under CLR execution environment is called managed code.
➢ Unmanaged Code executes outside the CLR boundary. Unmanaged code is
nothing but written in C++, VB6, VC++ etc.
Unmanaged code have their own environment in which the code runs and its
completely outside the control of CLR.
➢ CTS ensures that data types defined in two different languages get compiled to a
common data type.
➢ Stack and heap are memory types in an application. Stack memory stores data
types like int, double, Boolean etc. While heap stores data types like string and
objects.
Question 15 :- What are Value types & Reference types?
➢ Value types contain actual data while reference types are contain pointers and
pointers point to the actual data.
➢ Value types are stored on stack while reference types are stored on heap. Value
types are your normal data types like int, bool, double and reference types are all
objects.
Generic collection is strongly typed and flexible. It has better performance as compared
to ArrayList.
Strongly typed in the sense we have to insert the list of data of same datatype and
flexible in the sense we have the flebility of array size as of ArrayList.
➢ The code where it has chances of occuring exception is kept inside try block and if
the exception occur then that is thrown to the catch block where we can decide
what to do with that exception.
Question 26 :- What is the need of finally?
➢ Finally block runs even if you have an exception or if you do not have an
exception.
Question 27 :- Why do we need the out keyword ?
➢ If you want to return multiple outputs from a function you will use OUT keyword.
Question 28 :- What is the need of Delegates ?
➢ Delegates is a pointer to a function and very useful as callbacks to communicate
between threads.
Question 29 :- What are events ?
➢ Events are encapsulation over delegates.
Question 30 :- Whats the difference between Abstract class and interface ?
➢ Abstract class is a half defined parent class while interface is a contract. Abstract
class is inherited while interface is implemented.
Question 31 - What is a Delegate and how to create a Delegate?
For example,
Public delegate void methodName(string message);
➢ Events use delegates internally. They encapsulate delegates and make them safe.
Question 35 - How to Create an Event?
➢ By using the event keyword.
Syntax:
[Access modifier] event [delegate name] Variable_Name;
For example:
public delegate void delegateName(string search); // delegate
• Abstraction
• Polymorphism
• Inheritance
• Encapsulation
Question 39 :- What is a class and object ?
➢ Class is a type, blueprint.
➢ Object is a instance of the class.
Overriding:- Methods having same name and same signature but are in different class
and these classes must have the parent-child relationship.
We will create new interface. The class that want to implement both the interface will
do multiple inheritance because interface support multiple inheritance.
Association indicates there is dependency between objects. Aggregation and Composition are subset of
Association
Aggregation Composition
Part whole Yes Yes
Life time Independent Dependent
OwnerShip No ownership Parent object has ownership
Death relationship
It’s a background process which runs undetermestically and cleans unreferenced managed objects from
the memory.
Question 128:- How does Garbage collector know when to clean the objects ?
When the objects goes out of scope GC reclaims the memory and gives it to operating system.
Yes, we can analyze GC using performance counters. Performance counters are counters or they are
measures of events in a software which allows us to do analysis. These counters are installed when
software is installed.
So we can use counters like GC Heap size , GC0 , GC1 , GC2 and working set to make more sense of how
GC is working.
Question 130 :- Does Garbage collector clean primitive types ?
No , Garbage collector does not clean primitive types. They are allocated on stack and stack removes
them as soon as the variables goes out of scope.
The whole goal of generations is performance. GC makes a assumption that if objects are needed longer
in memory then it should be visited less as compared to objects which are freshly created and which
have high probability of going out of scope.
Having Empty destructor will cause lot of harm as objects gets promoted to higher generations thus
putting pressure on the memory.
• If the memory is increasing linearly it’s a indication of memory issues. If the memory is moving
in a range it’s a healthy sign.
• Also the memory allocation and deallocation should be balanced. If you just see memory
allocation and no deallocation is other sign that there is serious memory issues.
Question 145:- How can we know the exact source of memory issues ?
In profiler we should check for the top most memory allocated to objects. Once we know the top most
memory allocated to objects we can then focus on code around those objects.
Question 146 :- What is a memory leak ?
Memory leak is a situation where the memory consumed by the application is not returned back to the
operating system when the application exits.
Yes its still possible to have memory leaks because GC only takes care of managed memory. If
unmanaged memory is not claimed properly we can have memory leaks.
Total memory of .NET app = Unmanaged + Managed. So, if you see just see total memory is increasing
and managed is in a range then it means there is Unmanaged leak.
Weak reference: - It permits the GC to collect the object but still allows to access the object until GC
collects the object. We need to use the “WeakReference” object to create weak reference.
Strong reference: - This is a normal referenced objects and once object is marked for GC it can never be
referenced.
Caching , Object pooling . Wherever object creation process is resource intensive caching and pooling
can improve performance.
Design patterns are time tested solution for recurring architecture problems.
Also, with the above definition try to give one or two design patterns as examples. While giving
examples remember three things: -
1. Do not give example of Singleton pattern so that you can stand out in the crowd.
2. Give one example of non-GOF pattern like repository, CQRS and so on.
3. And whatever patterns you say in example make sure you are fully aware of the same.
“Iterator pattern” helps iterate over elements of an aggregate object sequentially without exposing the
underlying representation of the object.
“Template pattern” define a skeleton of the algorithm in parent class and let subclass override specific
steps.
“Repository pattern” helps to abstract and centralize the data access logic in an application. It provides a
layer of separation between the application's business logic and the data access code, making the code
more modular, maintainable, and testable.
Question 154 :- Which design pattern have you used in your project?
Note: - first and foremost do not answer SINGLETON pattern as it will not help you stand in the crowd.
and remember: -
Most of the applications are CRUD so repository pattern comes at the top.
Repository pattern: - Acts like an abstract layer between Models and Data access technologies like
EF, ADO.NET and so on. Data access logic is centralized making code maintainable, testable and
modular.
With repository pattern, UOW goes like hand in gloves.
UOW (Unit of work): - This pattern helps to manage transactions and changes made to objects. It
goes with repository pattern well.
We all know we use FOR EACH so much so the next pattern is also very must used. One
important point to note here talk about IEnumerator and IEnumerable as they implement
iterator by default.
Iterator pattern: - helps iterate over elements of an aggregate object sequentially without exposing
the underlying representation of the object.
Factory pattern:- From creational pattern this is the most used one. Creates an instance of several
derived classes.
Used when third party components are used.
Adapter pattern :- Makes incompatible interfaces compatible.
Decorator pattern :- Add behavior dynamically.
Command pattern:- Treat command as objects. Heavy use in CQRS
Façade :- Represent subsystem in a simplified way.
Composite - A tree structure of simple and composite objects
Template method - Defer the exact steps of an algorithm to a subclass
Question 154 :- Explain Singleton Pattern and the use of the same?
Singleton pattern helps to create a single instance of an object. Some of the uses of Singleton patterns
are: -
To implement singleton pattern (Check the number with the above figure): -
1. Whole part relationship: - The first thing needed in singleton is a root class through which all
shared objects should be exposed. This root class instance should be created inside the class and
it should be static.
2. Thread safety: - Whenever we are loading the singleton object use the “lock” keyword to make
sure only one thread manipulates at a time.
3. Private constructor: - Make sure the root class can not be instantiated from outside.
4. Ensure root object is single instance: - use the double null check and make sure that only one
instance is created and also lock is not executed unnecessarily.
5. Lazy loading implementation: - We would like to load the objects when demanded then loading
it unnecessarily.
6. Performance for threading: - NULL check before the locking to ensure that we do not execute
locks unnecessarily.
Question 156 :- Can we use Static class rather than using a private
constructor?
• Keyword VS Design Pattern: - Static is a language keyword while Singleton is a Design pattern.
• Loose many OOP features: - If you make a class static you cannot implement interfaces, cannot
inherit and so on.
• Lazy loading / Thread Safety: - When you make a class static, object is created in the first call
itself without giving you option of when to load and when not. Second you should also make
sure its thread safe as it’s a global object.
Yes, by using LAZY keyword you can get rid of double null check.
Dal focuses on the technical details of data access while repository pattern provides a higher level of
abstraction over Dal. Dal provides technical centric approach of how to access the data source, while
Repositories provide Domain centric approach.
Note: - I framed this question on purpose as this can be a part of discussion more than a question.
During this discussion the interviewer is expecting that reusability is also another big benefit of
Repository other than loose coupling.
The other benefit of repository pattern is reusability across data access layers. There can be many logic
which is common across data layers and they can be put in a common class and all repositories would
inherit from that common class.
Question 166:-How to implement transaction in repository?
Note: - In this question the interviewer is expecting you should know Unit of work design pattern which
is answered in the next question.
Note :- Many developers do not know how to answer transaction definition so here is the answer :-
Transaction is a group of task / operation where in either the whole group is committed or the whole
group is rolled back.
The first school of thought believes in best practices like loose coupling and unit testing. They
understand the importance of repository pattern that it helps to abstract the Data access layer
(Heterogenous data sources) so that we can change / use different DAL technologies (EF , Dapper ,
ADONET etc.) with ease.
If you are using multiple DAL technologies (heterogenous data sources) and you want plug and play of
DAL in future, repository is a MUST.
Second reason this school also thinks that unit testing is important. Which again is a very compelling
reason you need to have a repository so that you can mock test it easily.
Myth: - Many developers from this school think that EF implements repository pattern. But that’s not
true. Repository helps you to only abstract RDBMS data source and not other data sources (File , API and
so on) . Repository pattern abstracts across Heterogenous data access technologies.
But if you are only dealing with heterogenous RDBMS then I personally feel repository pattern is
overkill.
Comparison of Repository pattern with EF is like comparing apples with oranges because one is Data
access technology while other is a design pattern which focuses on abstraction over heterogenous data
access technologies.
As a good practice it’s always good to use Repository pattern as it creates a higher level of abstraction
over any kind of data source and also makes MOCK UNIT TESTING easy to implement.
After this watch the interviewers reaction if you think he belongs to the second school and if he is
having an ANTI- REPOSITORY mind set then you can just say “Yes , I do agree if we do not have
heterogenous data sources repository pattern can be overkill”.
Note :- One of the key to success during interview is “Satisfying the interviewer that he is RIGHT (The
EGO) ”. That releases a chemical called as dopamine which make him feel good and that can be affect
your interview results in a very positive way.
Question 169:-Did you do unit testing with Repository ?
Note: - All the above flavor of questions stresses the importance of unit testing and mock testing in
repository. Interviewer would like to know that do you understand the importance of unit testing in
repository.
Repository pattern is a higher level of abstraction over Data access logic. And this Abstraction is
represented by EMPTY GENERIC INTERFACE. Because of this generic Interfaces unit testing is like a cake
walk.
The ONE big benefit of doing unit testing on Empty interface is ISOLATED MOCK UNIT Testing. Mock Unit
testing has two primary uses: -
• Unit testing by passing Data access Logic: - Many times you want to only test business logic and
not the data access part. With repository this is like a cake walk.
• Parallel Development and Testing: - Many times data access is developed by some other team ,
so either they have not completed the code or are in between and you want to still test your
part of the code , repository is the answer for it.
private bool FakeSave(){
// Do nothing just by pass
return true;
}
Factory pattern belongs to creational pattern category. It centralizes objection creation and the biggest
benefit of centralizing object creation is “application becomes LOOSELY COUPLED”.
Question 173 :- How does centralizing object creation helps in loose
coupling ?
Object creation is normally done by the NEW keyword. For new keyword you need reference of the
concrete class , once you are referencing the concrete class you have TIGHT coupling scenario.
Now if these “NEW” keyword is scattered all across the places think about the amount of changes you
need to do when you want to switch to Entity Data layer or a different type of Data layer.
So if we can centralize this at one place we do not have to make changes all over the places.
So, IOC says that delegate / invert the object creation to external framework. While DI is an
implementation for IOC which says how the dependent objects will be injected ( from constructor ,
method etc ).
Note :- In interviews you will find use of DI containers which means a central collection of dependencies.
Service locator makes system tightly coupled while DI makes it more loose coupled. Currently
developers mostly use DI.
Question 179 :- Can not we use a simple class rather than interface for
DI ?
You can do it technically, but if you want proper loose coupling , you need proper abstraction and that
abstractness only Interfaces can provide.
No, DI is not factory pattern. DI just says how the objects are injected. While Factory pattern centralizes
COMPLEX object creation.
No. In case of Factory pattern the object creation has to be complex. There has to be some complex
logic.
Static DI is mostly used for services while Dynamic DI is used for complex model creation during runtime.
Personally, for me both of them are same. You will need to cross question the interviewer what exactly
he is looking for. There are some vocabulary confusion, just to try to align with the interviewer to avoid
frictions.
Provides an interface for creating families of related or dependent objects without specifying their
concrete classes.
Note :- This is test question to see that if you understand relationship between Factory and Abstract
Factory.
Yes , Abstract factory uses Factory internally. So, they kind of Aggregate similar families of Factory.
There is nothing officially termed as Simple Factory. Below is a sample code what developers term as
simple factory. But Simple factory is not an official terminology coming from GOF. The below code is
nothing but a great use of two concepts OOP polymorphism and centralizing the NEW keyword.
Question 190 :- Simple Factory vs Factory (Factory Method) vs Abstract
Factory ?
Note :- There is no Pattern as such called as Simple Factory. This name has come up from the industry to
distinguish between Real Factory pattern and Simple IF condition polymorphism.