Stage 7 - Generics, Delegates, Memory
Q43. What are generics?
Quick interview answer:
Generics let us write reusable code while keeping type safety.
For example, List<int> accepts only integers and List<string> accepts only strings, so we avoid unnecessary casting and runtime errors.
Study in detail: Generics - This article expands the topic with complete explanation, examples, and practice code.
Q44. What is a delegate?
Quick interview answer: A delegate is a type-safe reference to a method. It lets us pass a method as a value, which is useful for callbacks, events, and flexible behavior.
Study in detail: Delegates and events - This article expands the topic with complete explanation, examples, and practice code.
Q45. What is an event?
Quick interview answer: An event is a safe way for one object to notify other objects that something happened. For example, a school system can raise an event when a student is registered or fee payment is completed.
Study in detail: Delegates and events - This article expands the topic with complete explanation, examples, and practice code.
Q46. What is the difference between stack and heap?
Quick interview answer: Stack is used for method calls and local value data. It is fast and cleaned automatically when a method finishes. Heap is used for objects created from classes, and it is managed by the Garbage Collector.
Study in detail: Memory management - This article expands the topic with complete explanation, examples, and practice code.
Q47. What is boxing and unboxing?
Quick interview answer:
Boxing converts a value type, like int, into object.
Unboxing converts it back to the value type. Boxing can create extra memory allocations, so generics are preferred.
Study in detail: Memory management - This article expands the topic with complete explanation, examples, and practice code.
Q48. What is nullable reference type?
Quick interview answer:
Nullable reference types help C# warn us when a reference may be null.
For example, string? means the value can be null, while string means it should normally have a value.
Study in detail: Nullable reference types - This article expands the topic with complete explanation, examples, and practice code.
Q49. What is pattern matching?
Quick interview answer: Pattern matching checks an object's type, value, or shape and can safely create a variable. It makes code cleaner than manual casting, especially when working with inheritance or mixed object types.
Study in detail: Modern C# and Type operators - This article expands the topic with complete explanation, examples, and practice code.
Q50. What is the difference between == and Equals()?
Quick interview answer:
For classes, == usually checks whether two variables point to the same object, unless the operator is overloaded.
Equals() can be overridden to compare the actual values inside objects.
Study in detail: Object methods - This article expands the topic with complete explanation, examples, and practice code.