I mentioned named parameters and dynamic types in my previous post of tripping over interesting things in C#4. In this post we will re-look at named and optional parameters to mention some more facts and then conclude with another new feature, the COM and C# harmony.
Check VSTS 2010 – Some cool new features in C# 4: Default & Named Parameters and Dynamic Types (part 1 of many) for the previous chat on optional and named parameters.
Where both are invaluable, which I omitted to mention, is to simplify the pattern of method overloads to simulate optional parameters. Let us assume the following demo class, with a number of overloads:
You should notice the jungle of overloads to overcome a coding problem we could address in C++ for a long, long time. Well, with C#4 and optional parameters, we can re-write the above as:
You can start omitting optional parameters from the end, then start using named parameters if necessary. Neither optional nor named parameter are the silver bullet and I do not advocate a heavy usage from here onwards. However, both are exciting, because they allow us to simplify code, make it more readable and thus more maintainable.
If you are into COM and OLE, especially Office integration you should be aware of the associated kludge-magic and bloated binary payloads that are often necessary to make it all happen. I loved and still love COM … it is a great technology, but using it from .NET hardly every brought any smiles to the projects. So what does C# 4 bring to the party to bring back some of the happiness?
- Dynamic import … one of the headaches using Primary Interop Assemblies (PIA), was the use of object to define variant types, forcing you to perform cast, after cast after cast. Ugly code! You can now import the COM APIs using the dynamic type, instead of the object type, which nukes the need to use the ugly casting. Instead you can assign the dynamic variable with a strongly typed variable. That’s cool!
- No-PIA … instead of bloating your application by megabytes of junk, just because you made one call to a gazillion calls defined in a PIA, you can now benefit from the no-PIA feature. The C# compiler will automatically take what he needs out of the PIA, throw away the rest of the baggage and only insert the essentials into the relevant assemblies. Chris, this should address one of your greatest concerns with Office/COM integration 🙂
- Automatic ref … COM uses lots reference types, whereas C# is typically focused on value types, unless you explicitly decorate the walls with the “ref” type. Again COM and C# used to now start a debate. No more … the C# compiler will now allow you to pass value types, will automatically generate temporary reference variables and tricking COM into believing that you have still done the tedious programming yourself.
So at this stage I have stumbled over optional parameters, named parameters, dynamic types and many exciting COM interop productivity features. See you for some more exciting new features soon. We should probably chat about variance … need to first unravel a few confusions in the back of my mind though.
I feel like shouting “stop or slow down the bus” at times, because the wave of features arriving with VSTS alone are scary.
If you get the chance, visit http://code.msdn.microsoft.com/csharpfuture for some cool samples and documentation for C# 4.