Hello, please explain why an error occurs:
A field initializer cannot refer to the non-static field, method, or property GenericsHW.MyList.array.
Indeed, in the Contains(T item) method, exactly the same cycle is organized, but there is no error; what is it connected with? Moreover, if I make the array static, then everything is OK (why do I need to write static?). Thank you in advance :)
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); } }; } }