Untitled
unknown
plain_text
a year ago
1.1 kB
2
Indexable
Never
public class MyCollection { private int[] data; public MyCollection(int size) { data = new int[size]; } // Custom indexer public int this[int index] { get { if (index >= 0 && index < data.Length) { return data[index]; } else { throw new IndexOutOfRangeException(); } } set { if (index >= 0 && index < data.Length) { data[index] = value; } else { throw new IndexOutOfRangeException(); } } } } class Program { static void Main() { MyCollection collection = new MyCollection(5); // Using the custom indexer to set and get values collection[0] = 10; collection[1] = 20; collection[2] = 30; collection[3] = 40; collection[4] = 50; Console.WriteLine(collection[2]); // Output: 30 } }