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;
}
}
}
}
public A this[string key]
最新推荐文章于 2024-03-25 06:00:00 发布
本文介绍了一个简单的C#程序示例,演示了如何使用泛型字典类Dictionary来存储和检索自定义对象。具体包括创建字典实例、添加键值对以及通过键获取值的过程。
21万+

被折叠的 条评论
为什么被折叠?



