C# Fundamentals Flashcards
What are Generics in C#?
Generics allow you to create classes, methods, and structures that can work with different data types without specifying them in advance.
How are Generics useful in achieving type safety?
Generics ensure type safety by allowing you to specify the type of data a class or method works with at compile-time.
Explain the difference between a Generic class and a Generic method.
A Generic class is a class that can work with multiple data types, while a Generic method is a method within a class that can take different types as arguments.
What is a Generic constraint?
A Generic constraint restricts the types that can be used with a Generic class or method, ensuring they meet certain criteria.
How do you specify a Generic constraint in C#?
You can specify a Generic constraint using the where keyword followed by the constraint(s) in angle brackets.
What is a Covariant Generic?
Covariant Generics allow you to use a derived class where a base class is expected.
What is a Contravariant Generic?
Contravariant Generics allow you to use a base class where a derived class is expected.
Give an example of using Generics in a collection.
List<T> myList = new List<T>();</T></T>
Can you use Generics with value types and reference types?
Yes, Generics can be used with both value types and reference types.
What is the purpose of the default keyword in Generics?
The default keyword returns the default value of a type in Generics, which is null for reference types and a zero-initialized value for value types.
What is a Delegate in C#?
A Delegate is a type that defines a method signature, allowing you to reference methods as parameters and invoke them indirectly.
What is a Multicast Delegate?
A Multicast Delegate is a delegate that holds references to multiple methods and can call them all.
How do you declare a Delegate?
delegate void MyDelegate(int x);
Explain the concept of Events in C#.
Events are a way to provide notifications when a particular action occurs in your program.
What is the difference between a Delegate and an Event?
A Delegate is a function pointer, while an Event is a higher-level construct that provides a layer of encapsulation and protection.
How do you subscribe to an Event?
You use the += operator to add a method to the Event’s invocation list.
How can you prevent race conditions in Event handling?
By using a thread-safe approach like locking or using the Monitor class.
What is the significance of the sender and e parameters in an Event handler?
The sender parameter references the object that raised the event, and the e parameter contains additional event-specific information.
Can you explain the purpose of the EventHandler and EventHandler<T> delegates?</T>
These are predefined delegate types that are commonly used for event handling. The non-generic EventHandler takes sender and e parameters, while EventHandler<T> takes an additional event argument.</T>
How can you unsubscribe from an Event?
You use the -= operator to remove a method from the Event’s invocation list.
What is LINQ?
LINQ is a set of language extensions that allow you to perform queries on various data sources directly from C#.
Name the different types of LINQ.
LINQ to Objects, LINQ to SQL, LINQ to XML, LINQ to Entities, etc.
What is the purpose of the IEnumerable interface in LINQ?
IEnumerable is the base interface for all LINQ query operations. It provides an iterator for collections.
Explain the difference between Deferred Execution and Immediate Execution in LINQ.
Deferred Execution means that the query isn’t executed until the data is actually enumerated. Immediate Execution means the query is executed immediately when it’s defined.