问题
Having a user defined class, like this:
class Foo
{
public int dummy;
public Foo(int dummy)
{
this.dummy = dummy;
}
}
And having then something like this:
ArrayList dummyfoo = new ArrayList();
Foo a = new Foo(1);
dummyfoo.add(a);
foreach (Foo x in dummyfoo)
x.dummy++;
How much is a.dummy?
How can i create my ArrayList so that a.dummy equals 2, meaning that my ArrayList contains basically pointers to my objects and not copies.
回答1:
It is already 2, as Array/Collections (to be precise any .NET Class/reference type) are passed by reference by default.
In fact the reference variable is passed by value, but behaves as if passed by reference.
Why ->
Consider var arr = new ArrayList();
The above statement first creates an ArrayList object and a reference is assigned to arr. (This is similar for any Class as class are reference type).
Now at the time of calling,
example -> DummyMethod(arr) ,
the reference is passed by value, that is even if the parameter is assigned to a different object within the method, the original variable remains unchanged.
But as the variable points(refer) to same object, any operation done on underlying pointed object is reflected outside the called method.
In your example, any modification done in for each will be reflected in the arrayList.
If you want to avoid this behavior you have to create copy/clone of the object.
Example:
Instead of
foreach (Foo x in dummyfoo)
x.dummy++;
Use
foreach (Foo x in (ArrayList)dummyfoo.Clone())
x.dummy++;
回答2:
It already contains references, not copies. When doing this:
Foo a = new Foo(1);
dummyfoo.Add(a);
a reference to a is passed, not a copy.
Hence, dummy will be 1 initially and then 2 after the increments.
Anyway, you're better off using generics:
List<Foo> dummyfoo = new List<Foo>();
Foo a = new Foo(1);
dummyfoo.Add(a);
foreach (Foo x in dummyfoo)
x.dummy++;
回答3:
You declared Foo as a class. Classes are reference types. It already works like this. Give it a try:
class Program
{
static void Main(string[] args)
{
ArrayList dummyfoo = new ArrayList();
Foo a = new Foo(1);
dummyfoo.Add(a);
foreach (Foo x in dummyfoo)
x.dummy++;
Console.WriteLine(a.dummy); //Prints 2!
}
}
class Foo
{
public int dummy;
public Foo(int dummy)
{
this.dummy = dummy;
}
}
As an aside, a generic List<T> is preferred over the deprecated ArrayList type.
回答4:
It is already 2: Here is your code on ideone that verifies that..
Unlike value types (i.e. structs), reference (i.e. class) objects are passed by reference.
P.S. Generics are available since C#2.0, so consider using List<Foo> in place of ArrayList for improved type safety.
来源:https://stackoverflow.com/questions/10687601/arraylist-of-object-references