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

Delegates as callback pattern in C Sharp (C#) - Interactive Code Practice

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 Callback that takes no parameters and returns void.

C Sharp (C#)
public delegate [1] Callback();
Drag options to blanks, or click blank then click option'
Astring
Bvoid
Cint
Dbool
Attempts:
3 left
💡 Hint
Common Mistakes
Using a return type other than void when the callback does not return a value.
2fill in blank
medium

Complete the code to declare a method named Process that accepts a Callback delegate as a parameter.

C Sharp (C#)
public void Process([1] callback) { callback(); }
Drag options to blanks, or click blank then click option'
Aint
Bstring
CCallback
DAction
Attempts:
3 left
💡 Hint
Common Mistakes
Using a type like int or string instead of the delegate type.
3fill in blank
hard

Fix the error in the code by completing the delegate invocation inside the Process method.

C Sharp (C#)
public void Process(Callback callback) { callback[1]; }
Drag options to blanks, or click blank then click option'
A()
B;
C{}
D[]
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to add parentheses when calling the delegate.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps strings to their lengths only if length is greater than 3.

C Sharp (C#)
var lengths = new Dictionary<string, int> { { [1], [2] } };
Drag options to blanks, or click blank then click option'
A"apple"
B"banana"
C"pear"
D"kiwi"
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid syntax for dictionary initialization.
5fill in blank
hard

Fill all three blanks to define and use a delegate as a callback that prints a message.

C Sharp (C#)
public delegate void [1]();

public void Run([2] callback) {
    callback[3];
}
Drag options to blanks, or click blank then click option'
ACallback
C()
DAction
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for delegate and parameter.
Forgetting parentheses when invoking the delegate.