1/11
Flashcards covering core concepts of delegates, generic delegates, events, and the difference between delegates and interfaces as presented in the IT1811 handout. Each card poses a question about a concept and provides the concise definition or explanation.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What is a delegate in .NET?
A class that encapsulates a method; a reference type that holds a reference to a method and can call any method whose signature matches.
How do you declare a delegate in C#?
Use the delegate keyword with an access modifier, return type, name, and parameters: delegate (). Example: public delegate int mathOp(int x, int y);
How do you instantiate a delegate?
Using the new keyword with a method reference, or by assigning a method group
How do you invoke a delegate?
Call it like a method (e.g., delegateAddition(10, 20)); If it has a return value, use the returned result.
What is a generic delegate?
A delegate not bound to a specific type and can reference methods with parameters and return types of a generic type X (e.g., public delegate X DisplayOutput
What is a key difference between delegates and interfaces?
Delegates are declarations used for callbacks with no implementation; interfaces declare methods that a class must implement.
What is an event in C#?
A member of a class that fires when a specific action occurs; events are linked to delegates and invoke registered event handlers when raised.
Why must a delegate be declared before declaring an event?
Because an event is declared with a delegate type; the event signature must match a delegate.
What is the syntax to declare an event?
public event
How do you subscribe to an event?
Using the += operator: event_name += delegate_instance;
What are event accessors and what do they do?
The add and remove accessors; the compiler generates them when you subscribe; they handle registering and unregistering event handlers and can include custom logic.
What does the example MessageBox.Show(delegateAddition(10,20).ToString()) demonstrate?
Displaying the result of invoking a delegate by converting it to a string for output.