The value is passed by value to the Add
method; however, if you pass a reference type (a class is always a reference type), then the value itself is a reference. So the question is not so much whether the value is passed by value or by reference, but if the type is a value type or a reference type.
class MyClass
{
public int Number { get; set; }
}
With this declaration, we get:
MyClass m = new MyClass();
List<MyClass> myList = new List<MyClass>();
myList.Add(m);
myList[0].Number += 1;
Console.WriteLine(myList[0].Number); // Displays 1
Console.WriteLine(m.Number); // Displays 1
myList[0].Number += 1;
Console.WriteLine(myList[0].Number); // Displays 2
Console.WriteLine(m.Number); // Displays 2
Note that this would not work with a struct
, because the value returned by myList[0]
would be a copy of the value stored in the list. The += 1
would only increment the Number
property of this temporary copy and thus have no other effect than consuming a few processor cycles. Therefore it is a good advice to create only immutable structs.
If you want to display the object directly, override ToString
class MyClass
{
public int Number { get; set; }
public override string ToString()
{
return Number.ToString();
}
}
Now, you can write
myList[0].Number += 1;
Console.WriteLine(myList[0]);
Console.WriteLine(m);
You could even make myList[0] += 1
work with an operator overload. In MyClass
declare
public static MyClass operator +(MyClass m, int i)
{
m.Number += i;
return m;
}
But this is a bit weird, unless your class represents a number, but in that case an immutable struct
would be preferred, as numbers are generally perceived as immutable value types.
public static MyStruct operator +(MyStruct m, int i)
{
return new MyStruct(m.Number + i);
}
1
solved C# containers initialized by reference or value?