using System.Collections.Generic; ... class Client { private Dictionaryinvoices = new Dictionary (); public void Add (int id, string value) { invoices.Add (id, value); } public string this [int id] // indexer { get { return invoices[id]; } set { invoices[id] = value; } } } public class Test { public static void Main( ) { Client client = new Client(); client.Add (1, "I005238A"); Console.WriteLine ("Invoice: " + client[1]); // indexer access } }
Search
Create an indexer property
This C# code snippet creates an indexer property which can be used to access the class as if it were an array. The Dictionary is not required, but is used only as example. Any class which can be viewed logically as an array could have an indexer.
Subscribe to:
Posts
(
Atom
)