C# Language Indexer A simple indexer

30% OFF - 9th Anniversary discount on Entity Framework Extensions until December 15 with code: ZZZANNIVERSARY9

Example

class Foo
{
    private string[] cities = new[] { "Paris", "London", "Berlin" };

    public string this[int index]
    {
        get {
            return cities[index];
        }
        set {
            cities[index] = value;
        }
    }
}

Usage:

    var foo = new Foo();

    // access a value    
    string berlin = foo[2];

    // assign a value
    foo[0] = "Rome";

View Demo



Got any C# Language Question?