So, you're gearing up for a C# interview? Fantastic! This powerful and versatile language is a cornerstone of modern software development, and landing that C# role can open up a world of exciting opportunities. But navigating the interview process can feel like traversing a complex codebase. Fear not! We've compiled a comprehensive list of the top 60 C# interview questions, complete with detailed answers, to help you ace your next technical challenge.
Whether you're just starting your C# journey or you're a seasoned pro looking to brush up your knowledge, this guide has something for you. We've broken down the questions into three levels: Beginner, Intermediate, and Advanced, allowing you to focus on the areas most relevant to your experience.
Let's dive in and equip you with the knowledge you need to shine!
Beginner Level (1–20)
1. What is C#?
C# is a modern, object-oriented programming language developed by Microsoft as part of its .NET platform. It is designed to be simple, efficient, type-safe, and scalable. C# is widely used for developing desktop applications, web services, and enterprise-level applications.
2. What are value types and reference types?
Value types store data directly (e.g., int, float, char) and are stored on the stack. Reference types store a reference to the memory location where the data is held (e.g., class, string, object) and are stored on the heap.
3. What are the basic data types in C#?
Data types include: int, float, double, bool, char, decimal, string, and more. Divided into value and reference types.
4. What is the difference between ==
and .Equals()
?
-
==
checks reference equality for reference types and value equality for value types. -
.Equals()
checks value equality and can be overridden.
Example:
string a = "hello";
string b = new string("hello".ToCharArray());
Console.WriteLine(a == b); // True
Console.WriteLine(a.Equals(b)); // True
5. What are access modifiers in C#?
They define scope:
-
public
: accessible from anywhere -
private
: within class only -
protected
: class + derived class -
internal
: within the same assembly -
protected internal
: class + same assembly -
private protected
: class + derived in same assembly
6. What is the difference between a class and an object?
A class is a blueprint; an object is an instance of a class.
7. What is a constructor?
A special method with the same name as the class, used to initialize objects.
8. What is method overloading?
Defining multiple methods with the same name but different parameters.
9. What is method overriding?
Changing the behavior of a base class method in a derived class using override
.
10. What is the difference between static and instance members?
-
static
: belongs to the class -
instance
: belongs to the object
11. What is the purpose of the this
keyword?
Refers to the current instance of the class.
12. What is inheritance in C#?
Allows a class to acquire properties/methods from another class using :
.
13. What is polymorphism?
One method behaves differently based on context — compile-time (overloading) and runtime (overriding).
14. What is encapsulation?
Wrapping data and code together and controlling access via access modifiers.
15. What is abstraction?
Hiding internal implementation and showing only functionality.
16. What is a namespace?
Container for classes and other namespaces. Avoids naming conflicts.
17. What is the difference between Array and ArrayList?
-
Array: fixed size, type-safe
-
ArrayList: dynamic size, stores object
18. What is the use of is
and as
operators?
-
is
: checks type -
as
: casts if possible, else returns null
19. What is the difference between const
and readonly
?
-
const
: compile-time constant, static by default -
readonly
: runtime constant, can differ per instance
20. What are ref
and out
parameters?
-
ref
: variable must be initialized before passing -
out
: must be assigned inside the method
Intermediate Level (21–40)
21. What is the difference between interface and abstract class?
Delegates are references to methods with a particular signature. Useful for callbacks and event handling.
23. What are events in C#?
Special delegates that are triggered when something happens.
24. What is a lambda expression?
A concise way to represent anonymous methods using =>
.
Example:
Func<int, int, int> add = (a, b) => a + b;
Value types can hold null using
?
.Example:
26. What is exception handling in C#?
Use try
, catch
, finally
, throw
to manage errors gracefully.
27. What is the using
statement?
Ensures proper disposal of resources (e.g., files, DB connections).
28. What is the difference between StringBuilder
and string
?
-
string
: immutable -
StringBuilder
: mutable, better for frequent modifications
29. What is boxing and unboxing?
-
Boxing: value type → object
-
Unboxing: object → value type
Example:
int i = 42;
object o = i; // Boxing
int j = (int)o; // Unboxing
30. What is a foreach loop?
Simplifies iteration over collections.
31. What are properties in C#?
Provide controlled access to fields using get
and set
.
32. What is an indexer?
Allows objects to be indexed like arrays.
33. What is a static constructor?
Initializes static data, called once automatically.
34. What is difference between ==
and Equals()
for reference types?
==
checks reference; Equals()
can check value equality if overridden.
35. What is a partial class?
Allows a class definition to be split across multiple files.
36. What is a sealed class?
Cannot be inherited.
37. What is a struct in C#?
Value type, lightweight alternative to class.
38. What is a record in C# 9.0+?
Immutable reference type primarily for data storage with value-based equality.
39. What is a destructor?
Finalizer method (~ClassName
) to clean up resources. Rarely used.
40. What is a generic method?
Type-safe reusable method:
public T GetDefault<T>() {
return default(T);
}
Advanced Level (41–60)
41. What is the difference between Task and Thread?
-
Task: high-level, for short or I/O-bound work
-
Thread: low-level, for long-running or CPU-bound
42. What is async/await in C#?
Simplifies asynchronous code:
async Task<int> GetDataAsync() {
return await SomeMethod();
}
43. What is dependency injection (DI)?
Technique for achieving inversion of control by injecting dependencies.
44. What is reflection?
Used to inspect metadata about assemblies, types, and members at runtime.
45. What are attributes in C#?
Metadata added to code (e.g., [Obsolete]
, [Serializable]
).
46. What is the difference between IEnumerable
and IEnumerator
?
-
IEnumerable
: returns an enumerator -
IEnumerator
: allows iteration
47. What is LINQ?
Language Integrated Query to query data in a type-safe manner:
var items = list.Where(x => x > 5);
48. What is difference between var
, dynamic
, and object
?
-
var
: compile-time type inferred -
dynamic
: resolved at runtime -
object
: base type, needs casting
49. What is covariance and contravariance?
Used in generics to allow type flexibility with in
and out
keywords.
50. What is a memory leak in C#?
Occurs when objects are not released due to references still held.
51. What is garbage collection in .NET?
Automatic memory management that frees unused objects.
52. What is the lock
keyword used for?
Ensures thread-safe execution of code blocks.
53. What are extension methods?
Allow adding methods to existing types without modifying them.
54. What is a thread pool?
Manages threads efficiently for concurrent work.
55. What is the difference between finalize and dispose?
-
Finalize: part of GC, non-deterministic
-
Dispose: deterministic, user-controlled
56. What is yield
in C#?
Used in iterators to return values lazily:
yield return value;
Lightweight data structure for returning multiple values:
58. What is pattern matching in C#?
Switch expressions and type checks:
Comments
Post a Comment