- ViewState: Stores data in the page itself and is limited to that page only.
- Server.Transfer: Transfers control to another page on the server without changing the URL.
- Response.Redirect: Redirects the client to a new URL and changes the browser’s address bar.
Ans: The Repeater control is a lightweight data-bound control that allows custom layouts for displaying repeated data.
- Dynamic Binding: Data is assigned at runtime (commonly used with controls like GridView).
- Session State: Data is stored per user and lasts until the session expires.
- Application State: Data is shared across all users and persists until the application stops.
21. What is boxing and unboxing in C#?
Ans: Boxing is the process of converting a value type (e.g., int, struct) to a reference type (object). Unboxing is the reverse process, converting a reference type back to a value type.
22. What is the difference between deep copy and shallow copy?
Ans: A shallow copy copies the reference of objects, so both the original and copy point to the same data. A deep copy creates a new object and recursively copies the values of the original object.
23. What is the `using` statement in C#?
Ans: The `using` statement ensures that objects like file streams are disposed of correctly, releasing resources when they are no longer needed.
24. What is the purpose of the `IDisposable` interface?
Ans: The `IDisposable` interface provides a mechanism for releasing unmanaged resources, with the `Dispose` method used to perform cleanup operations.
25. What is the difference between `throw` and `throw ex`?
Ans: `throw` rethrows the current exception, preserving the original stack trace. `throw ex` rethrows the exception `ex`, resetting the stack trace.
26. What is the purpose of the `finally` block in C#?
Ans: The `finally` block contains code that is executed after the `try` and `catch` blocks, regardless of whether an exception was thrown, ensuring resource cleanup.
27. What is the difference between `continue` and `break` statements?
Ans: `continue` skips the remaining code in the loop for the current iteration and proceeds to the next iteration. `break` exits the loop entirely.
28. What is the purpose of the `yield` keyword in C#?
Ans: The `yield` keyword is used in iterator methods to return an enumerator that allows iteration over a collection of values.
29. What is the difference between `value` types and `reference` types?
Ans: Value types hold data directly (e.g., int, struct), while reference types hold a reference to the data (e.g., class, object).
30. What is the purpose of the `sealed` keyword in C#?
Ans: The `sealed` keyword prevents a class from being inherited, ensuring that it cannot be used as a base class.
31. What is the difference between `is` and `as` keywords?
Ans: `is` checks if an object is of a specific type and returns a boolean. `as` attempts to cast an object to a specific type and returns null if the cast fails.
32. 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.
33. 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.
34. 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.
35. 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.
36. 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.
37. 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.
38. 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.
39. 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.
40. 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.
41. 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.
42. 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.
43. 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.
44. 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.
45. 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.
46. 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.
47. 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.
48. 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.
49. 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.
50. 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.
51. 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.
52. 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.
53. 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`.
54. 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.
55. 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.
56. 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.
57. 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.
58. 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.
59. 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.
60. 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.
61. 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.
62. What is the purpose of the `ManualResetEventSlim` class in C#?
Ans: The `ManualResetEventSlim` class provides a lightweight, thread-safe event for signaling between threads.
63. What is the difference between `ManualResetEvent` and `ManualResetEventSlim`?
Ans: `ManualResetEvent` is a full-featured event with cross-process support, while `ManualResetEventSlim` is a lightweight event for in-process use.
64. What is the purpose of the `AutoResetEvent` class in C#?
Ans: The `AutoResetEvent` class provides an event that automatically resets after releasing a single waiting thread.
65. What is the difference between `AutoResetEvent` and `ManualResetEvent`?
Ans: `AutoResetEvent` automatically resets after releasing one waiting thread, while `ManualResetEvent` must be manually reset.
66. 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.
67. What is the difference between `CountdownEvent` and `ManualResetEvent`?
Ans: `CountdownEvent` is set after a specified number of signals, while `ManualResetEvent` is set manually.
68. 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.
69. 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.
70. 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.
71. 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.
72. 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.
73. 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.
74. 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.
75. 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.
76. 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.
77. 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.
78. 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.
79. 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.
80. 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.
81. 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.
82. 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.
83. 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.
84. 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.
85. 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.
86. 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.
87. 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.
88. 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 collected by the garbage collector.
89. 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.
90. 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.
91. 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.
92. 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.
93. 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.
94. 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.
95. 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.
96. 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.
97. 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.
98. 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.
99. What is the difference between `IAsyncDisposable` and `IDisposable`?
Ans: `IAsyncDisposable` provides asynchronous resource release, while `IDisposable` provides synchronous resource release.
100. 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.