11
// x is compiled as an int 
var x = 10;

// y is compiled as a string 
var y = "Hello";

// z is compiled as int[] 
var z = new[] { 0, 1, 2 };

but

// ano is compiled as an anonymous type 
var ano = new { x1 = 10, y1 = "Hello" };

ano object's properties created are read-only . I want to figure it out why those properties are read only. suggestions are appreciated ?

EDIT:

var ano1 = new { x1 = 10, y1 = "Hello" };

var ano2 = new { x1 = 10, y1 = "Hello" };

Is that if the new anonymous type has the same number and type of properties in the same order will it be of the same internal type as the first ?

Marc
  • 3,905
  • 4
  • 21
  • 37
Thilina H
  • 5,754
  • 6
  • 26
  • 56
  • 3
    Because that is Just How It Is - and *immutability* is nice. One nifty "for free" feature this allows is a sensible Equals/HashCode automatically implemented (with some basic sanity that it will remain valid). This is different in VB.NET (showing that it is merely a C# design decision) where the `Key` keyword is required to specify that an anonymous type's field is immutable. – user2246674 Sep 15 '13 at 18:56
  • That's just how anonymous types are defined in the C# standard. If you want to make them mutable (or only some of them mutable) don't use an anonymous type. – p.s.w.g Sep 15 '13 at 18:57
  • possible duplicate of [Why are the properties of anonymous types in C# read-only?](http://stackoverflow.com/questions/1089406/why-are-the-properties-of-anonymous-types-in-c-sharp-read-only) – Mike Zboray Sep 15 '13 at 19:03
  • Thankx for all .One more question.I edited the post. – Thilina H Sep 15 '13 at 20:16

3 Answers3

14

var does not mean "use an anonymous type", it means "Compiler, go figure out the type for me!". In the first three cases, the type is actually a "named" type - System.Int32, System.String, and System.Int32[] (in the last case the type of array's elements is also deduced by the compiler from the type of array elements that you put in the initializer).

The last case is the only one where an anonymous type is used. It is by design that C#'s anonymous types are immutable. The primary case for adding them in the language in the first place has been introduction of LINQ, which does not need mutability in cases when anonymous types are produced. In general, immutable classes tend to give designers less problems, especially when concurrency is involved, so designers of the language decided to go with immutable anonymous types.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Is there an alternative to anonymous types which will allow me to set values in c# ? – Thilina H Sep 15 '13 at 19:12
  • 1
    @Leez Yes. Create a type. You appear to be going down the road of "how can I do this in as few lines as possible", which, while convenient now, will be incredibly frustrating 2 months from now when you're trying to troubleshoot your code. Explicit type definition is not a bad thing. – Shirik Sep 15 '13 at 19:17
  • 1
    @Leez I think that the logic behind making anonymous types immutable was that if you wanted a mutable type, you would very likely benefit from giving such type a name. Giving a custom type a name would let you freely make objects of this type that could be passed across method's boundary without jumping through hoops or using `dynamic` types. Of course you can replace one object of anonymous type with another object with the type of the same structure ([demo](http://ideone.com/pYqA7O)). – Sergey Kalinichenko Sep 15 '13 at 19:49
1

Anonymous types are immutable, i.e Can't change.

What is an immutable type?

Eric Lippert's Blog

Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
1

Interesting statement from here. And an alternative is found here.

... [B]y ensuring that the members do not change, we ensure that the hash is constant for the lifetime of the object.This allows anonymous types to be used with collections like hashtables, without actually losing them when the members are modified. There are a lot of benefits of immutabilty in that, it drastically simplifies the code that uses the object since they can only be assigned values when created and then just used (think threading)

Community
  • 1
  • 1
Matthias
  • 5,574
  • 8
  • 61
  • 121