如何在C#中为每个类实例分配一个整数ID? [英] How to assign an integer ID to each class instace in C#?
问题描述
假设我有几个班级的实例
公共班级AB
{
}
在main我创建AB类实例
AB a = new AB();
AB b = new AB();
AB c = new AB();
......
i想要将每个类实例映射到一个整数ID。我怎么能在c#中做到这一点?
我尝试了什么:
我试过枚举。和字典。字典的问题是,如果我改变实例的顺序,那么即使ID也会改变。如果ID为0,我需要类似
的东西,如果ID为1,则需要使用
,如果ID为2则使用b
,c应该使用等等
但是我不想使用if / else和switch
< blockquote>你可以使用一个数组:
private AB [] AllInstances = new AB [ 3 ];
...
AB a = new AB();
AllInstances [ 0 ] = a;
AB b = new AB();
AllInstances [ 1 ] = b;
AB c = new AB();
AllInstances [ 2 ] = c;
然后您只需按ID访问数组作为索引:
AB it = AllInstances [ID];
或者,您可以将ID值作为属性添加到AB类,并且添加静态集合:
private static 列表< AB> AllInstances = new 列表< AB>
然后在AB构造函数中将其添加到集合中:
public AB( int id)
{
ID = ID;
AllInstances.Add( this );
}
您只需要使用Linq来获取实例:
public AB Get( int id)
{
return AllInstances.Where(ab = > ab.ID == id).FirstOrDefault();
}
字典的问题是,如果我改变实例的顺序,那么即使是ID更改。该声明没有意义。虽然你应该永远不要依赖在一个字典中的KeyValuePairs中,然后将它们放入(或者,任何顺序),使用字典保证......事实上它不允许重复的密钥...每个密钥都是唯一的。
我认为(猜测)你想要的是每个密钥的唯一id(int)类的实例;你能做到的一种方法是:使用 System.Collections.Generic;
命名空间 YourNameSpace
{
public class AB
{
private static int AB_ID = 0 ;
public static 字典< AB,int> ABInstanceToID = new 字典< AB,int>();
public int ID {设置跨度>; get ; }
public AB()
{
this .ID = AB_ID;
ABInstanceToID.Add( this , this .ID);
AB_ID ++;
}
}
}因此,给定AB的运行时实例,您可以通过在静态字典中进行查找来读取其ID:
if (AB.ABInstanceToID.ContainsKey(someAB))
{
int index = AB.ABInstanceToID [someAB];
switch (index)
{
case 0 :
// 句柄O
break ;
默认:
break ;
}
}
else
{
// 抛出错误?
// 使用MessageBox发出警告 ?>
}注意:
1.in理论'ContainsKey测试不应该是必要的,但我认为检查和重新检查以及计划错误总是一个好主意。 br />
2.虽然有些人可能更喜欢使用Dictionary.TryGetValue进行检查,在这种情况下,我们可以假设经常尝试查找丢失的密钥是非常不可能的,我认为没有理由在这里使用它。
试试这个
AB a = new AB();
AB b = new AB();
AB c = new AB();
AB d = new AB();
AB e = new AB();
AB [] array = new AB [ 5 ];
array [ 0 ] = a;
array [ 1 ] = b;
array [ 2 ] = c;
array [ 3 ] = d;
array [ 4 ] = e;
int id = 2 ;
var instance = array [id]; // 获取c的实例
lets say i have few instances of the class
public class AB
{
}
in main i create instances of class AB
AB a = new AB();
AB b = new AB();
AB c = new AB();
......
i want to map each class instance to an integer ID. How can i do that in c#?
What I have tried:
I tried enums. and dictionary. The problem with dictionary is that if i change the order of the instances, then even the ID changes. I want something like
if ID is 0, a should be used
if ID is 1, b should be used
if ID is 2, c should be used and so on
but i dont want to use if/else and switch
You could use an array:
private AB[] AllInstances = new AB[3]; ... AB a = new AB(); AllInstances[0] = a; AB b = new AB(); AllInstances[1] = b; AB c = new AB(); AllInstances[2] = c;Then you just access the array by ID as an index:
AB it = AllInstances[ID];
Alternatively, you could add the ID value to your AB class as a property, and add a static collection:
private static List<AB> AllInstances = new List<AB>Then in the AB constructor add it to the collection:
public AB(int id) { ID = id; AllInstances.Add(this); }All you need then is to use Linq to get the instance:
public AB Get(int id) { return AllInstances.Where(ab => ab.ID == id).FirstOrDefault(); }
"The problem with dictionary is that if i change the order of the instances, then even the ID changes."That statement does not make sense. While it is correct that you should never rely on the KeyValuePairs in a Dictionary being in the same "order" you put them in (or, any order), using a Dictionary guarantees ... by virtue of the fact it does not allow duplicate Keys ... that each key is unique.
I think (guess) what you want is a unique id (int) for each instance of the Class; one way you can do that is:using System.Collections.Generic; namespace YourNameSpace { public class AB { private static int AB_ID = 0; public static Dictionary<AB, int> ABInstanceToID = new Dictionary<AB, int>(); public int ID { set; get; } public AB() { this.ID = AB_ID; ABInstanceToID.Add(this, this.ID); AB_ID++; } } }So, given a run-time instance of AB, you can read its ID by doing a lookup in the static Dictionary:
if(AB.ABInstanceToID.ContainsKey(someAB)) { int index = AB.ABInstanceToID[someAB]; switch(index) { case 0: // handle case O break; default: break; } } else { // throw an error ? // put up a warning with a MessageBox ?> }Note:
1.in "theory" the 'ContainsKey test should not be necessary, but I think it's always a good idea to check and re-check, and plan for errors.
2. while some might favor using Dictionary.TryGetValue for the check, in this case, where we can assume frequent attempts at look-up of missing Keys is very improbable, I see no reason to use that, here.
try this
AB a = new AB(); AB b = new AB(); AB c = new AB(); AB d = new AB(); AB e = new AB(); AB[] array = new AB[5]; array[0] = a; array[1] = b; array[2] = c; array[3] = d; array[4] = e; int id = 2; var instance = array[id]; // gets instance for c
这篇关于如何在C#中为每个类实例分配一个整数ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!