Home All Groups Group Topic Archive Search About

by reference and by value

Author
9 Mar 2006 10:27 PM
Andrew Bullock
Hi,

Why do i get the following error? In other projects I have done (what i
thought was) the same thing, without compiler complaint :-/


1: List<MyClass> myClasses = new List<MyClass>();
2: myClasses.Add(new MyClass(20));
3: myClasses[0].value = 0;


Line 3: Cannot modify the return value of
System.Collections.Generic.List<OPS.MyClass>.this[int]' because it is
not a variable



And why does this print 20, and not 0? Again I thought i had done this
in other applications without this happening


List<MyClass> myClasses= new List<MyClass>();
myClasses.Add(new MyClass(20));
MyClass m = myClasses[0];
m.value = 0;

// prints "20"
Console.WriteLine(myClasses[0].value);


Thanks


Andrew

Author
9 Mar 2006 10:36 PM
Jon Skeet [C# MVP]
Andrew Bullock <andrewREMOVEbullockT***@ANDntlworldTHIS.com> wrote:
> Why do i get the following error? In other projects I have done (what i
> thought was) the same thing, without compiler complaint :-/
>
> 1: List<MyClass> myClasses = new List<MyClass>();
> 2: myClasses.Add(new MyClass(20));
> 3: myClasses[0].value = 0;
>
> Line 3: Cannot modify the return value of
> System.Collections.Generic.List<OPS.MyClass>.this[int]' because it is
> not a variable

My guess is that despite its name, "MyClass" is a struct. Fields in a
struct which are referenced via a non-variable expression (a method
call, property or indexer) aren't classed as variables.

> And why does this print 20, and not 0? Again I thought i had done this
> in other applications without this happening
>
> List<MyClass> myClasses= new List<MyClass>();
> myClasses.Add(new MyClass(20));
> MyClass m = myClasses[0];
> m.value = 0;
>
> // prints "20"
> Console.WriteLine(myClasses[0].value);

Again, that would be consistent with MyClass being a value type rather
than a reference type.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Are all your drivers up to date? click for free checkup

Author
9 Mar 2006 10:44 PM
Andrew Bullock
Jon Skeet [C# MVP] wrote:
Show quoteHide quote
> Andrew Bullock <andrewREMOVEbullockT***@ANDntlworldTHIS.com> wrote:
>> Why do i get the following error? In other projects I have done (what i
>> thought was) the same thing, without compiler complaint :-/
>>
>> 1: List<MyClass> myClasses = new List<MyClass>();
>> 2: myClasses.Add(new MyClass(20));
>> 3: myClasses[0].value = 0;
>>
>> Line 3: Cannot modify the return value of
>> System.Collections.Generic.List<OPS.MyClass>.this[int]' because it is
>> not a variable
>
> My guess is that despite its name, "MyClass" is a struct. Fields in a
> struct which are referenced via a non-variable expression (a method
> call, property or indexer) aren't classed as variables.
>
>> And why does this print 20, and not 0? Again I thought i had done this
>> in other applications without this happening
>>
>> List<MyClass> myClasses= new List<MyClass>();
>> myClasses.Add(new MyClass(20));
>> MyClass m = myClasses[0];
>> m.value = 0;
>>
>> // prints "20"
>> Console.WriteLine(myClasses[0].value);
>
> Again, that would be consistent with MyClass being a value type rather
> than a reference type.
>


Excellent,

Thankyou very much! You are a true legend!

One further question if you don't mind, how can I duplicate my list of
MyClasses?


Thanks,

Andrew
Author
9 Mar 2006 10:56 PM
Jon Skeet [C# MVP]
Andrew Bullock <andrewREMOVEbullockT***@ANDntlworldTHIS.com> wrote:
> Excellent,
>
> Thankyou very much! You are a true legend!
>
> One further question if you don't mind, how can I duplicate my list of
> MyClasses?

You want a separate, independent list?

List<MyClass> copy = new List<MyClass>(original);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Bookmark and Share