小编Mar*_*ela的帖子

List or Dictionary of Objects inside Class

A friend and I (both new in programming) were arguing if it was wise to include a Dictionary inside a Class that contains all instances of that class.

I say that is better that the dictionary is in the main instead in the class itself.

我没有充分的理由说出为什么,但是我从未见过有类记录已创建对象的记录。

您能给我每种方法的利弊吗?

提前致谢。

A:

    class Table
    {
      public static Dictionary<int,Table> Tables = new Dictionary<int, Table>();
     ...
     public table (int ID)  // constructor
     {
      ...
      Tables.Add(ID,this);
     }   
    }
Run Code Online (Sandbox Code Playgroud)

B:

     class Program
     {
      public Dictionary<int,Table> Tables = …
Run Code Online (Sandbox Code Playgroud)

c# dictionary list

7
推荐指数
2
解决办法
552
查看次数

标签 统计

c# ×1

dictionary ×1

list ×1