March 2006 - Posts

Some useful C# 2.0 features

Our team has recently moved over to the .NET Framework 2.0 and I've decided to catch up on this new skill by reading the C# 2.0 specification. There are a couple of well-known features such as iterators, anonymous methods (yuck) and generics, but I noticed two small features that might have a tremendous effect on our team's productivity.

The first is the new double-question-mark operator. Consider the following code snippet:

object foo = bar ?? String.Empty;

This piece of code will assign foo to bar only if bar is not null. If bar is null, then foo will be assigned to String.Empty. As a matter of interest, this operator was introduced as part of the nullable types feature, but it will work with any reference type.

The second feature is called Property Accessor Accessibility. I really missed this feature while developing in .NET 1.1, but now it is part of C# 2.0, and allows you to modify the accessibility of a property's get or set accessor.

Let's say that you have a property that you want to be read publicly, but you only want to modify it internally, then you will write a code snippet similar to the following:

public object Foo
{
     get return foo; }
    internal set { foo = value; }
}

powered by IMHO 1.3

Posted by trumpi | 4 comment(s)
Filed under: