Archive for the '.NET 2.0' Category

Extending CopyHelper using Generics

In my last post, I created a method that does the grunt work of copying data from an instance of one class to an instance of another type. I often find myself copying data between the properties of my data layer classes and those of my user interface like this. // Copy the data from [...]

Read Properties from an MSI File

Today I was working writing auto-updating for some software. I wanted to base it on the Product Version property in the installer MSI file, so I needed some code to read that from the file. It took a fair amount of searching and code tweaking, but I finally worked it all out. Add a reference [...]

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”, duration.TotalMilliseconds ); If you do, you should [...]

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 [...]

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 [...]

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 [...]