Anonymous types? A good thing?
C# 3.0 comes with some new language features, such as Anonymous Types and Type Inference, which allow me to write code like this:
C#:
The new-structure is a way of creating an anonymous type. The type is created implicitly in compile-time. The var is type inference. Since the compiler knows the type of the structure behind it (anonymous or not), it can implicitly infer the type, so typing var should be enough. Basically it's a reduction of syntax. It's not a variant. Once it's got it's type, you cannot override that.
But when do I want to implicitly let the compiler create a type, when I can have all the control of creating my own? In LINQ, it is a way of minimizing the object without having to create a new type. Like this:
C#:
Now "o" only contains (a collection of) Name and Age instead of the complete object. But what would I want to do with it?
Remember, I'm not talking about object initializers. I like object initializers very much and result in much nicer and cleaner code. I'm just not sure if Anonymous Types is going to bring more advantages or more pain.
If you worked with anonymous types with success, I'd like to here from you. I'm skeptical, but always open minded.
C#:
1 | var o = new
|
The new-structure is a way of creating an anonymous type. The type is created implicitly in compile-time. The var is type inference. Since the compiler knows the type of the structure behind it (anonymous or not), it can implicitly infer the type, so typing var should be enough. Basically it's a reduction of syntax. It's not a variant. Once it's got it's type, you cannot override that.
But when do I want to implicitly let the compiler create a type, when I can have all the control of creating my own? In LINQ, it is a way of minimizing the object without having to create a new type. Like this:
C#:
1 | var o = from person in persons
|
Now "o" only contains (a collection of) Name and Age instead of the complete object. But what would I want to do with it?
- Binding it to a control would be good for viewing, but it isn't two-way bounded to a data source, so committing changes to the database, like LINQ to SQL (NHibernate, LLBLGen) does, isn't possible.
- Using it as a small type for use in web services isn't possible. SOAP needs the return types to be explicit, so that the consumer can act upon it.
- You can't write domain-specific code for it. When that wish becomes reality, you still need to create your own class.
- I think it's less readable. Explicit types are defined in one place and used in another. Where anonymous types are defined in place... or worse: all over the place. It ignores the use of an object model.
Remember, I'm not talking about object initializers. I like object initializers very much and result in much nicer and cleaner code. I'm just not sure if Anonymous Types is going to bring more advantages or more pain.
If you worked with anonymous types with success, I'd like to here from you. I'm skeptical, but always open minded.
|
|
Microsoft DevDays 2008 - An overview |
|
|
Interesting links - April 3rd |
Comments
Voor linq is het zeer handig, dan moet ge niet altijd aparte klassen aanmaken voor de dingen die ge opvraagt.
while I do quite some programming in the .net environment (mainly 2.0 and 3.5), I have never had the need to use anonymous types. Today I overlooked quite some code, and kept ATs in my mind reviewing the code. But it there hasn't ben a single instance where ATs could be usefull.
Will keep looking for it though, I find it interesting to check the relevance for new features in software/languagees.
Will keep looking for it though, I find it interesting to check the relevance for new features in software/languagees.
@Ruudjah: Tnx for your input. I'm glad to see some confirmation of my doubts on anonymous types. 