OOPs concepts, Encapsulation, Abstraction
1. What is Encapsulation in C#?
Ans: Encapsulation is a principle in object-oriented programming that involves bundling data and methods that operate on that data into a single unit, called an object.
2. What is the difference between `sealed` and `static` keywords in C#?
Ans: The `sealed` keyword prevents a class from being inherited, ensuring that it cannot be used as a base class. The `static` keyword defines a member that belongs to the class rather than an instance of the class.
3. What is the purpose of the `params` keyword in C#?
Ans: The `params` keyword allows a method to accept a variable number of arguments of a specified type, passed as an array.
Looking for more questions on Array, String and String Builder to build a strong foundation!
4. What is the difference between `==` and `.Equals()` methods?
Ans: `==` compares references for reference types and values for value types. `.Equals()` compares the actual content of objects.
5. What is the purpose of the `virtual` keyword in C#?
Ans: The `virtual` keyword allows a method to be overridden in a derived class, enabling polymorphism.
6. What is the difference between `override` and `new` keywords?
Ans: `override` extends or modifies a virtual method in a base class. `new` hides a member inherited from a base class.
7. What is the purpose of the `base` keyword in C#?
Ans: The `base` keyword is used to access members of a base class from a derived class, such as calling a base class constructor.
8. What is the difference between `struct` and `class` in C#?
Ans: `struct` is a value type stored on the stack, while `class` is a reference type stored on the heap.
9. What is the purpose of the `this` keyword in C#?
Ans: The `this` keyword refers to the current instance of a class, used to access class members and differentiate between class members and parameters.
10. What is the difference between `abstract` class and `interface`?
Ans: An `abstract` class can have implemented methods and fields, while an `interface` can only declare method signatures and properties without implementation.
11. What is the purpose of the `partial` keyword in C#?
Ans: The `partial` keyword allows a class, struct, or interface to be split across multiple files, useful for separating generated code from user code.
12. What is the difference between `List` and `Array` in C#?
Ans: `List` is a dynamic array that can grow and shrink in size, while `Array` has a fixed size.
13. What is the purpose of the `Tuple` class in C#?
Ans: The `Tuple` class provides a simple way to store a fixed number of elements of different types without creating a new class or struct.
14. What is the difference between `foreach` and `for` loops?
Ans: `foreach` is used to iterate over collections implementing `IEnumerable`, while `for` is used for iterating a specific number of times with an index.
15. What is the purpose of the `Nullable` type in C#?
Ans: The `Nullable` type allows value types to represent null values, useful for database interactions where nullable fields are common.
16. What is the difference between `StringBuilder` and `string`?
Ans: `StringBuilder` is mutable and more efficient for concatenating strings, while `string` is immutable and creates new instances for each modification.
17. What is the purpose of the `lock` statement in C#?
Ans: The `lock` statement is used to ensure that a block of code is executed by only one thread at a time, preventing race conditions.
18. What is the difference between `Monitor` and `lock`?
Ans: `lock` is a simplified way to use `Monitor.Enter` and `Monitor.Exit`, providing a more concise syntax for thread synchronization.
19. What is the purpose of the `volatile` keyword in C#?
Ans: The `volatile` keyword indicates that a field might be modified by multiple threads, ensuring that reads and writes are not optimized away.
20. What is the difference between `Thread` and `Task`?
Ans: `Thread` represents a single thread of execution, while `Task` represents an asynchronous operation that can be awaited.
Looking for more Microservice resources? Read our Master Microservices: Top 30 Interview Questions and Answers for 2025 to build a strong foundation!
21. What is the purpose of the `CancellationToken` in C#?
Ans: `CancellationToken` is used to propagate notification that operations should be canceled, allowing for cooperative cancellation of tasks.
22. What is the difference between `async` and `await`?
Ans: `async` is used to mark a method as asynchronous, enabling the use of `await`. `await` is used to pause the execution of an `async` method until the awaited task completes.
23. What is the purpose of the `Task` class in C#?
Ans: The `Task` class represents an asynchronous operation, providing methods to wait for the operation to complete and retrieve its result.
24. What is the difference between `Task` and `Task`?
Ans: `Task` represents an asynchronous operation that does not return a value, while `Task` represents an asynchronous operation that returns a value of type `TResult`.
25. What is the purpose of the `Parallel` class in C#?
Ans: The `Parallel` class provides support for parallel programming, including methods for parallel loops and task execution.
26. What is the difference between `Parallel.For` and `Parallel.ForEach`?
Ans: `Parallel.For` is used for parallel execution of a `for` loop, while `Parallel.ForEach` is used for parallel execution of a `foreach` loop.
27. What is the purpose of the `ConcurrentDictionary` class in C#?
Ans: The `ConcurrentDictionary` class provides a thread-safe collection for key-value pairs, allowing concurrent reads and writes.
28. What is the difference between `ConcurrentDictionary` and `Dictionary`?
Ans: `ConcurrentDictionary` is thread-safe and supports concurrent operations, while `Dictionary` is not thread-safe and should be used in single-threaded scenarios.
29. What is the purpose of the `BlockingCollection` class in C#?
Ans: The `BlockingCollection` class provides a thread-safe collection for producer-consumer scenarios, supporting blocking and bounding.
30. What is the difference between `BlockingCollection` and `ConcurrentQueue`?
Ans: `BlockingCollection` supports blocking and bounding, while `ConcurrentQueue` is a thread-safe queue without blocking or bounding capabilities.
31. What is the purpose of the `SemaphoreSlim` class in C#?
Ans: The `SemaphoreSlim` class provides a lightweight, thread-safe semaphore for limiting the number of concurrent operations.
32. What is the difference between `Semaphore` and `SemaphoreSlim`?
Ans: `Semaphore` is a full-featured semaphore with cross-process support, while `SemaphoreSlim` is a lightweight semaphore for in-process use.
33. What is the purpose of the `ManualResetEventSlim` class in C#?
Ans: The `ManualResetEventSlim` class provides a lightweight, thread-safe event for signaling between threads.
34. What is the purpose of the `AutoResetEvent` class in C#?
Ans: The `AutoResetEvent` class provides an event that is set when a specified number of signals have been received.
35. What is the difference between `AutoResetEvent` and `ManualResetEvent`?
Ans: `AutoResetEvent` automatically resets after releasing one waiting thread, while `ManualResetEvent` must be manually reset.
36. What is the purpose of the `CountdownEvent` class in C#?
Ans: The `CountdownEvent` class provides an event that is set when a specified number of signals have been received.
37. What is the difference between `CountdownEvent` and `ManualResetEvent`?
Ans: `CountdownEvent` is set after a specified number of signals, while `ManualResetEvent` is set manually.
38. What is the purpose of the `Barrier` class in C#?
Ans: The `Barrier` class provides a synchronization primitive for coordinating multiple threads at a barrier point.
39. What is the difference between `Barrier` and `CountdownEvent`?
Ans: `Barrier` coordinates multiple threads at a barrier point, while `CountdownEvent` is set after a specified number of signals.
40. What is the purpose of the `ReaderWriterLockSlim` class in C#?
Ans: The `ReaderWriterLockSlim` class provides a lightweight, thread-safe lock for managing access to a resource by multiple readers and writers.
41. What is the difference between `ReaderWriterLock` and `ReaderWriterLockSlim`?
Ans: `ReaderWriterLock` is a full-featured lock with cross-process support, while `ReaderWriterLockSlim` is a lightweight lock for in-process use.
42. What is the purpose of the `Mutex` class in C#?
Ans: The `Mutex` class provides a synchronization primitive for mutual exclusion, ensuring that only one thread can access a resource at a time.
43. What is the difference between `Mutex` and `Monitor`?
Ans: `Mutex` is a system-wide synchronization primitive, while `Monitor` is a lightweight synchronization primitive for in-process use.
44. What is the purpose of the `SpinLock` class in C#?
Ans: The `SpinLock` class provides a low-level, lightweight lock for short-duration synchronization, using a spin-wait loop.
45. What is the difference between `SpinLock` and `Monitor`?
Ans: `SpinLock` is a low-level lock suitable for short-duration synchronization, while `Monitor` is a higher-level lock suitable for longer-duration synchronization.
46. What is the purpose of the `Interlocked` class in C#?
Ans: The `Interlocked` class provides atomic operations for variables shared by multiple threads, ensuring thread safety without locks.
47. What is the difference between `Interlocked` and `lock`?
Ans: `Interlocked` provides lock-free atomic operations, while `lock` uses a monitor to ensure that a block of code is executed by only one thread at a time.
48. What is the purpose of the `ThreadLocal` class in C#?
Ans: The `ThreadLocal` class provides thread-local storage, allowing each thread to have its own instance of a variable.
49. What is the difference between `ThreadLocal` and `static`?
Ans: `ThreadLocal` provides a separate instance of a variable for each thread, while `static` provides a single instance shared across all threads.
50. What is the purpose of the `ThreadPool` class in C#?
Ans: The `ThreadPool` class provides a pool of worker threads for executing tasks, improving performance by reusing threads.
51. What is the difference between `ThreadPool` and `Task`?
Ans: `ThreadPool` manages a pool of threads for executing tasks, while `Task` represents an asynchronous operation that can be awaited.
52. What is the purpose of the `SynchronizationContext` class in C#?
Ans: The `SynchronizationContext` class provides a base class for synchronization contexts, allowing custom synchronization behaviors for asynchronous operations.
53. What is the difference between `SynchronizationContext` and `TaskScheduler`?
Ans: `SynchronizationContext` provides a context for synchronizing asynchronous operations, while `TaskScheduler` schedules tasks for execution on threads.
54. What is the purpose of the `CancellationTokenSource` class in C#?
Ans: The `CancellationTokenSource` class provides a mechanism for creating and managing cancellation tokens, allowing for cooperative cancellation of tasks.
55. What is the difference between `CancellationTokenSource` and `CancellationToken`?
Ans: `CancellationTokenSource` creates and manages cancellation tokens, while `CancellationToken` is used to propagate cancellation requests to tasks.
56. What is the purpose of the `Lazy` class in C#?
Ans: The `Lazy` class provides support for lazy initialization, delaying the creation and initialization of an object until it is needed.
57. What is the difference between `Lazy` and `Lazy`?
Ans: `Lazy` is a non-generic class for lazy initialization, while `Lazy` is a generic class that provides type-safe lazy initialization.
58. What is the purpose of the `WeakReference` class in C#?
Ans: The `WeakReference` class provides a way to maintain a reference to an object without preventing it from being garbage collected.
59. What is the difference between `WeakReference` and `strong` reference?
Ans: A `WeakReference` does not prevent an object from being garbage collected, while a strong reference does.
60. What is the purpose of the `GC` class in C#?
Ans: The `GC` class provides methods for interacting with the garbage collector, such as forcing a garbage collection or getting the current generation.
61. What is the difference between `GC.Collect` and `GC.WaitForPendingFinalizers`?
Ans: `GC.Collect` forces an immediate garbage collection, while `GC.WaitForPendingFinalizers` waits for all finalizers to complete before returning.
62. What is the purpose of the `Memory` class in C#?
Ans: The `Memory` class provides a way to work with slices of managed memory, allowing for efficient memory management without copying data.
63. What is the difference between `Memory` and `ArraySegment`?
Ans: `Memory` provides a more flexible and efficient way to work with slices of memory, while `ArraySegment` is a simpler structure for working with segments of arrays.
64. What is the purpose of the `Span` class in C#?
Ans: The `Span` class provides a type-safe way to work with contiguous regions of arbitrary memory, enabling high-performance, low-allocation programming.
65. What is the difference between `Span` and `Memory`?
Ans: `Span` represents a contiguous region of memory and is stack-only, while `Memory` can represent non-contiguous memory and can be heap-allocated.
66. What is the purpose of the `ValueTask` class in C#?
Ans: The `ValueTask` class provides a way to return results from asynchronous methods without allocating a `Task` object, improving performance for value-based results.
67. What is the difference between `ValueTask` and `Task`?
Ans: `ValueTask` is a lightweight alternative to `Task` for returning results from asynchronous methods, avoiding the allocation of a `Task` object for value-based results.
68. What is the purpose of the `IAsyncDisposable` interface in C#?
Ans: The `IAsyncDisposable` interface provides a way to release unmanaged resources asynchronously, enabling efficient resource management in asynchronous code.
69. What is the difference between `IAsyncDisposable` and `IDisposable`?
Ans: `IAsyncDisposable` provides asynchronous resource release, while `IDisposable` provides synchronous resource release.
Looking for more ASP.NET Core resources? Read our Most important ASP.NET Web Forms Interview Questions to build a strong foundation!
70. What is the purpose of the `CancellationTokenRegistration` class in C#?
Ans: The `CancellationTokenRegistration` class provides a way to register a callback that is invoked when a `CancellationToken` is canceled, enabling custom cancellation logic.
71. What is the purpose of the `ThreadLocal` class in C#?
Ans: The `ThreadLocal` class provides thread-local storage, allowing each thread to have its own instance of a variable.
72. What is the difference between `ThreadLocal` and `static`?
Ans: `ThreadLocal` provides a separate instance of a variable for each thread, while `static` provides a single instance shared across all threads.
73. What is the purpose of the `ThreadPool` class in C#?
Ans: The `ThreadPool` class provides a pool of worker threads for executing tasks, improving performance by reusing threads.
74. What is the difference between `ThreadPool` and `Task`?
Ans: `ThreadPool` manages a pool of threads for executing tasks, while `Task` represents an asynchronous operation that can be awaited.
75. What is the purpose of the `SynchronizationContext` class in C#?
Ans: The `SynchronizationContext` class provides a base class for synchronization contexts, allowing custom synchronization behaviors for asynchronous operations.
76. What is the difference between `SynchronizationContext` and `TaskScheduler`?
Ans: `SynchronizationContext` provides a context for synchronizing asynchronous operations, while `TaskScheduler` schedules tasks for execution on threads.
77. What is the purpose of the `CancellationTokenSource` class in C#?
Ans: The `CancellationTokenSource` class provides a mechanism for creating and managing cancellation tokens, allowing for cooperative cancellation of tasks.
78. What is the difference between `CancellationTokenSource` and `CancellationToken`?
Ans: `CancellationTokenSource` creates and manages cancellation tokens, while `CancellationToken` is used to propagate cancellation requests to tasks.
79. What is the purpose of the `Lazy` class in C#?
Ans: The `Lazy` class provides support for lazy initialization, delaying the creation and initialization of an object until it is needed.
80. What is the difference between `Lazy` and `Lazy`?
Ans: `Lazy` is a non-generic class for lazy initialization, while `Lazy` is a generic class that provides type-safe lazy initialization.
81. What is the purpose of the `WeakReference` class in C#?
Ans: The `WeakReference` class provides a way to maintain a reference to an object without preventing it from being garbage collected.
82. What is the difference between `WeakReference` and `strong` reference?
Ans: A `WeakReference` does not prevent an object from being garbage collected, while a strong reference does.
83. What is the purpose of the `GC` class in C#?
Ans: The `GC` class provides methods for interacting with the garbage collector, such as forcing a garbage collection or getting the current generation.
84. What is the difference between `GC.Collect` and `GC.WaitForPendingFinalizers`?
Ans: `GC.Collect` forces an immediate garbage collection, while `GC.WaitForPendingFinalizers` waits for all finalizers to complete before returning.
85. What is the purpose of the `Memory` class in C#?
Ans: The `Memory` class provides a way to work with slices of managed memory, allowing for efficient memory management without copying data.
86. What is the difference between `Memory` and `ArraySegment`?
Ans: `Memory` provides a more flexible and efficient way to work with slices of memory, while `ArraySegment` is a simpler structure for working with segments of arrays.
87. What is the purpose of the `Span` class in C#?
Ans: The `Span` class provides a type-safe way to work with contiguous regions of arbitrary memory, enabling high-performance, low-allocation programming.
88. What is the difference between `Span` and `Memory`?
Ans: `Span` represents a contiguous region of memory and is stack-only, while `Memory` can represent non-contiguous memory and can be heap-allocated.
89. What is the purpose of the `ValueTask` class in C#?
Ans: The `ValueTask` class provides a way to return results from asynchronous methods without allocating a `Task` object, improving performance for value-based results.
90. What is the difference between `ValueTask` and `Task`?
Ans: `ValueTask` is a lightweight alternative to `Task` for returning results from asynchronous methods, avoiding the allocation of a `Task` object for value-based results.
91. What is the purpose of the `IAsyncDisposable` interface in C#?
Ans: The `IAsyncDisposable` interface provides a way to release unmanaged resources asynchronously, enabling efficient resource management in asynchronous code.
92. What is the difference between `IAsyncDisposable` and `IDisposable`?
Ans: `IAsyncDisposable` provides asynchronous resource release, while `IDisposable` provides synchronous resource release.
93. What is the purpose of the `CancellationTokenRegistration` class in C#?
Ans: The `CancellationTokenRegistration` class provides a way to register a callback that is invoked when a `CancellationToken` is canceled, enabling custom cancellation logic.
94. What is the purpose of the `ThreadLocal` class in C#?
Ans: The `ThreadLocal` class provides thread-local storage, allowing each thread to have its own instance of a variable.
95. What is the difference between `ThreadLocal` and `static`?
Ans: `ThreadLocal` provides a separate instance of a variable for each thread, while `static` provides a single instance shared across all threads.
96. What is the purpose of the `ThreadPool` class in C#?
Ans: The `ThreadPool` class provides a pool of worker threads for executing tasks, improving performance by reusing threads.
97. What is the difference between `ThreadPool` and `Task`?
Ans: `ThreadPool` manages a pool of threads for executing tasks, while `Task` represents an asynchronous operation that can be awaited.
98. What is the purpose of the `SynchronizationContext` class in C#?
Ans: The `SynchronizationContext` class provides a base class for synchronization contexts, allowing custom synchronization behaviors for asynchronous operations.
99. What is the difference between `SynchronizationContext` and `TaskScheduler`?
Ans: `SynchronizationContext` provides a context for synchronizing asynchronous operations, while `TaskScheduler` schedules tasks for execution on threads.
100. What is the purpose of the `CancellationTokenSource` class in C#?
Ans: The `CancellationTokenSource` class provides a mechanism for creating and managing cancellation tokens, allowing for cooperative cancellation of tasks.
101. What is the difference between `CancellationTokenSource` and `CancellationToken`?
Ans: `CancellationTokenSource` creates and manages cancellation tokens, while `CancellationToken` is used to propagate cancellation requests to tasks.
102. What is the purpose of the `Lazy` class in C#?
Ans: The `Lazy` class provides support for lazy initialization, delaying the creation and initialization of an object until it is needed.
103. What is the difference between `Lazy` and `Lazy`?
Ans: `Lazy` is a non-generic class for lazy initialization, while `Lazy` is a generic class that provides type-safe lazy initialization.
104. What is the purpose of the `WeakReference` class in C#?
Ans: The `WeakReference` class provides a way to maintain a reference to an object without preventing it from being garbage collected.
105. What is the difference between `WeakReference` and `strong` reference?
Ans: A `WeakReference` does not prevent an object from being garbage collected, while a strong reference does.
106. What is the purpose of the `GC` class in C#?
Ans: The `GC` class provides methods for interacting with the garbage collector, such as forcing a garbage collection or getting the current generation.
107. What is the difference between `GC.Collect` and `GC.WaitForPendingFinalizers`?
Ans: `GC.Collect` forces an immediate garbage collection, while `GC.WaitForPendingFinalizers` waits for all finalizers to complete before returning.
108. What is the purpose of the `Memory` class in C#?
Ans: The `Memory` class provides a way to work with slices of managed memory, allowing for efficient memory management without copying data.
Looking for more ASP.NET resources? Read our Most important questions based on OOPs. to build a strong foundation!
109. What is the difference between `Memory` and `ArraySegment`?
Ans: `Memory` provides a more flexible and efficient way to work with slices of memory, while `ArraySegment` is a simpler structure for working with segments of arrays.
110. What is the purpose of the `Span` class in C#?
Ans: The `Span` class provides a type-safe way to work with contiguous regions of arbitrary memory, enabling high-performance, low-allocation programming.
111. What is the difference between `Span` and `Memory`?
Ans: `Span` represents a contiguous region of memory and is stack-only, while `Memory` can represent non-contiguous memory and can be heap-allocated.
112. What is the purpose of the `ValueTask` class in C#?
Ans: The `ValueTask` class provides a way to return results from asynchronous methods without allocating a `Task` object, improving performance for value-based results.
113. What is the difference between `ValueTask` and `Task`?
Ans: `ValueTask` is a lightweight alternative to `Task` for returning results from asynchronous methods, avoiding the allocation of a `Task` object for value-based results.
114. What is the purpose of the `IAsyncDisposable` interface in C#?
Ans: The `IAsyncDisposable` interface provides a way to release unmanaged resources asynchronously, enabling efficient resource management in asynchronous code.
115. What is the difference between `IAsyncDisposable` and `IDisposable`?
Ans: `IAsyncDisposable` provides asynchronous resource release, while `IDisposable` provides synchronous resource release.
116. What is the purpose of the `CancellationTokenRegistration` class in C#?
Ans: The `CancellationTokenRegistration` class provides a way to register a callback that is invoked when a `CancellationToken` is canceled, enabling custom cancellation logic.
117. What is the purpose of the `ThreadLocal` class in C#?
Ans: The `ThreadLocal` class provides thread-local storage, allowing each thread to have its own instance of a variable.
118. What is the difference between `ThreadLocal` and `static`?
Ans: `ThreadLocal` provides a separate instance of a variable for each thread, while `static` provides a single instance shared across all threads.
119. What is the purpose of the `ThreadPool` class in C#?
Ans: The `ThreadPool` class provides a pool of worker threads for executing tasks, improving performance by reusing threads.
120. What is the difference between `ThreadPool` and `Task`?
Ans: `ThreadPool` manages a pool of threads for executing tasks, while `Task` represents an asynchronous operation that can be awaited.
121. What is the purpose of the `SynchronizationContext` class in C#?
Ans: The `SynchronizationContext` class provides a base class for synchronization contexts, allowing custom synchronization behaviors for asynchronous operations.
122. What is the difference between `SynchronizationContext` and `TaskScheduler`?
Ans: `SynchronizationContext` provides a context for synchronizing asynchronous operations, while `TaskScheduler` schedules tasks for execution on threads.
123. What is the purpose of the `CancellationTokenSource` class in C#?
Ans: The `CancellationTokenSource` class provides a mechanism for creating and managing cancellation tokens, allowing for cooperative cancellation of tasks.
124. What is the difference between `CancellationTokenSource` and `CancellationToken`?
Ans: `CancellationTokenSource` creates and manages cancellation tokens, while `CancellationToken` is used to propagate cancellation requests to tasks.
125. What is the purpose of the `Lazy` class in C#?
Ans: The `Lazy` class provides support for lazy initialization, delaying the creation and initialization of an object until it is needed.
126. What is the difference between `Lazy` and `Lazy`?
Ans: `Lazy` is a non-generic class for lazy initialization, while `Lazy` is a generic class that provides type-safe lazy initialization.
127. What is the purpose of the `WeakReference` class in C#?
Ans: The `WeakReference` class provides a way to maintain a reference to an object without preventing it from being garbage collected.
128. What is the difference between `WeakReference` and `strong` reference?
Ans: A `WeakReference` does not prevent an object from being garbage collected, while a strong reference does.
129. What is the purpose of the `GC` class in C#?
Ans: The `GC` class provides methods for interacting with the garbage collector, such as forcing a garbage collection or getting the current generation.
130. What is the difference between `GC.Collect` and `GC.WaitForPendingFinalizers`?
Ans: `GC.Collect` forces an immediate garbage collection, while `GC.WaitForPendingFinalizers` waits for all finalizers to complete before returning.
131. What is the purpose of the `Memory` class in C#?
Ans: The `Memory` class provides a way to work with slices of managed memory, allowing for efficient memory management without copying data.
132. What is the difference between `Memory` and `ArraySegment`?
Ans: `Memory` provides a more flexible and efficient way to work with slices of memory, while `ArraySegment` is a simpler structure for working with segments of arrays.