List.Add似乎重复条目。怎么了? [英] List.Add seems to be duplicating entries. What's wrong?

查看:110
本文介绍了List.Add似乎重复条目。怎么了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个类:

public class myClass
{
  public List<myOtherClass> anewlist = new List<myOtherClass>;

  public void addToList(myOtherClass tmp)
  {
    anewList.Add(tmp);
  }

}

所以我称之为addToList百次,每增加一个独特的项目列表中。我测试过我的项目,以证明我运行addToList方法之前,他们是独一无二的。我甚至把线测试TMP,以确保它是我所期待的。

So I call "addToList" a hundred times, each adding a unique item to the list. I've tested my items to show that before I run the "addToList" method, they are unique. I even put a line in to test "tmp" to make sure it was what I was expecting.

然而,当我这样做(让我们说MyClass的对象称为tmpClass):

However, when I do this (lets say myClass object is called tmpClass):

int i = tmpClass.anewList.Count();
for (int j = 0; j<i; j++)
{
   //write out each member of the list based on index j...
}

我得到完全相同的项目,它是被写进我的列表中的最后一个。就好像当我添加,我覆写我已经添加了最后一个项目的完整列表。

I get the same exact item, and it's the last one that was written into my list. It's as if when I add, I'm overwriting the entire list with the last item I've added.

帮助?这是没有意义的。
我也试过List.Insert,在那里我总是在年底或索引0仍然没有骰子插入。是的,我倍加来源索引我是正确的,当我做我的测试,我通过每个元素的索引。

Help? This makes no sense. I've also tried List.Insert, where I'm always inserting at the end or at index 0. Still no dice. Yes, I'm doubly source my indexing is correct and when I do my test I'm indexing through each of the elements.

:)

更新:
好吧,我尝试这样做,仍然有同样的问题:

UPDATE: Okay, I tried this and still had the same problem:

foreach(myOtherClass tmpC in tmpClass.anewList)
{    
    Console.WriteLine(tmpC.theStringInMyClass.ToString());
}

和还为每个100个项目中,我得到了相同的字符串输出......我知道我在做一些完全愚蠢的,但我不知道是什么呢。我仍然100%肯定,正确的字符串获得通过的开始。

and still for each of the 100 items, I got the same string output... I'm sure I'm doing something completely stupid, but I don't know what yet. I'm still 100% sure that the right string is getting passed in to begin with.

-Adeena

好吧,我尝试这样做,仍然有同样的问题:

Okay, I tried this and still had the same problem:

foreach(myOtherClass tmpC in tmpClass.anewList)
{
    Console.WriteLine(tmpC.theStringInMyClass.ToString());
}

和还为每个100个项目中,我得到了相同的字符串输出......我知道我在做一些完全愚蠢的,但我不知道是什么呢。我仍然100%肯定,正确的字符串获得通过的开始。

and still for each of the 100 items, I got the same string output... I'm sure I'm doing something completely stupid, but I don't know what yet. I'm still 100% sure that the right string is getting passed in to begin with.

-Adeena

推荐答案

鉴于你addToList方法的签名:

Given the signature of your addToList method:

public void addToList(myOtherClass tmp)
  {
    anewList.Add(tmp);
  }

时有可能在该方法的消费者,你实际上并没有创建一个新的实例?

Is is possible that in the consumer of that method, you aren't actually creating a new instance?

您说,您所呼叫addToList 100倍。 presumably,即在一个循环。在每次循环迭代,你需要创建myOtherClass的新实例,否则,您只是在更新同一个对象在内存中。

You said that you are calling addToList 100 times. Presumably, that is in a loop. At each loop iteration, you will need to create a new instance of "myOtherClass", otherwise, you'll just be updating the same object in memory.

例如,如果你这样做了下面,你将有同一对象的100份:

For example, if you do the below, you will have 100 copies of the same object:

myOtherClass item = new myOtherClass();

for(int i=0; i < 100; i++)
{
  item.Property = i;
  addToList(item);
}

不过,如果你的循环看起来像以下时,将正常工作:

However, if your loop looks like the below, it will work fine:

myOtherClass item = null;
for(int i=0; i < 100; i++)
{
  item = new myOtherClass();
  item.Property = i;
  addToList(item);
}

希望帮助!

这篇关于List.Add似乎重复条目。怎么了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆