String.Split(): Skip Empty Entries

At the risk of publicising that I’m the last person to know this(!), I recently discovered that String.Split() has an overload that takes a parameter
StringSplitOptions.RemoveEmptyEntries that does exactly what it says, like this:
.cf { font-family: Consolas, Courier New, Courier, Monospace; font-size: 9pt; color: black; background: white; }.cl { margin: 0px; }.cb1 { color: blue; }.cb2 { color: #a31515; }.cb3 { color: #2b91af; }

char[] separator = new char[] { ',' };
string[] result;
string toSplit = "Rick,Dave,,Nick,,,Roger,";
 
result = toSplit.Split(separator,
                       StringSplitOptions.RemoveEmptyEntries);
foreach (string s in result)
{
    Console.WriteLine("[{0}]", s);
}

This is also very useful for splitting text where extra whitespace should be ignored:
.cf { font-family: Consolas, Courier New, Courier, Monospace; font-size: 9pt; color: black; background: white; }.cl { margin: 0px; }.cb1 { color: blue; }.cb2 { color: #a31515; }.cb3 { color: #2b91af; }

string woods = "The woods are  lovely, dark and deep.." +
               "But I  have promises to keep, " +
               "And miles to  go before  I sleep,, " +
               "And   miles to go before I sleep.";
char[] whitespace = { ' ', ',', ';', ':', '.', '!', '?' };
 
string[] words = woods.Split(whitespace,
                             StringSplitOptions.RemoveEmptyEntries);
foreach (string s in words)
{
    Console.WriteLine("[{0}]", s);
}

Perth .NET User Group Meeting, Tues Nov 4th, 5:30pm: F# |> Functional with Nick Hodge

Join us at the Perth .NET Community of Practice, Tuesday November 4th to hear Nick Hodge present a session on F# and the rise and rise of the new .NET functional and dynamic languages, where and when to use them, and why F# is NOT the new C#!

TOPIC: F# > Functional with Nick Hodge
DATE: Tuesday, November 4th, 5:30pm
VENUE: Excom, Level 2, 23 Barrack Street, Perth
COST: Free. All welcome

Nick is a self-confessed professional geek working for Microsoft. He has over 22 years of IT industry experience in a variety of sales, technical, management, semi-marketing and strategic roles. He is a sought-after presenter, prolific social networker and closet workaholic.

Please Note: This meeting is not in our usual Thursday slot.

C# Tips and Tricks

There is a very useful post over at StackOverflow on some of the less known parts of C#. Here are a few of my favourites:

Default Event Handler:
.cf { font-family: Consolas, Courier New, Courier, Monospace; font-size: 9pt; color: black; background: white; }.cl { margin: 0px; }.cb1 { color: blue; }.cb2 { color: #2b91af; }.cb3 { color: green; }

public delegate void MyClickHandler(object sender, string myValue);
public event MyClickHandler Click = delegate { }; // add empty delegate!

Let’s you do this:
.cf { font-family: Consolas, Courier New, Courier, Monospace; font-size: 9pt; color: black; background: white; }.cl { margin: 0px; }.cb1 { color: blue; }.cb2 { color: #a31515; }

public void DoSomething()
{
    Click(this, "foo");
}

Instead of checking for null before invocation:
.cf { font-family: Consolas, Courier New, Courier, Monospace; font-size: 9pt; color: black; background: white; }.cl { margin: 0px; }.cb1 { color: blue; }.cb2 { color: green; }.cb3 { color: #a31515; }

public void DoSomething()
{
    if (Click != null) // Unnecessary
    {
        Click(this, "foo");
    }
}

Chaining the ?? operator:
.cf { font-family: Consolas, Courier New, Courier, Monospace; font-size: 9pt; color: black; background: white; }.cl { margin: 0px; }.cb1 { color: blue; }.cb2 { color: #2b91af; }

string result = val1 ?? val2 ?? val3 ?? String.Empty;

And it never ceases to amaze me that many devs don’t use System.IO.Path.Combine(), instead of:
.cf { font-family: Consolas, Courier New, Courier, Monospace; font-size: 9pt; color: black; background: white; }.cl { margin: 0px; }.cb1 { color: blue; }.cb2 { color: #a31515; }

string path = dir + "\\" + fileName;

Do You Review?

I can honestly say I love where I work. And today was a classic example why. I had a code review! (does your team have code reviews?). One of my colleagues pointed out I could make use of Nullable GetValueOrDefault() in the following code snippet:

Instead of this:
.cf { font-family: Consolas, Courier New, Courier, Monospace; font-size: 9pt; color: black; background: white; }.cl { margin: 0px; }.cb1 { color: blue; }.cb2 { color: green; }.cb3 { color: #2b91af; }

int? objectID;          // passed in to a method...
DateTime? signedDate;   // --- "" ---
 
if (objectID == null)
{
    objectID = 0;
}
 
if (signedDate == null)
{
    signedDate = (DateTime)SqlDateTime.MinValue;
}
 
SomeDBWrapperMethod((int)objectID, (DateTime)signedDate);

Just do this:
.cf { font-family: Consolas, Courier New, Courier, Monospace; font-size: 9pt; color: black; background: white; }.cl { margin: 0px; }.cb1 { color: #2b91af; }

SomeDBWrapperMethod(objectID.GetValueOrDefault(), 
                    signedDate.GetValueOrDefault((DateTime)SqlDateTime.MinValue));

Seems so obvious, after the fact! I’m sure I must have come across this before, but I can’t remember having ever used it. It’s great to have extra pairs of eyes go over your code.

SQL Server scripts

A few SQL Server script resources:

Updated: added a few more.

MVP Award

This morning I received an email welcoming me into the Microsoft MVP award program. I would sincerely like to thank everyone for their support and encouragement. Over the last 5 years I have met and corresponded with several MVPs and have always had the highest respect for their skills, knowledge and commitment. It’s an honour and I hope I can live up to it.

Congratulations to Joe Albahari, who was also awarded MVP status today.