Anonymous types? A good thing?

By mOrPhie on Tuesday 08 April 2008 10:57 - Comments (3)
Category: programming, Views: 1903

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 03 April 2008 11:07 - Comments (2)
Categories: programming, techrelated, Views: 1469

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.

Virtual PC "erratic mouse" problem with some intel-chipsets

By mOrPhie on Wednesday 12 March 2008 16:05 - Comments (4)
Category: techrelated, Views: 2424

For my work I got a new Dell Vostro 1700. It's a great machine. 2 gigs of memory. A 7200RPM sata disc. Core 2 Duo CPU. 1920x1200 resolution. Pretty fast for a notebook. Fast enough to run me a virtual instance of Windows Server 2003 right? Well that seemed to be a bit of a problem.

I created a completely clean install of Windows Server 2003 R2. Installed all updates and installed the Virtual Machine additions. But still I seemed to have a "erratic mouse". And not only the mouse, the whole system seemed to suffer from that problem. It was as if the vritual CPU could not synchronize with the host CPU.

After investigation I came across this blog post. It provides this workaround:
  1. Stop running Virtual PC
  2. Open notepad
  3. Open %appdata%\Microsoft\Virtual PC\options.xml
  4. Locate or create the <virtual_machines> section of the file and add this key:
    <enable_idle_thread type="boolean">true</enable_idle_thread>
  5. Save the file and exit notepad
  6. Start Virtual PC
It worked on my notebook. It is a very dirty fix, because it simulates an idle thread to sync the CPU cycles, but still, it worked.

The problem I experienced, had something to do with a specific version of "Speedstep" which is used in some intel centrino chipsets. It's actually a Virtual PC problem, since VMWare and Parallels didn't seem to have the problem. I don't have a complete list of which chipsets have the problem, but when you have the problem, and you have a centrino notebook, try this fix.

Boolean parameters or Enums?

By mOrPhie on Wednesday 12 March 2008 15:44 - Comments (5)
Category: programming, Views: 1284

We all did it: adding a parameter like “bool useCachedConnection” to a method. When using the method it will look like this:


C#:
1
DoSomething(true);


While the method works, one day your collegue (or yourself) will come across this function and needs to digg into the definition to understand what the boolean implies, or worse, needs to adjust the method interface due to the fact that there are three options, instead of using or not using the cached connections. I’ve seen it several times and it saves a hell of a lot time if boolean parameters were to be avoided. A better way to do it:


C#:
1
2
3
4
5
6
7
8
9
10
11
12
enum ConnectionOptions
{
    CachedConnection,
    NewConnection
}

void DoSomething(ConnectionOptions options)
{
    //
}

DoSomething(ConnectionOptions.CachedConnection);


Now it’s possible to add options without having to change the methods’ interface. After a while you’ll have a nice collection of enums which can be reused by other projects. And don’t forget that the .NET Framework holds his own collection of enums which can be reused. When working with languages like T-SQL you don’t have a choice, but with .NET you have. Please make use of it. It’ll brighten up my day….. and yours. ;-)

.NET Framework 3.5 will be released with source code

By mOrPhie on Thursday 04 October 2007 15:44 - Comments (0)
Categories: programming, techrelated, Views: 307

As anyone can read on Scott’s page Microsoft decided to release the source code (with comments) of the .NET Framework (not the CLR as some blogs might make you think), beginning with V3.5. This is a big thing. But I want to tell you why.

There are several disassembly tools available for .NET which allow you to disassambly an assembly into source code. When you want to see what happens in System.Threading, just disassemble. But, it won’t provide you with debugging symbols. The result is you need to read it line by line when you want to figure something out. Hooray for Scott, because this release won’t just include the source code, but also debugging symbols, which are dynamically (down)loaded from the MSDN in VS 2008. Hence, the big thing. Great stuff. :-)