In C# language and .NET framework, could you help me with understanding delegates? I was trying to check some code, and found that the results I received were unexpected for me.
You need to pass in I
to your function so that I.ToString()
can be executed at the appropriate time (instead of at the time function is created).
class Program
{
public static int I = 0;
static Func del = num => num.ToString();
static void Main(string[] args)
{
I = 10;
Console.WriteLine("{0}", del(I));
}
}