Anonymous Types: VB.NET vs C#

Did you know that in C#, the anonymous types are immutable ie they cannot be changed. However in VB.NET this is not the case.

The following example illustrates the difference

In C#

 var customAnonymoustType = new { Name = "Some Name", Age = 12 };
            customAnonymoustType.Name = "New Name"; //This will not compile

You will get the following error message "Property or indexer 'AnonymousType#1.Name' cannot be assigned to -- it is read only" .Let us now try the same thing in VB.NET

Dim customAnonymoustType = New With {.Name = "Some Name", .Age = 12}

customAnonymoustType.Name = "New Name"  'compiles fine

Just one of the differences between VB.NET and C#

 

No Comments