How to

use indexers

Published: 3. November 2011 | Updated: 3. November 2011
License: Microsoft Public License (MS-PL)
Categories: Framework
Tags: C#
Was this snippet helpful for you? YESYES / NONO

Import namespace

using System.Collections.Generic;

Example

public class MyClass
{
    private Dictionary<int, string> _values = new Dictionary<int, string>();

    // indexer
    public string this[int i]
    {
        get
        {
            return _values[i];
        }
        set
        {
            _values[i] = value;
        }
    }
}

Use

var obj = new MyClass();
obj[1] = "test";
Send us feedback about this snippet »



Related Snippets: