C#ArrayList.Add方法将引用添加到项目吗? [英] C# ArrayList.Add Method Adds Ref to Items?

查看:209
本文介绍了C#ArrayList.Add方法将引用添加到项目吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

0假设我创建了一个ArrayList并添加了两个字符串,如下所示:

0Suppose I have created an ArrayList and added two strings as such:

string item1 = string.Empty;
string item2 = string.Empty;

ArrayList someList = new ArrayList();
someList.Add(item1);
someList.Add(item2);

someFunc(someList);

然后我获取someList并将其传递给方法.该方法对item1和item2进行一些操作并返回,如下所示:

Then I take someList and pass it into a method. That method performs some manipulation on item1 and item2 and returns, as such:

public void someFunc(ArrayList parameters)
{
    parameters[0] = "Monkeys";
    parameters[1] = "More Monkeys";
}

我希望当someFunc返回时item1等于"Monkeys",item2等于"More Monkeys",因为someList分别在索引0和1中包含对item1和item2的引用.但是,我看到的是item1等于猴子",item2等于".

I would expect that when someFunc returns that item1 equals "Monkeys" and item2 equals "More Monkeys" as someList contains references to item1 and item2 in indices 0 and 1 respectively. However, what I am seeing is that item1 equals "Monkeys" and item2 equals "".

为什么?而我如何在不通过ref单独传递每个项目的情况下执行此壮举呢?

Why? And how would I perform this feat without passing in each item individually by ref?

推荐答案

您的代码将根本无法满足您的期望.重新分配到列表将不会修改变量item1item2.您不能将对变量 引用添加到ArrayList(或者最好是 List<T> 在现代.NET世界中).您可以简单地添加值本身(可以是对对象 引用).当您覆盖最初存储在列表中的值时,它不会影响先前的变量.

Your code will simply not do what you expect. Your variables item1 and item2 will not be modified by reassigning to the list. You cannot add a reference to a variable to an ArrayList (or preferably a List<T> in the modern .NET world). You can simply add the value itself (which could be a reference to an object). When you overwrite the value that was originally stored in the list, it does not impact the previous variable.

如果需要在列表中进行更新以反映外部变量,则可能需要不同的抽象.例如,您将变量设置为类的属性,然后维护该类的列表.然后,您可以通过更改属性来更改类,而不是覆盖列表的内容. Niyoko Yuliawan的答案中对此技术进行了演示,因此我将不再显示它.

If you need updates inside the list to reflect on outer variables, you may need a different abstraction. You make your variables properties of a class, for example, and then maintain a list of that class. And instead of overwriting the contents of your list, you could then mutate the class by changing the property. This technique is demonstrated in Niyoko Yuliawan's answer, so I will not redisplay it.

提到的另一种技术是将值读回到变量中.如果列表以相同顺序具有相同数量的值,则这是可行的.但是,我应该注意,如果要向列表中添加多个值,将它们传递给方法,更改列表中的值,然后将这些值重新读入变量中,您可能不需要列表,您需要一个类.例如,如果item1item2确实是 firstNamelastName,则您不希望ArrayList list(或者再次优选List<string> list),您真的想要具有string FirstNamestring LastName属性的Person person.

Another technique mentioned is to read the values back into your variables. If the list has the same number of values in the same order, this is doable. However, I should note that if you are adding multiple values to a list, passing them into a method, changing the values in the list, and then rereading those values into your variables, you probably did not need a list, you needed a class. If item1 and item2 are really firstName and lastName, for example, you don't want ArrayList list (or, again, preferably List<string> list), you really want Person person that has string FirstName and string LastName properties.

所以,如果您有

public void SomeFunc(ArrayList list)
{
     list[0] = "Sam";
     list[1] = "Smith";
}

您可能应该考虑改为定义一个类,然后使用该类.

You probably should consider instead defining a class and then working with that class.

public class Person 
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

// elsewhere in your code...

public void SomeFunc(Person person)
{
     person.FirstName = "Sam";
     person.LastName = "Smith";
}

这篇关于C#ArrayList.Add方法将引用添加到项目吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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