C#

Regions in C#

Regions in C# are one of those things that seem to have never quite delivered on their initial promise of giving us an another way to structure our code in addition to the constructs provided by the language.

Parameter Attributes and more

Fresh from Twitter, here are some ways to use attributes in C# that you might not know ...

Did you know you can put attributes on method parameters ...

public string MyMethod([MyCustomAttribute] int id)
{
    // ...
}

Did you know you can specify an attribute on the return of a method ...

[return: CrazyAttributeOnAReturnMethod(WithAParameterOfItsOwn = true)]
public string MyMethod(int id)
{
    // ...
}

For the record: I knew the first, but not the second ...

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.

Syndicate content