例如:
xxx方法 name=new xxx方法();
name["abc"]=1234;
name["eeee"]="sdfg";
----------------上面的只是给个类似结构的例子。
可以直接读取name["abc"]得到整数1234.
和直接读取name["eeee"]得到字符串"sdfg"有人知道吗?最好给个简单例子。。谢谢。。

解决方案 »

  1.   

    索引器。详见msdn
    http://msdn.microsoft.com/zh-cn/library/6x16t2tx(VS.80).aspx
      

  2.   

    使用索引    index  可以定义
      

  3.   

    Dictinary<int,string> d = new Dictionary<int,string>();
    d.add(123,"asdad");得到的话
    d(123)
      

  4.   

                Dictionary<int, string> d = new Dictionary<int, string>();
                d.Add(123, "123");
                d[123].ToString();
      

  5.   


    public class xxx
    {
        public string this[string s]
    {
    get
    {
    switch(s)
    {
    case "abc";
    return "1234";
    case "eeee"
    return "sdfg";
    }
    }
    }
    }或者
    public class xxx
    {
        public object this[string s]
    {
    get
    {
    switch(s)
    {
    case "abc";
    return 1234;
    case "eeee"
    return "sdfg";
    }
    }
    }
    }
      

  6.   


    public class xxx
    {
        public object this[string s]
        {
            get
            {
                switch (s)
                {
                    case "abc":
                        return 1234;
                    case "eeee":
                        return "sdfg";
                }
                return null;
            }
        }
    }
      

  7.   

    定义一个类,继承自Dictionary<TKey, TValue>:
    class MyClass : System.Collections.Generic.Dictionary<string, string>
    {}调用示例:
    MyClass myClass = new MyClass();
    myClass["aaa"] = "111";
    myClass["bbb"] = "222";Console.WriteLine(myClass["aaa"]);
    Console.WriteLine(myClass["bbb"]);