public A this[string key]

来源:互联网 发布:icsd数据库下载 编辑:程序博客网 时间:2024/03/29 18:26
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication13{    class Program    {              static void Main(string[] args)        {            C c = new C();            Console.Write(c["test2"].Name);        }    }    class A    {        private string name;        public A(string names)        {            name = names;        }        public string Name        {            get{return name;}        }    }    class C    {        public Dictionary<string, A> test = new Dictionary<string, A>();        public C()        {            test.Add("test",new A("testValues"));            test.Add("test2", new A("test2Values"));        }        public A this[string key]        {            get            {                A value = test.Where(q => q.Key == key).Select(q => q.Value).FirstOrDefault();  //get all keys                return value;            }        }    }}

原创粉丝点击