SD Flashcards
What is a delegate in C#?
A delegate is a type that represents references to methods with a specific parameter list and return type.
How can you declare a delegate in C#?
You can declare a delegate using the delegate keyword followed by the return type and parameters of the method it will reference.
Can a delegate reference static methods?
Yes, a delegate can reference both instance methods and static methods.
What is a multicast delegate in C#?
A multicast delegate is a delegate that holds references to multiple methods.
How can you add methods to a multicast delegate in C#?
You can add methods to a multicast delegate using the += operator.
What is the purpose of the Invoke method in C# delegates?
The Invoke method is used to call the methods that the delegate references.
What is the difference between delegates and interfaces in C#?
Delegates are similar to interfaces but are used to create references to methods rather than defining a contract for classes.
Can delegates be used for event handling in C#?
Yes, delegates are commonly used for event handling in C#.
What is a callback in C#?
A callback is a method that is passed as an argument to another method and is called when a certain event occurs.
How can you define a delegate with a custom signature in C#?
You can define a delegate with a custom signature by specifying the return type and parameters that the delegate will reference.
Can delegates be used for asynchronous programming in C#?
Yes, delegates can be used for asynchronous programming in C# by using the BeginInvoke and EndInvoke methods.
What is covariance in relation to delegates in C#?
Covariance allows you to assign a method that returns a more derived type to a delegate that returns a less derived type.
What is contravariance in relation to delegates in C#?
Contravariance allows you to assign a method that accepts a less derived type as a parameter to a delegate that expects a more derived type.
How can you check if a delegate is null in C#?
You can check if a delegate is null by comparing it to null or using the Equality operator.
What is the difference between synchronous and asynchronous delegates in C#?
Synchronous delegates execute the methods immediately, while asynchronous delegates allow the methods to execute on a separate thread.