Posts Tagged ‘tips’

C# Tips – Type Conversion with “as” and “is”

Tuesday, July 21st, 2009

I had used C# for at least a year before I found out a couple of nice shorthand ways to convert types. These are the is and as expressions, and using them can help make your code more readable.

The is Expression

The is expression will return true if the provided expression is non-null and can be cast to the specific type.

private string GetDisplayField(object val)
{
	if (val is string)
	{
		string text = (string)val;
		return "Value was text: " + text;
	}
	else if (val is DateTime)
	{
		DateTime time = (DateTime)val;
		return "Value was date: " + time.ToShortDateString();
	}
 
	return "Could not match type";
}

The as Expression

The as expression will try to cast the object into the given type, and returns an object of that type if the cast was successful, or return null if unsuccessful.

This buys a little bit more functionality for you, as it assigns the variable with an already casted value if successful. It is functionally equivalent to: expression is type ? (type)expression : (type)null

private string GetDisplayField(object val)
{
	string text = val as string;
	DateTime? time = val as DateTime?;
 
	if (text != null)
	{
		return "Value was text: " + text;
	}
	if (time.HasValue)
	{
		return "Value was date: " + time.Value.ToShortDateString();
	}
 
	return "Could not match type";
}

Testing out the functions


Console.WriteLine(GetDisplayField(1));              // Output: "Could not match type"
Console.WriteLine(GetDisplayField("Hello"));        // Output: "Value was text: Hello"
Console.WriteLine(GetDisplayField(DateTime.Now));   // Output: "Value was date: 7/21/2009"

They are both readable ways to perform a type conversion – but pick one or the other! Using both of them is redundant.

C# Tips – Null Coalescing ??

Tuesday, May 5th, 2009

I have always enjoyed using the logical OR operator in JavaScript (||) as an oppurtunity to check for a null-like value, specifically to provide default parameters for functions or to check for existence of a property on a collection. I think they are a great way to make code more concise, and if used well, more readable.

These two functions do the same thing, but the first is shorter and more readable to people who may have to go back and modify it later.

function contentDocument(frame) {
    // If the frame.contentDocument either doesn't exist or is a null 
    // value, it will skip to the next value down the line 
    return frame.contentDocument || 
        frame.contentWindow.document || 
        frame.document;
}
 
function contentDocumentVerbose(frame) {
    // This works, but is possibly too verbose for such a simple task
    if (frame.contentDocument) {
        return frame.contentDocument;
    }
    else if (frame.contentWindow) {
        return frame.contentWindow.document;
    }
    else if (frame.document) {
        return frame.document;
    }
}

I was happy to see that C# has a similar feature, the Null Coalescing Operator.

This is nice for the same reasons, and is more readable than the normal ternary operator when simply checking null. Of course, if I am not checking for a null value, but a more complex boolean expression, I will usually use the ternary operator or an if-else block it is more than just a variable assignment or return statement. Anytime I use any extra syntactical feature, it is my hope to make the code more readable.

Anyway, here is an example of the ‘??’ (coalescing) operator contrasted with the ‘?:’ (ternary) operator.

string APP_DEFAULT = "application/octet-stream";
 
private string GetContentType(string contentType)
{
    // Nice and readable.  The '??' operator can be very useful.
    string FUNCTION_DEFAULT = null;
 
    return contentType ?? FUNCTION_DEFAULT ?? APP_DEFAULT;
}
 
private string GetContentTypeTernary(string contentType) 
{
    // This is NOT readable.  Please do not use nested ternary
    // operators... They take too much energy to figure out.
    string FUNCTION_DEFAULT = "text/plain";
 
    return (contentType != null) ? contentType :
        ((FUNCTION_DEFAULT != null) ? FUNCTION_DEFAULT : APP_DEFAULT);
}