In the following code, why is it necessary for the array (arr) to be static ?
If I remove static, then it is no more visible for using in the LINQ query.
class A
{
static int[] arr = { 1, 2, 3, 4 };
IEnumerable<int> result = from i in arr where i < 10 select i;
}
Thanks.
You cannot access other class instance variables if you directly initialize variables using a variable initializer – you could move the code to the constructor instead:
From the C# spec, 10.5.5.2 Instance field initialization:
This makes sense, since variable initializers are executed before the base class constructor, hence the class instance has not been fully “constructed” yet.