Scott Dorman

ephemeral segment

  Home  |   Contact  |   Syndication    |   Login
  578 Posts | 10 Stories | 698 Comments | 51 Trackbacks

News


Post Categories

Image Galleries


Microsoft Store


Creative Commons License


Microsoft MVP


MCP Profile


Locations of visitors to this page

Subscribers to this feed

TwitterCounter for @sdorman

View blog authority

Add to Technorati Favorites

Windows Live Alerts

AddThis Social Bookmark Button

LinkedIn profile

Community Credit profile

The Code Project

Follow me on Twitter

Get Free Shots from Snap.com

Community Credit Hall of Fame

Get Feedghost

Xobni outlook add-in for your inbox



Support This Site

Tag Cloud


Article Categories

Archives

Post Categories

Image Galleries

Saturday, March 13, 2010 #

An empty string is different than an unassigned string variable (which is null), and is a string containing no characters between the quotes (""). The .NET Framework provides String.Empty to represent an empty string, and there is no practical difference between ("") and String.Empty.

One of the most common string comparisons to perform is to determine if a string variable is equal to an empty string. The fastest and simplest way to determine if a string is empty is to test if the Length property is equal to 0. However, since strings are reference types it is possible for a string variable to be null, which would result in a runtime error when you tried to access the Length property.

Since testing to determine if a string is empty is such a common occurrence, the .NET Framework provides the static method String.IsNullOrEmpty method:

public static bool IsNullOrEmpty(string value)
{
    if (value != null)
    {
        return (value.Length == 0);
    }
 
    return true;
}

It is also very common to determine if a string is empty and contains more than just whitespace characters. For example, String.IsNullOrEmpty("   ") would return false, since this string is actually made up of three whitespace characters. In some cases, this may be acceptable, but in many others it is not. TO help simplify testing this scenario, the .NET Framework 4 introduces the String.IsNullOrWhiteSpace method:

public static bool IsNullOrWhiteSpace(string value)
{
    if (value != null)
    {
        for (int i = 0; i < value.Length; i++)
        {
            if (!char.IsWhiteSpace(value[i]))
            {
                return false;
            }
        }
    }
    
    return true;
}
 

Using either String.IsNullOrEmpty or String.IsNullOrWhiteSpace helps ensure correctness, readability, and consistency, so they should be used in all situations where you need to determine if a string is null, empty, or contains only whitespace characters.

Technorati Tags: ,
Digg This

An enumerated type, also called an enumeration (or just an enum for short), is simply a way to create a numeric type restricted to a predetermined set of valid values with meaningful names for those values. While most enumerations represent discrete values, or well-known combinations of those values, sometimes you want to combine values in an arbitrary fashion. These enumerations are known as flags enumerations because the values represent flags which can be set or unset. To combine multiple enumeration values, you use the logical OR operator.

For example, consider the following:

public enum FileAccess
{
   None = 0,
   Read = 1,
   Write = 2,
}
 
class Program
{
    static void Main(string[] args)
    {
        FileAccess access = FileAccess.Read | FileAccess.Write;
        Console.WriteLine(access);
    }
}

The output of this simple console application is:

image

The value 3 is the numeric value associated with the combination of FileAccess.Read and FileAccess.Write. Clearly, this isn’t the best representation. What you really want is for the output to look like:

image

To achieve this result, you simply add the Flags attribute to the enumeration. The Flags attribute changes how the string representation of the enumeration value is displayed when using the ToString() method.

Although the .NET Framework does not require it, enumerations that will be used to represent flags should be decorated with the Flags attribute since it provides a clear indication of intent.

One “problem” with Flags enumerations is determining when a particular flag is set. The code to do this isn’t particularly difficult, but unless you use it regularly it can be easy to forget. To test if the access variable has the FileAccess.Read flag set, you would use the following code:

(access & FileAccess.Read) == FileAccess.Read

Starting with .NET 4, a HasFlag static method has been added to the Enum class which allows you to easily perform these tests:

access.HasFlag(FileAccess.Read)

This method follows one of the “themes” for the .NET Framework 4, which is to simplify and reduce the amount of boilerplate code like this you must write.

Technorati Tags: ,
Digg This

Friday, February 26, 2010 #

image

For those of you who like/use Facebook, I have created a Facebook Fan Page for the book. I’ll try and keep this page updated with information about what’s going on with the book.

Digg This

0672331012

Probably my fault for not actually checking this sooner, but my upcoming Sams Teach Yourself Visual C# 2010 in 24 Hours book is available for pre-order on Amazon.com.

Digg This

Thursday, February 25, 2010 #

0672331012 I am very happy to announce that the core content for my upcoming Sams Teach Yourself Visual C# 2010 in 24 Hours book is done!

I still need to incorporate comments from Claudio and Eric (my technical editors) and then incorporate edits from the Sams editors, but all of the really hard work is now done.

Even though I knew before starting that it would not be easy I don’t think I was really prepared for just how difficult it is to actually write a book. I started actually writing around mid-July 2009, so it has taken about 7 months (however, the process actually started  mid-April 2009). I am currently weighing in at 476 content pages, but the final page count will be a bit higher due to the front and back matter (table of contents, index, etc.) and a forward that Eric has graciously agreed to write.

Working with Sams has been a great experience. Working with both Claudio and Eric has been great as well; they have both provided an incredible amount of comments and insight. I also want to thank Keith Elder, Shawn Weisfeld, Brad Abrams, and Krzysztof Cwalina for their early input on the table of contents (Although I did end up changing the table of contents slightly from what they originally saw, the majority of it is still the same and without their input I would have had a much harder time focusing the content.)

The book should hopefully be on the shelves sometime around April, in time for the official launch of Visual Studio 2010 and C# 4. If you have already looked at one of the earlier editions and decided that it wasn’t for you, please give the new edition a chance to change your mind; for those of you who already have an earlier edition, I hope this one will be of interest to you as well. This new edition is quite a bit different from the previous ones by being much more language-focused and taking a more holistic view of the language by focusing on what is in the language now rather than showing how the language evolved.