// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections; using System.Collections.Generic; namespace E { public class ArrayOne : IEnumerable { private T[] a; public ArrayOne(int nelm) { a = new T[nelm]; } public T this[int ix] { get { return a[ix-1]; } set { a[ix-1] = value; } } public int Length { get { return a.Length; } } public static implicit operator T[](ArrayOne input) { return input.a; } public IEnumerator GetEnumerator() { foreach(T e in a) { yield return e; } } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } public class Program { public static void Test1(ArrayOne a) { for(int i = 1; i <= a.Length; i++) { Console.WriteLine(a[i]); } foreach(int v in a) { Console.WriteLine(v); } } public static void Test2(int[] a) { for(int i = 0; i < a.Length; i++) { Console.WriteLine(a[i]); } foreach(int v in a) { Console.WriteLine(v); } } public static void Main(string[] args) { ArrayOne a = new ArrayOne(3); for(int i = 1; i <= a.Length; i++) { a[i] = i; } for(int i = 1; i <= a.Length; i++) { Console.WriteLine(a[i]); } foreach(int v in a) { Console.WriteLine(v); } Test1(a); Test2(a); } } }