I’m experienced with C++, but a little new to C#.
When you add objects to a container, are they passed by reference or by value?
That is, if I do this:
myClass m = new myClass(0); //Assume the class just holds an int
List<myClass> myList = new List<myClass>(1);
myList.Add(m);
myList[0] += 1;
Console.WriteLine(m);
Console.WriteLine(myList[0]);
Will the result be:
0
1
or will it be
1
1
?
If the former, then how can get I make it do the latter? My first instinct was to do something like
myClass ref mref = m;
Console.WriteLine(mref);
But this doesn’t seem to be valid syntax.
The value is passed by value to the
Addmethod; 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.With this declaration, we get:
Note that this would not work with a
struct, because the value returned bymyList[0]would be a copy of the value stored in the list. The+= 1would only increment theNumberproperty 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
ToStringNow, you can write
You could even make
myList[0] += 1work with an operator overload. InMyClassdeclareBut this is a bit weird, unless your class represents a number, but in that case an immutable
structwould be preferred, as numbers are generally perceived as immutable value types.