Search code examples
c#classthisinstant

Using this[] keyword to retrive a value from a class instant


In the following example this[] is used within a class to obtain a value from somewhere from the class instant. Where is this value stored? Is it likely the [] operator being overloaded or is this just c# syntax I am unfamiliar with?

public class MyUserSettings : ApplicationSettingsBase
{
    [UserScopedSetting()]
    [DefaultSettingValue("white")]
    public Color BackgroundColor
    {
        get
        {
            return ((Color)this["BackgroundColor"]);
        }
        set
        {
            this["BackgroundColor"] = (Color)value;
        }
    }
}

Solution

  • That is called the indexer. It is very similar to overloading operators for a class. Somewhere else in that class, or in one of that classes base classes, an indexer is defined.