I'm referring to some code from the book [C# concurrency in action], the chapter has following code snippet:
int _v1;
readonly Lazy<int> Myv1 = new Lazy<int>(() => _v1++);//error
void UseV1()
{
Console.WriteLine(Myv1.Value);
}
int _v2;
readonly Lazy<Task<int>> Myv2 = new Lazy<Task<int>>(async () =>//error
{
await Task.Delay(2).ConfigureAwait(false);
return _v2++;
});
It doesn't compile, saying that
error CS0236: A field initializer cannot reference the non-static field, method, or property 'Program._v1'
Why this rule happens, how to fix or get around this problem? Thanks a lot.