【发布时间】:2012-03-29 17:22:11
【问题描述】:
假设我有一个类似的课程
class ABC
{
int ID;
public string Name;
public ABC(int i, string str) { ID = i; Name = str; }
}
并声明一个ABC类的数组。 假设我还使用 new 关键字初始化了每个元素。 现在我想使用它的成员 ID 而不是基于 0 的索引来访问类的元素。如果需要,公开 ID。
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ABC[] a = new ABC[3];
a[0] = new ABC(100, "First");
a[1] = new ABC(101, "Second");
a[2] = new ABC(102, "Third");
ABC newobj = a[100]; // where 100 is ID of class ABC
}
}
class ABC
{
int ID;
string Name;
public ABC(int i, string str)
{
ID = i;
Name = str;
}
}
【问题讨论】:
-
那你为什么不把它们放到
Dictionary<int, ABC>呢? -
你能给我一个示例代码吗?我不知道如何在 .NET 中使用字典
-
我不能,但 Google 和 MSDN 可以。
-
请投票支持我的问题。我需要一些分数来投票给答案。