Ошибка "A field initializer cannot reference the non-static field, method, or property" - C#
Формулировка задачи:
Здравствуйте,объясните пожалуйста,почему возникает ошибка:"A field initializer cannot reference the non-static field, method, or property 'GenericsHW.MyList<T>.array' ".Ведь в методе Contains(T item) организован точно такой же цикл,но ошибки нет;с чем это связано?При чём если я сделаю array статическим,то всё ок(почему нужно писать static?).
Заранее спасибо
namespace GenericsHW
{
interface IMyList<T>
{
void Add(T a);
T this[int index] { get; }
int Count { get; }
void Clear();
bool Contains(T item);
}
class MyList<T>:IMyList<T>
{
T[] array = null;
delegate void ShowArrayElements();
public MyList()
{
array = new T[0];
}
public int Count
{
get { return array.Length; }
}
public void Add(T a)
{
T[] temp = new T[array.Length + 1];
for (int i = 0; i < array.Length; i++)
temp[i] = array[i];
temp[array.Length] = a;
array = temp;
}
public T this[int index]
{
get { return array[index]; }
}
public void Clear()
{
array = new T[0];
}
public bool Contains(T item)
{
foreach (T ar in array)
{
if (ar.Equals(item))
return true;
}
return false;
}
public override string ToString()
{
return String.Format("Количество элементов массива:{0}\n", array.Length);
}
private ShowArrayElements showArray = () =>
{
foreach (T ar in array)
{
Console.WriteLine("{0} ",ar);
}
};
}
}Решение задачи: «Ошибка "A field initializer cannot reference the non-static field, method, or property"»
textual
Листинг программы
private ShowArrayElements showArray => () =>
{
foreach (T ar in array)
{
Console.WriteLine("{0} ", ar);
}
};