Creating A Comma Delimited String

A little while back I needed to create a comma-delimited string to parse into my SQL Query. My first attempt in creating my comma-delimited string involved using a StringBuilder class and appending a comma at the end of each of my values via a loop. However, I found that my application would error when parsing my comma-delimited string into my SQL query due to the fact a comma was added to the end of my string.

After some research on the MSDN website to solve my problem I found a solution and it was simpler than I thought. The .NET Framework already has a class called CommaDelimitedStringCollection and it is pretty simple to use as the following example shows:

using System.Configuration;
public partial class CommaPage : System.Web.UI.Page 
    protected void Page_Load(object sender, EventArgs e)
        //Create a collection to parse to CommaDelimitedStringCollection class
        List<string> cars = new List<string>();
        //Create instance of CommaDelimitedStringCollection
        CommaDelimitedStringCollection commaCollection 
        = new CommaDelimitedStringCollection() ;
        //Iterate through cars collection and add to commaCollection
        foreach (string item in cars)
        //Read out list of values

The output of the example above would be: "Volvo, VW, BMW, Ford".

So pretty much the .NET Framework's CommaDelimitedStringCollection class did all the work for me.

Nice one!

Is an Arraylist still in use?

When I first started using ASP.NET 1.1, I always used an Arraylist to iterate through most of my collections. However, when I started using ASP.NET 2.0 I was introduced to Generic Lists. The List<T> class is the generic equivalent of the ArrayList class. It implements the IList<T> generic interface using an array whose size is dynamically increased as required. This means the List class performs much better in most cases and more importantly it is "type" safe.

So I am not too sure why you would ever need to use an Arraylist since a Generic List class is able to do exactly the same thing with the added benefit of extra perfomance. So is the Arraylist still widely used in todays standards? I guess it still must be in use if Microsoft has not omitted it from their Framework. Maybe Microsoft has something in store for the Arraylist in their grand plan.

Here are a few useful links:

C# Corner - C# Generics Josh Williams MSDN Blog - Arraylist vs. Generic List MSDN Network - List (T) Generic Class

Inline If Statement

Standard IF statements are great. But I found that when I am using very simple conditions within my IF Statement I waste a lot of space in my code.

Example 1:

public int IfExample(bool bolFlag)
int result = 0;
if (bolFlag)
result = 1;
result = 2;
return result;

There is no problem using Example 1. But there is a much quicker and less tedius way of using a simple conditional IF statement.

Example 2:

public int IfExample(bool bolFlag)
return (bolFlag) ? 1 : 2;

This example is basically saying: "return if (bolFlag) 1 else 2". So you are in theory assigning the IF condition to the variable.

I know it takes a little while to understand the syntax. But it is really good for those less complex IF conditions.

C# NULL Coalescing Operator

Here is a really neat trick that a mate showed me at work. It is a way to assign one variable to another, but only if the variable is not null. If it is null, you want to populate the target variable with perhaps another value.

For example, lets assume we have an integer variable "intCounter". We could check if the integer contents was null, and if not return another value:

int intCounter = 5;
int intResult = intCounter ?? 0;
// Output: intResult == 5 

We got the following output because intCounter did not contain a null.

The C# NULL coalescing operator also works with string variables as well:

string strMessage = "Hello World";
string strResult = strMessage ?? "No message";
// Output: strResult == "Hello World" 

So we can see from the two examples above that this NULL Coalescing operator is quite useful. Here is an example of what would have happened if a variable was null:

string strMessage = null;
string strResult = strMessage ?? "No message";
// Output: strResult == "No message" 

Scott Guthrie talks more about this in his own blog and how to use this feature in LINQ: