Archive for the '.NET 2.0' Category

The Stopwatch Class in .NET

Do you ever find yourself using DateTime to time a section of code?  Do you have code like the following?
DateTime start = DateTime.Now;

// Perform a long process
Thread.Sleep( 1968 );

DateTime end = DateTime.Now;
TimeSpan duration = end.Subtract( start );
Console.WriteLine( “This process took {0} ms”,
[...]

The Yield Statement in C#

Another often overlooked C# statement that was introduced in .NET 2.0 is yield. This keyword is used to return items from a loop within a method and retain the state of the method through multiple calls. That is a bit hard to wrap your head around, so as always, an example will help;
public static IEnumerable<int> [...]

Null Coalescing Operator

The first time I saw the ?? operator in C#, I did a double take and had to look it up. The operator, called the null coalescing operator was added in C# 2.0 and is pretty useful, but still fairly unknown.

Order order = GetOrder( id ) ?? new Order();
In the above code, if the return [...]

Nullable Types in C#

In .NET 1.0 and 1.1, if you wanted to store a null value for a value type (int, double, DateTime, etc) you would have to box it or write your own wrapper around it. The introduction of generics in .NET 2.0 allowed the concept of nullable types to be introduced into the language through the [...]