I came here being curious about Anonymous Types, and turned out learning a lot from the answers given.
In my answer i will try to synthesize some valuable information i found, and provide information that i did not see in the other answers and that i think will also be worth to read for future visitors.
Why is not possible to re-set (out of the box) the values of an anonymously type object?
To better understand the why, it is worth to read what @EricLippert said in a comment to another question about Anonymous Types:
Note that anonymous types in VB are allowed to be partially mutated. In VB you get to state which parts of the anonymous type are mutable; the generated code will not use mutable bits as part of a hash code / equality, so you don't get the "lost in the dictionary" problem. We decided to not implement these extensions in C#.
Plus the text cited by the Accepted Answer from the official C# documentation:
Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first. The type name is generated by the compiler and is not available at the source code level. The type of each property is inferred by the compiler.
And just to justify a bit the why it is not possible (out of the box) to set the value of a anonymously typed object, let's see what C# in a Nutshell says:
[If you define]
var dude = new { Name = "Bob", Age = 23 };
The compiler translates this to (approximately) the following:
internal class AnonymousGeneratedTypeName
{
private string name; // Actual field name is irrelevant
private int age; // Actual field name is irrelevant
public AnonymousGeneratedTypeName(string name, int age)
{
this.name = name; this.age = age;
}
public string Name { get { return name; } }
public int Age { get { return age; } }
// The Equals and GetHashCode methods are overriden...
// The ToString method is also overriden.
}
...
var dude = AnonymousGeneratedTypeName ("Bob", 23);
But, is it true that once you set the value of an Anonymous Type it is not possible to modify it?
Well, as we can learn from the answer given by @Alex:
nothing is truly immutable when using reflection in combination with knowledge on how certain things are implemented (backing fields for the read-only properties of anonymous types in this case).
If you are curious to see HOW you can modify the value of an anonymously typed object, go and read his answer, it is really worth!
If at the end, you what to stick with the simplicity in the one liner
var dude = new { Name = "Bob", Age = 23 };
and want to be able to modify latter one of dude's properties, you can (in many cases) simply change the var keyword by dynamic. Then you can do
dynamic dude = new { Name = "Bob", Age = 23 };
dude.Name = "John"; // Compiles correctly.
But watch out! var and dynamic are not as similar as they seem at a first look. As already noted in a comment to @B.K. by @MikeBeaton
this doesn't work in the case where you need a typed null value? (i.e. an ExpandoObject can't support that, but an anonymous type can)
There are some SO posts about dynamic vs var.