Anonymous types? A good thing?

By mOrPhie on Tuesday 8 April 2008 10:57 - Comments (18)
Category: software engineering, Views: 8.714

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#:
1
2
3
4
5
6
7
8
var o = new
{
    Name = "Michael",
    Age = 30
};

Console.WriteLine(o.Name);
Console.WriteLine(o.Age);



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
2
3
4
5
6
7
var o = from person in persons
        where person.Age > 20
        select new
        {
            Name = person.Name,
            Age = person.Age
        };



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.
Ok, for demo-purposes it is easy to just print out the values to the console. And I could think of scenario's where I want to take data and put into an XML document or other sorts of structured data sources (for migration perhaps?). But is really that much work to do that explicitly and get readability?

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. ;)

Interesting links - April 3rd

By mOrPhie on Thursday 3 April 2008 11:07 - Comments (2)
Categories: software engineering, techrelated, Views: 2.910

When doing my job or just browsing the web in search for some brain food for the tech oriented geek, I sometimes come across some links I find interesting and then bookmark for later review. On some blogs (Scott Guthries' blog for example) it is common to post some links once a month. I mostly find these blog posts interesting so I decided to do the same for you readers. Maybe some links interest you too.

Teched 2007 Linq to SQL Presentation
Linq, part of the .NET Framework 3.5 and the latest language enhancements to C# and VB.NET, is a way of dealing with structured data. One implementation is Linq to SQL, which is generally an O/R-mapper for use with Linq. This presentation provide you with the basics.

Google Code University
This is a collection of video's, sessions, presentations and tutorials written by or for Google. Examples are C++ threading, Python 3000 by Guido Van Rossum, Java programming, web technologies like AJAX, etc... Very interesting.

Django on IronPython
Python is a interpreted programming language. IronPython is the .NET implemation. Django is a web application framework for Python. No Django runs on IronPython, which means you get the benifits of Python, .NET and Django in one package. Nice read, didn't try it yet though.

Singularity. An experimental operating system in C#
Microsoft has a research-project which aims on creating an operating system using the C# programming language. For this, they used custom compilers and a custom runtime to compile C# code directly to x86. Very interesting. Includes some documents. If you are into OS-programming, it is a must-read.

How to recognize a good programmer
Great read if you are a programmer yourself. A must read if you are about to hire programmers. Some points I might argue with, but still it covers most of the aspects of a good programmer.

BONUS: Direct Note Access - Splitting audio compositions in single notes
Not directly tech, but definitely tech-related. If you are into audio-programming, this thing is like water burning or pigs flying. I'm very interested in how this holds in some of my own recording-cases, but still, it looks very cool.