Here you get a free C# Interview Question which helps you to Give Correct Answer of the Question in the Interview. It also helps you for you college exams Preparation.
Basics C# and Syntax
- What is the difference between
const
,readonly
, andstatic
in C#? Provide examples. - Explain the purpose of nullable value types in C#. How are they declared and used?
- What are extension methods in C#? Provide an example of when and how you would use one.
- Discuss the var keyword. When should it be used, and when should it be avoided?
- Explain the difference between String and string in C#. Are they interchangeable?
- Why is Main declared static in C#, and what would happen if it weren’t?
- Explain the role of partial classes in C#. How do they improve modularity?
- What is the nameof operator, and how is it useful in C#?
- Explain is and as operators in C# with practical examples.
- Discuss how discard variables (_) can be used in C#.
Object-Oriented Programming (OOP)
- Explain how C# implements polymorphism. Provide code examples.
- What is the difference between method hiding (
new
keyword) and method overriding (override
keyword)? - Discuss the concept of
sealed
classes and methods. Why and when would you use them? - Explain abstract classes versus interfaces. When should you choose one over the other?
- How does C# handle multiple interface inheritance? Provide examples of resolving conflicts.
- Can you declare a constructor in an interface? Why or why not?
- How does C# support encapsulation? Provide examples demonstrating access modifiers.
- What is the difference between shallow copy and deep copy in C#? Provide code examples.
- Explain covariance and contravariance in delegates and generics with examples.
- How do
base
andthis
keywords differ in usage? - Describe the purpose of virtual methods. What happens if a derived class doesn’t override them?
- What are the pros and cons of implementing default interface methods in C#?
- What is the difference between composition and inheritance? When is each appropriate?
- Can static methods be overridden in C#? Why or why not?
- Explain
Object.Equals
andObject.ReferenceEquals
. How do they differ? - What is the difference between overriding and overloading? Provide examples.
- Can you instantiate an abstract class in C#? If yes, how?
- What is the purpose of a destructor in C#, and how is it declared?
- Explain the concept of a “pure” virtual function in C#.
- Why would you use
protected internal
as an access modifier?
LINQ and Collections
- Explain deferred execution in LINQ. How does it affect query performance?
- What is the difference between
IEnumerable
,IQueryable
, andList
in C#? - How would you write a custom LINQ extension method?
- Discuss the significance of
GroupBy
in LINQ. - Provide a practical example. Explain the difference between
Select
andSelectMany
. - What is the purpose of
ToLookup
in LINQ? - How does it differ from
GroupBy
? - How does LINQ-to-SQL differ from LINQ-to-Objects?
- Provide an example of using LINQ to calculate the Cartesian product of two sequences.
- Explain how LINQ uses lambda expressions for query syntax.How would you perform pagination using LINQ?
- What are immutable collections in C#? How do they differ from regular collections?
- Discuss the differences between
HashSet
,Dictionary
, andList
. - How does a
ConcurrentDictionary
differ from a regularDictionary
in C#? - What is the purpose of the
yield
keyword? How does it work in LINQ queries? - How does LINQ handle null values in sequences?
- Explain how LINQ queries can be parallelized using PLINQ.
- How does
OrderBy
preserve stability in sorting? - What are the advantages of using
ObservableCollection
overList
? - Can LINQ queries be chained? Provide examples.
- How does
Aggregate
work in LINQ? Provide practical usage scenarios.
Multithreading and Asynchronous Programming
- Explain the difference between
Task
andThread
in C#. - How does
async
/await
improve asynchronous programming in C#? - What is the difference between
Task.Run
andTask.Factory.StartNew
? - How does the
SynchronizationContext
affect async code execution? - Explain the difference between
lock
,Monitor
, andMutex
. - What is the purpose of
async void
, and why should it be avoided in most cases? - Explain the
ThreadPool
in C#. When should you use it? - What is a
SemaphoreSlim
, and how does it differ fromSemaphore
? - What is the role of
ConfigureAwait(false)
in async programming? - How do you implement a producer-consumer problem in C#?
- What is the purpose of
CancellationToken
in async tasks? Provide examples. - Discuss the purpose of
Task.WhenAll
andTask.WhenAny
. - How does the
Parallel.ForEach
method differ fromForEach
? - What is a deadlock, and how can you prevent it in C#?
- Explain the concept of thread safety and how to achieve it.
- What is a
BlockingCollection
, and when would you use it? - How does the
Volatile
keyword work in C#? - Explain the difference between race conditions and data corruption in multithreading.
- What is
ConcurrentBag
, and how does it work? - How do you ensure a task runs synchronously without blocking a thread?
Methods and Functions
- What is the difference between
params
andparams object[]
in method parameters? Provide examples of their usage. - Can you declare two methods with the same name but different return types? Why or why not?
- Explain the purpose of optional parameters in C#. How do they affect method overloading?
- How do named arguments in C# improve method readability? Provide an example.
- Explain how method hiding (
new
keyword) differs from overriding (override
keyword) in C#. Provide examples. - Can a method be overridden in a derived class if it is declared as
private
in the base class? Why or why not? - What is a virtual method? How does the runtime decide which version of the method to call?
- How do anonymous methods differ from lambda expressions in C#? Provide examples.
- What is the role of
Func
,Action
, andPredicate
in method delegates? How are they used? - Explain how closures work with lambda expressions in methods. Provide examples of potential pitfalls.
- How are extension methods resolved when multiple classes define the same extension method for the same type?
- Can an extension method be used to override an existing instance method? Why or why not?
- Write an extension method for a
string
to reverse its characters. - How does the
async
modifier affect the method signature and return type in C#? - Why can’t
async
methods returnvoid
, and what are the exceptions to this rule? - What happens if you call an
async
method withoutawait
? - Explain the differences between
in
,ref
, andout
parameters. Provide examples of each. - What is the purpose of
default
literal in generic methods, and how does it differ fromdefault(T)
? - How do you handle a method that takes a varying number of generic parameters?
- How do you implement a method with a callback mechanism? Provide an example.