Keyed Anonymous types in VB9

Posted Tue, Jul 10 2007 6:05 by bill

Previously I wrote about my opinion on anonymous types and keys.  This post just tells it how it actually is

(IOW: yes I would still prefer some way to specify the entire type is For ReadOnly rather than having to prefix every property assignment with Key or to use the constructor with named parameters instead.)

 

These samples are per the June CTP:

 

(1)   Dim person = New With {.Name = "Fred", .Age = 42}

Name and Age are read/write, and there is no hash code or equals defined for the class,(only System.Object implementations, hence reference equality only)

 

(2)   Dim person = New With {Key .Name = "Fred", .Age = 42}

Name is ReadOnly, Age is Read/Write. Equals and GetHashCode are defined and are based on the Name property only.

 

(3)  Dim person = New With {Key .Name = "Fred", Key .Age = 42}

Name and Age are both ReadOnly. Equals and GetHashCode are defined and are based on both the Name and Age properties.

 

Of the above, (1) is mutable, (2) is partially mutable, and (3) is immutable.

Filed under: , ,