Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why anonymous type are immutable in c#? [duplicate]

Possible Duplicate:
Why are the properties of anonymous types in C# read-only?

I wrote something like this,

 var suspense = new { Name = "Android", Market = string.Empty };
 suspense.Market = "Potential";

.NET throws error

Property or indexer 'AnonymousType#1.Market' cannot be assigned to -- it is read only

I know that AnonymousTypes in C# are immutable, but why? Is this due to some limitation with CLR?

like image 663
eka Avatar asked Oct 21 '22 19:10

eka


1 Answers

The motivating factor for driving the immutable anonymous types was because the LINQ APIs used hash tables internally and returning projections of anonymous types that could be modified was a dangerous situation.

You can check :

Immutable types: understand their benefits and use them

Anonymous Types and Object Identities By Tim Ng on MSDN

like image 87
Pranay Rana Avatar answered Oct 28 '22 20:10

Pranay Rana