Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why class's this is readonly? [closed]

Tags:

c#

.net

I tried to define a class. when I assign using

this = blah blah

the compiler reports "this is readonly"

when I change the class to struct, it looks fine, any idea?

like image 215
Adam Lee Avatar asked Nov 30 '22 02:11

Adam Lee


1 Answers

this in a class refers to the reference; you cannot reassign your own reference, but you can assign fields etc of the current instance.

this in a struct refers to the value itself; when you assign this, just like when you assign to any value-type variable / parameter, it is copying all of the fields over the top (as a memory-copy). That is possible, but is frankly rare to see in the wild. Or, as with classes, you can assign each of the fields separately.

like image 88
Marc Gravell Avatar answered Dec 09 '22 22:12

Marc Gravell