SD Flashcards

1
Q

What is a delegate in C#?

A

A delegate is a type that represents references to methods with a specific parameter list and return type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can you declare a delegate in C#?

A

You can declare a delegate using the delegate keyword followed by the return type and parameters of the method it will reference.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Can a delegate reference static methods?

A

Yes, a delegate can reference both instance methods and static methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a multicast delegate in C#?

A

A multicast delegate is a delegate that holds references to multiple methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can you add methods to a multicast delegate in C#?

A

You can add methods to a multicast delegate using the += operator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the purpose of the Invoke method in C# delegates?

A

The Invoke method is used to call the methods that the delegate references.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the difference between delegates and interfaces in C#?

A

Delegates are similar to interfaces but are used to create references to methods rather than defining a contract for classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Can delegates be used for event handling in C#?

A

Yes, delegates are commonly used for event handling in C#.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a callback in C#?

A

A callback is a method that is passed as an argument to another method and is called when a certain event occurs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How can you define a delegate with a custom signature in C#?

A

You can define a delegate with a custom signature by specifying the return type and parameters that the delegate will reference.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Can delegates be used for asynchronous programming in C#?

A

Yes, delegates can be used for asynchronous programming in C# by using the BeginInvoke and EndInvoke methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is covariance in relation to delegates in C#?

A

Covariance allows you to assign a method that returns a more derived type to a delegate that returns a less derived type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is contravariance in relation to delegates in C#?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How can you check if a delegate is null in C#?

A

You can check if a delegate is null by comparing it to null or using the Equality operator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the difference between synchronous and asynchronous delegates in C#?

A

Synchronous delegates execute the methods immediately, while asynchronous delegates allow the methods to execute on a separate thread.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How can you remove a method from a multicast delegate in C#?

A

You can remove a method from a multicast delegate using the -= operator.

17
Q

What is the purpose of the GetInvocationList method in C# delegates?

A

The GetInvocationList method returns an array of delegates that represent the methods currently referenced by a multicast delegate.

18
Q

Can delegates be used with lambda expressions in C#?

A

Yes, delegates can be used with lambda expressions to create inline methods.

19
Q

What is the difference between a delegate and an event in C#?

A

An event is a special type of delegate that can only be invoked from within the class that defines it.

20
Q

How can you create a generic delegate in C#?

A

You can create a generic delegate by using the generic type parameters when declaring the delegate.

21
Q

What is the purpose of the DynamicInvoke method in C# delegates?

A

The DynamicInvoke method is used to invoke the methods that the delegate references with parameters passed as an object array.

22
Q

Can delegates be used with anonymous methods in C#?

A

Yes, delegates can be used with anonymous methods to define inline methods without a separate method declaration.

23
Q

What is the difference between a delegate and a function pointer?

A

Function pointers are a feature of C and C++, while delegates are a type-safe object-oriented way to refer to methods in C#.

24
Q

How can you pass a delegate as a parameter in C#?

A

You can pass a delegate as a parameter by declaring the parameter as the delegate type that the method expects.