user1675891
user1675891

Reputation:

Arguments on properties

Assuming there's an enumeration defined as follows:

public enum Beep
{
  HeyHo,
  LetsGo
}

I wonder if it's possible to improve the following property:

public Dictionary<Beep, String> Stuff{ get; set; }
...
String content = Stuff[Beep.HeyHo]

because the way it's right now, I retrieve the dictionary and then pick out the element I need. I wonder if it's (a) possible at all and if so (b) recommended to create something like this pseudo-code.

public String Stuff{ get<Beep>; set<Beep>; }
...
String content = Stuff[Beep.HeyHo]

Upvotes: 0

Views: 90

Answers (3)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112259

You can also use an indexer.

class MyClass
{
    private readonly Dictionary<Beep, string> _stuff = new Dictionary<Beep, string>();

    public string this[Beep beep]
    {
        get { return _stuff[beep]; }
        set { _stuff[beep] = value; }
    }
}

Now, instead of calling

var obj = new MyClass();
string result = obj.Stuff[Beep.HeyHo];

You can call

var obj = new MyClass();
string result = obj[Beep.HeyHo];

Indexers work much like properties but have at least one argument used as index. You can have only one indexer per class, however you can create different overloads of it. The same overloading rules apply as for methods.

Upvotes: 1

Parimal Raj
Parimal Raj

Reputation: 20565

Something like this using Indexer

public class Stuff
{
    public Dictionary<Beep, String> _stuff { get; set; }
    public enum Beep
    {
        HeyHo,
        LetsGo
    }

    public Stuff()
    {
        _stuff = new Dictionary<Beep, string>();
        // add item
        _stuff[Beep.HeyHo] = "response 1";
        _stuff[Beep.LetsGo] = "response 2";
    }

    public string this[Beep beep]
    {
        get { return _stuff[beep]; }
    }
}

Sample Usage :

public static class Program
{
    private static void Main()
    {
        Stuff stuff = new Stuff();

        string response;
        response = stuff[Stuff.Beep.HeyHo]; // response 1
        response = stuff[Stuff.Beep.LetsGo]; // response 2

    }
}

Upvotes: 0

RB.
RB.

Reputation: 37172

You can apply an indexer to your class.

It is recommended, as it improves encapsulation. For example, it's perfectly possible using the original code to replace the Dictionary entirely with a different dictionary - which is probable not desirable.

public class MyClass
{
    // Note that dictionary is now private.
    private Dictionary<Beep, String> Stuff { get; set; }

    public String this[Beep beep]
    {
        get
        {
            // This indexer is very simple, and just returns or sets 
            // the corresponding element from the internal dictionary. 
            return this.Stuff[beep];
        }
        set
        {
            this.Stuff[beep] = value;
        }
    }

    // Note that you might want Add and Remove methods as well - depends on 
    // how you want to use the class. Will client-code add and remove elements,
    // or will they be, e.g., pulled from a database?
}

Usage:

MyClass myClass = new MyClass();
string myValue = myClass[Beep.LetsGo];

Upvotes: 2

Related Questions