C#

Catching 'Exception' is Bad? Isn't It?

We all know that catching Exception is a bad idea - for all sorts of reasons.

Co- and Contra-Variance in .NET 3.5

A lot of the discussion about .NET 4.0 is revolving around the introduction of co-variance and contra-variance for generic types.

It's important to remember, though, that these concepts aren't entirely new - there has been some support for variance built into .NET for some time.

For example, consider this snippet of code:

    public Form1()
    {
        InitializeComponent();
 
        textBox1.KeyPress += Handler;
        button1.Click += Handler;
 
        textBox1.KeyPress += new KeyPressEventHandler(Handler);
        button1.Click += new EventHandler(Handler);

Performance of Dynamic

This is interesting - a side-by-side performance comparison of reflection, dynamic, and direct property access.

When to use Var

I had an interesting conversation with a friend earlier this week, over when to use the var keyword in C# 3.0.

He and a colleague had divergent views, one passionately arguing it should be used whenever possible, and the other just as passionate in his view that it should never be used.

Struct Weirdness

Over on StackOverflow, saw an interesting question that taught me something new about C# 3.0.

Using Lambdas as Event Handlers

Of all the new features in C# 3.0, Lambda expressions have to be one of my favourites.

One non-obvious way that they can be used is as event handlers, in just the way that anonymous delegates could be.

Contravariance and Covariance at last

It seems the information deluge has started, and the first piece of good news about C# 4.0 is the introduction of contravariance and covariance for delegates and interfaces.

Charlie Calvert has a useful summary that I'd suggest you read.

On first glance, it looks as though the support hinges on adding a in and out modifiers to type parameters as hints for further use.

Extension Methods and Null Pointers

I'm sure that most of you know that you can't call methods on a null reference, so you'll expect that this code will fire a nasty exception:

   1:  
   2: IPath p = null;
   3: int l = p.Length();
   4:  

Except, it doesn't.

Lambda Events

One key feature of C# as a language is its event support.

While the basic infrastructure has remained substantially unchanged from the original 1.0 version of C#, the supporting syntax has become progressively cleaner and clearer.

In the recently released C# 3.0, lambda expressions coupled with enhanced type inference give the simplest syntax yet.

No longer the C# we knew

Over the past few weeks, I've been spending a great deal of time getting to grips with the new features available in .NET 3.5, especially the new syntax supported by C# 3.0.

Rare C#

To me, the areas that languages have in common are much less interesting than the unique bits.

For example, Delphi has support for virtual constructors and virtual class methods, two features not supported by the C# language. They give a Delphi developer the ability to implement some (very elegant) solutions that are simply impossible in C#.

Some C# features that I only recently discovered include the "??" operator, and checked/unchecked blocks. Google for more.