2

In this example:

var p1 = new {Name = "A", Price = 3};

And this translates into IL:

class __Anonymous1
{
   private string name ;
   private int price;
   public string Name{ get { return name; } set { name = value ; } }
   public int Price{ get { return price; } set { price= value ; } }
}
__Anonymous1 p1 = new __Anonymous1();
p1.Name = "A";
pt.Price =3

According to IL, it is Allowed, why is it so? What is the decision behind it? Shouldn't be readonly?

Thanks

It is my first question, be gentle.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794

6 Answers6

5

Let me turn that around on you: why restrict it?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
2

The compiler generates the classes and names them when the source is compiled. The reason these classes have read/write properties is (as the IL shows) the compiler does not create a constructor with all of the necessary properties as such the properties must be made read/write to allow the values to be set. This also allows the same class to be used by several anonymous types as long as their properties are the same.

Jeremy
  • 6,580
  • 2
  • 25
  • 33
1

If I make a list of anonymous types, as happens when using LINQ, for example, then you may want to change the value of an attribute once you get the rest of the information.

For example, if you have a percentage field in your anonymous type, you may not be able to calculate that until you know the max value or total (depending on how you want to define percentage).

James Black
  • 41,583
  • 10
  • 86
  • 166
1

I think the reason anonymous types are immutable is the functional language influence on LINQ and C# 3.0 together with them being a solution to a very specific problem.

If you want to modify your fields (properties), just create your own class instead of using an anonymous class. Is there a situation where you must use an anonymous class and be able to modify the properties?

Ðаn
  • 10,934
  • 11
  • 59
  • 95
0

It's an implementation detail of the compiler and shouldn't be depended on. A future compiler is completely free to make anonymous types take constructor parameters and make all properties get-only. For all intents and purposes, anonymous type objects should be considered immutable once they are instantiated. (The properties themselves might reference mutable objects, though.)

Levi
  • 32,628
  • 3
  • 87
  • 88
-2

I'm not sure I understand your question... Why wouldn't it be allowed?... or what do you expect not to be allowed?

var p = new {Name = "A", Price = 3};
//Some Code Here
p.Name = "B";
//Some more code 
p.Price = 5;

Likewise, if I were to create a second anonymous variable the C# compiler is smart enough to know that they have same structure, so it will generate a single anonymous class in the background.

var p = new {Name = "A", Price = 3};

//Some Time Later

var q = new {Name = "B", Price = 4};

//Only one anonymous class is generated in the IL
Noldorin
  • 144,213
  • 56
  • 264
  • 302
Eoin Campbell
  • 43,500
  • 17
  • 101
  • 157
  • 3
    Either you are wrong or Mr. Jon Skeet is, see http://stackoverflow.com/a/1585466/382783 – Boris B. Feb 02 '12 at 19:13
  • 3
    You can't modify value in anonymous type var as mentioned by @BorisB.. try yourself and you'll get `Property or indexer 'AnonymousType#1.[FIELD]' cannot be assigned to -- it is read only` – Andre Figueiredo Apr 22 '14 at 21:04
  • This answer contradicts that of http://stackoverflow.com/questions/1089406/why-are-the-properties-of-anonymous-types-in-c-sharp-read-only – Old Geezer Feb 28 '15 at 03:12