I'm new in C#. I encountered such code-example:
namespace App1
{
delegate int Sum(int number);
class TestAnonymusMethod
{
static Sum m()
{
int result = 0; // is not zeroed between calls
Sum del = delegate (int number)
{
for (int i = 0; i <= number; i++)
result += i;
return result;
};
return del;
}
static void Main()
{
Sum del1 = m();
for (int i = 1; i <= 5; i++)
Console.WriteLine("Sum of {0} == {1}", i, del1(i));
Console.ReadKey();
}
}
}
Output is:
Sum of 1 == 1
Sum of 2 == 4
Sum of 3 == 10
Sum of 4 == 20
Sum of 5 == 35
As you see, local variable result
is not zeroed between calls. Is it "undefined behaviour"? Looks like it happens because of when scope of result
is closed, its life time is undefined.
But what are the rules of reusing alive entities in C#? Is it the rule - "to reuse always", or there are some cases, when new one will be created instead of reusing survived old one?
Is it "undefined behavior"?
No, it's well-defined behaviour - just not the behaviour you expect.
Looks like it happens because of when scope of result is closed, its life time is undefined.
Nope, the lifetime of result
is extended beyond the scope of m()
. From the C# 5 specification section 7.15.5.1:
When an outer variable is referenced by an anonymous function, the outer variable is said to have been captured by the anonymous function. Ordinarily, the lifetime of a local variable is limited to execution of the block or statement with which it is associated (ยง5.1.7). However, the lifetime of a captured outer variable is extended at least until the delegate or expression tree created from the anonymous function becomes eligible for garbage collection.
See more on this question at Stackoverflow