Tom Goff's .Net Musings

Tidbits of information regarding .Net, C#, and SQL Server.

Archive for September 2007

TOTW: Snippet Complier

with one comment

Often times I need to test or verify a small piece of C# code. Instead of creating a full-blown Visual Studio solution, I usually turn to Jeff Key’s Snippet Compiler[^]. This handy little tool allows you to write and test “snippets” of C#, VB.Net, and JScript.Net code. Best of all, I don’t have dozens of unneeded solutions sitting around on my hard disk.

Written by Tom

September 30, 2007 at 7:27 pm

Posted in .Net, C#, Tip of the Week

String.IsNullOrEmpty Issue & Solution

with 2 comments

I’m sure everyone has heard of the static IsNullOrEmpty[^] method that is available in the String class. Although, you may not have heard about the JIT optimizer bug[^] relating to that method. Essentially, the JIT optimizer is generating “optimized” code that accesses a null variable, thus throwing an exception. You may be tempted to write your own version of this method to get around the bug, like so:

public class StringHelper {
    public static Boolean IsNullOrEmpty(String x) {
        return (null == x || 0 == x.Length);
    }
}

Unfortunately, the JIT optimizer will generate the same null access exception with this method. There is a solution that relies on the fact[^] that the JIT optimizer (sometimes?) skips try blocks. Meaning it does not apply it’s optimizations to the code inside the try block. If we change our custom version of IsNullOrEmpty as shown below, then it will no longer generate the offending code or the exception.

public class StringHelper {
    public static Boolean IsNullOrEmpty(String x) {
        try {
            return (null == x || 0 == x.Length);
        }
        catch (Exception) {
            throw;
        }
    }
}

Although, this may be too much trouble for a method that is suppose to be a convenience. Microsoft has said[^] that this issue is fixed in the next version of Visual Studio.

Written by Tom

September 30, 2007 at 4:58 pm

Posted in .Net, C#, Gotcha, Visual Studio

COTW: Nullable Generic Structure

leave a comment »

The Nullable Generic Structure[^] allows developers to wrap value types so that they can be set to null. This wrapper object can either hold the referenced value type or it can indicate that no value is referenced (e.g. it’s null).

For example, if you would like to have an integer value that can also be set to null, you can define a variable as shown below:

Nullable<Int32> i = null;

if (null == i)

    i = 8;

When assigning a value to the Nullable variable, an implicit conversion is provided from the wrapped type. In our example above, this means that we can directly assign the value 8 without casting it. In addition, if we had an variable of type Int32, then we could directly assign it to the Nullable variable as shown here:

Int32 i1 = 8;

Nullable<Int32> i2 = i1;

When getting the actual value out of a Nullable variable, then we have to explicitly convert to the wrapped type as shown here:

Nullable<Int32> i1 = 8;

Int32 i2 = (Int32)i1;

In addition, you can access the Value[^] property directly. In both cases, an InvalidOperationException[^] will be thrown if there is no value (e.g. it’s null).

To determine if a value is present (e.g. it’s not null), then you can either compare the Nullable variable with null, as shown in the first example, or you can check the HasValue[^] property.

There is a helper method called GetValueOrDefault[^] which allows you to get the associated value or, if there is no value, then return a default value. So the following code, can be reduced to a single line:

Nullable<Int32> i1 = 8;

// ...

Int32 i2 = 99; // 99 is our default value

if (null != i1)

    i2 = (Int32)i1;

// ... The code above can be reduced to

Int32 i3 = i1.GetValueOrDefault(99);

Finally, C# compiler has a syntax shortcut to quickly and easy create Nullable objects. The code below actually uses the Nullable structure:

// The following two lines create identical objects

Nullable<Int32> i1 = 8;

Int32? i2 = 8;

You can find more information on MSDN[^] or Google[^].

Written by Tom

September 30, 2007 at 4:17 pm

Posted in .Net, C#, Class of the Week

Introducing COTW and TOTW

leave a comment »

Starting this week, I am introducing two weekly series to my blog. The first series will introduce a new .Net class and is called “Class of the Week” (COTW). The .Net framework has alot of classes that many people never use because they do not know about them. The second series is titled “Tip of the Week” (TOTW). The goal of this series is fairly obvious.

If you have any specific topics you would like to hear about, please let me know.

Written by Tom

September 30, 2007 at 4:13 pm