0
0
C Sharp (C#)programming~10 mins

Why delegates are needed in C Sharp (C#) - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a delegate named MyDelegate that takes no parameters and returns void.

C Sharp (C#)
public delegate [1] MyDelegate();
Drag options to blanks, or click blank then click option'
Aint
Bbool
Cstring
Dvoid
Attempts:
3 left
💡 Hint
Common Mistakes
Using a return type other than void when the delegate should not return a value.
2fill in blank
medium

Complete the code to create an instance of the delegate MyDelegate that points to the method ShowMessage.

C Sharp (C#)
MyDelegate del = new MyDelegate([1]);
Drag options to blanks, or click blank then click option'
AShowMessage()
BShowMessage
CShow_Message
DshowMessage
Attempts:
3 left
💡 Hint
Common Mistakes
Including parentheses when assigning the method to the delegate.
Using incorrect method name or casing.
3fill in blank
hard

Fix the error in the delegate invocation by completing the code to call the delegate instance del.

C Sharp (C#)
del[1];
Drag options to blanks, or click blank then click option'
A[]
B;
C()
D{}
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to use parentheses to invoke the delegate.
4fill in blank
hard

Fill both blanks to declare a delegate that takes an int parameter and returns a string.

C Sharp (C#)
public delegate [1] MyDelegate([2] number);
Drag options to blanks, or click blank then click option'
Astring
Bint
Cvoid
Dbool
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up return type and parameter type.
Using void as return type when a string is expected.
5fill in blank
hard

Fill all three blanks to create a delegate instance that points to the method GetMessage and invoke it.

C Sharp (C#)
MyDelegate del = new [1]([2]);
string result = del[3];
Drag options to blanks, or click blank then click option'
AMyDelegate
BGetMessage
C()
DShowMessage
Attempts:
3 left
💡 Hint
Common Mistakes
Using method name with parentheses when creating the delegate.
Forgetting parentheses when invoking the delegate.