W
William Stacey [C# MVP]
Maybe an Oxymoron, but this would be very useful. Anonomous types are great
for doing projections (especially in Linq), but not being able to pass or
return them is a bummer. Why couldn't they add something like an "AS"
modifier like:
var v = new {Name="", Age=0} as MyType;
var v2 = Change(v);
void MyType Change(MyType mt)
{
return new {mt.Name + "2", mt.Age += 1} as MyType;
}
In this case, MyType can only be "declared" the same way over the whole
assembly. So you can use "as MyType" in multiple projections with
compile-time checking. However, the compiler flags an error if MyType is not
declared the same way each time, so the first declaration "sets" the type by
name. In this case, it is not really anonymous anymore, however it adds
ability to strongly type projections and ref them from across functions and
assemblies.
for doing projections (especially in Linq), but not being able to pass or
return them is a bummer. Why couldn't they add something like an "AS"
modifier like:
var v = new {Name="", Age=0} as MyType;
var v2 = Change(v);
void MyType Change(MyType mt)
{
return new {mt.Name + "2", mt.Age += 1} as MyType;
}
In this case, MyType can only be "declared" the same way over the whole
assembly. So you can use "as MyType" in multiple projections with
compile-time checking. However, the compiler flags an error if MyType is not
declared the same way each time, so the first declaration "sets" the type by
name. In this case, it is not really anonymous anymore, however it adds
ability to strongly type projections and ref them from across functions and
assemblies.