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.
How can you remove a method from a multicast delegate in C#?
You can remove a method from a multicast delegate using the -= operator.
What is the purpose of the GetInvocationList method in C# delegates?
The GetInvocationList method returns an array of delegates that represent the methods currently referenced by a multicast delegate.
Can delegates be used with lambda expressions in C#?
Yes, delegates can be used with lambda expressions to create inline methods.
What is the difference between a delegate and an event in C#?
An event is a special type of delegate that can only be invoked from within the class that defines it.
How can you create a generic delegate in C#?
You can create a generic delegate by using the generic type parameters when declaring the delegate.
What is the purpose of the DynamicInvoke method in C# delegates?
The DynamicInvoke method is used to invoke the methods that the delegate references with parameters passed as an object array.
Can delegates be used with anonymous methods in C#?
Yes, delegates can be used with anonymous methods to define inline methods without a separate method declaration.
What is the difference between a delegate and a function pointer?
Function pointers are a feature of C and C++, while delegates are a type-safe object-oriented way to refer to methods in C#.
How can you pass a delegate as a parameter in C#?
You can pass a delegate as a parameter by declaring the parameter as the delegate type that the method expects.