views:

1685

answers:

6

Can someone point me out to some C# code examples or provide some code, where a Dictionary has been used as a property for a Class.

The examples I have seen so far don't cover all the aspects viz how to declare the dictionary as property, add, remove, and retrieve the elements from the dictionary.

A: 

You mean like a property bag ?

http://www.codeproject.com/KB/recipes/propertybag.aspx

Radu094
+10  A: 

sample code:

public class MyClass
{
  public MyClass()
  {
    TheDictionary = new Dictionary<int, string>();
  }

  // private setter so no-one can change the dictionary itself
  // so create it in the constructor
  public IDictionary<int, string> TheDictionary { get; private set; }
}

sample usage:

MyClass mc = new MyClass();

mc.TheDictionary.Add(1, "one");
mc.TheDictionary.Add(2, "two");
mc.TheDictionary.Add(3, "three");

Console.WriteLine(mc.TheDictionary[2]);
Hans Kesting
I would consider exposing the property as IDictionary<int, string> rather than the whole class.
Jeff Yates
ok, changed the property
Hans Kesting
+5  A: 

Here's a quick example

class Example {
  private Dictionary<int,string> _map;
  public Dictionary<int,string> Map { get { return _map } }
  public Example() { _map = new Dictionary<int,string>(); }
}

Some use cases

var e = new Example();
e.Map[42] = "The Answer";
JaredPar
+1 for 42 [15 chars]
Aistina
+1  A: 

An example...

public class Example
{
    public Dictionary<Int32, String> DictionaryProperty
    {
        get; set;
    }

    public Example()
    {
        DictionaryProperty = new Dictionary<int, string>();
    }
}

public class MainForm
{
    public MainForm()
    {
        Example e = new Example();

        e.DictionaryProperty.Add(1, "Hello");
        e.DictionaryProperty.Remove(1);
    }
}
Chalkey
+1  A: 

You could also look into indexers. (official MSDN documentation here)

class MyClass
{
    private Dictionary<string, string> data = new Dictionary<string, string>();

    public MyClass()
    {
        data.Add("Turing, Alan", "Alan Mathison Turing, OBE, FRS (pronounced /ˈtjʊ(ə)rɪŋ/) (23 June, 1912 – 7 June, 1954) was a British mathematician, logician, cryptanalyst and computer scientist.")
        //Courtesy of [Wikipedia][3]. Used without permission
    }

    public string this [string index]
    {
        get
        {
            return data[index];
        }
    }
}

Then, once you have populated the dictionary internally, you can access it's information by going

MyClass myExample = new MyClass();

string turingBio = myExample["Turing, Alan"];

EDIT

Obviously, this has to be used carefully, because MyClass is NOT a dictionary, and you cannot use any dictionary methods on it unless you implement them for the wrapper class. But indexers are a great tool in certain situations.

+1  A: 

Another example of using a dictionary as a static property with only the get accessor:

  private static Dictionary <string, string> dict = new  Dictionary   <string,string>(){            
            {"Design Matrix", "Design Case"},
            {"N/A", "Other"}
    };


    public static Dictionary <string, string> Dict
    {
        get { return dict}
    }          

This structure can be used to replace values.

Eric