初始化多个List <boolean> vs多个List <string> </string> </boolean>

时间:2015-02-18 08:32:03

标签: c# .net list

我以前在我的代码中有以下类

public class SomeListClass
{ 
       public List<Boolean> ListA {get; set;}
       public List<Boolean> ListB {get; set;}

       public List<String> ListStringsA {get; set;}
       public List<String> ListStringsB {get; set;}
       public List<String> ListStringsC {get; set;}
       public List<String> ListStringsD {get; set;}

       public void InitLists()
       {
            ListStringsA  = ListStringsB = ListStringsC = ListStringsD = new List<String>();
            ListA = ListB = new List<Boolean>();
       }
}

我注意到使用此初始化和以下代码:

SomeListClass itm = new SomeListClass();
itm.InitLists();
itm.ListStringsA.Add("TestString");
itm.ListA.Add(true);
  • 如果我向ListStringsA添加字符串,则只会将其添加到ListStringsA
  • 如果我向ListA添加一个布尔值,则它会自动添加到ListB

结果将是:

ListStringsA = {"TestString"}
ListStringsB = {}
ListStringsC = {}
ListStringsD = {}
ListA  = {true}
ListB = {true}

这是什么原因?有人能指点我一些解释这种奇怪行为的文件吗?我似乎无法找到任何东西。

注意:这解决了它:

   public void InitLists()
   {
        ListStringsA = ListStringsB = ListStringsC = ListStringsD = new List<String>();
        ListA = new List<Boolean>();
        ListB = new List<Boolean>();
   }

注意:有些简化了生产代码:

我得到一个名为SomeListObject

objectList类型列表(见下文)
public class SomeListObject {
    public String SomeListObjectName {get; set;}
    public Boolean SomeListObjectNeeded {get; set;}
    public Boolean SomeListObjectOk {get; set;}
}

该列表包含2个项目:

objectList = {{SomeListObjectName  = "PersonA", SomeListObjectNeeded  = true, SomeListObjectOk = false},{SomeListObjectName  = "PersonB", SomeListObjectNeeded  = true, SomeListObjectOk = false}}

然后我做了一个foreach:

SomeListClass itm = new SomeListClass();
itm.InitLists();
foreach (SomeListObject  myObj in objectList )
{
      ListStringsA.Add(myObj.SomeListObjectName);
      // ListStringsA: {"PersonA"}
      // ListStringsB: {}
      // ListA: {}
      // ListB: {}
      ListA.Add(myObj.SomeListObjectNeeded);
      // ListStringsA: {"PersonA"}
      // ListStringsB: {}
      // ListA: {true}
      // ListB: {true}
      ListB.Add(myObj.SomeListObjectNeeded);
      // ListStringsA: {"PersonA"}
      // ListStringsB: {}
      // ListA: {true, false}
      // ListB: {true, false} <=== Say What now???

}

3 个答案:

答案 0 :(得分:5)

这是因为您只为boolstring创建了一个列表,然后只将其分配给变量。 See more

如果要为每个属性创建新列表,请参阅:

ListStringsA = new List<string>(); ListStringsB = new List<string>(); ListStringsC = new List<string>(); ListA = new List<bool>(); ListB = new List<bool>();

答案 1 :(得分:3)

你确定吗?我想结果应该是

ListStringsA = {"TestString"}
ListStringsB = {"TestString"}
ListStringsC = {"TestString"}
ListStringsD = {"TestString"}
ListA  = {true}
ListB = {true}

答案 2 :(得分:1)

亲自尝试

SomeList mysomelist = new SomeList();

mysomelist.InitLists();

mysomelist.ListA.Add(true);
mysomelist.ListStringsA.Add("a");

Console.WriteLine(mysomelist.ListA[0] + "   " + mysomelist.ListB[0]);
Console.WriteLine(mysomelist.ListStringsA[0] + "   " + mysomelist.ListStringsB[0] + "  " + mysomelist.ListStringsC[0] + "  " + mysomelist.ListStringsD[0]);

输出:

True    True 
a    a    a    a

编辑:可能您正在尝试的不是问题中所述的内容