posts - 293 , comments - 426 , trackbacks - 0

My Links

News

View Steve Michelotti's profile on LinkedIn

Twitter












Tag Cloud

Archives

Post Categories

My Online Presence

C# If Else Shortcut

 

Recently I participated in a local user group challenge which involved submitting a project that solved a problem with the fewest bytes. My end solution resulted in one long line of code that at first glance seemed like a series of symbols and letters. While I wouldn’t use such an approach in production code it was nice to go against everything I have been taught about naming and spacing.

One optimization that we used was to simplify the if..then..else statement.

In C# you could have the following code where foo is an integer…

if (foo > 5)
{
    foo = 6;
}
else
{
    foo = 4;
}
 

Of course this is to long, so instead you can use the if then else shorthand, which looks something like this…

foo = foo < 5 ? 6 : 4;

 

Pretty cool, what if you didn’t want the else part, so you wanted the shorthand to the following…

if (foo > 5)
{
    foo = 6;
}
 

You could do this…

foo = foo < 5 ?? 6;

Print | posted on Wednesday, October 27, 2010 6:49 AM | Filed Under [ C# ]

Feedback

Gravatar

# re: C# If Else Shortcut

You're inverting your logic, it should be
foo = foo > 5 ? 6 : 4;

Also, the null coallesce operator ?? is used if the reference is null, not as you have used it. For example:

String foo = null;
String bar = foo ?? "mystring";

bar will be set to "mystring" not null.

See http://en.wikipedia.org/wiki/C_Sharp_syntax#Null_coalesce_operator
10/27/2010 9:10 AM | josh
Gravatar

# re: C# If Else Shortcut

The last example isn't correct, the the null-coalescing operator ("??") can't be used for that.
10/27/2010 9:31 AM | Erwin
Gravatar

# re: C# If Else Shortcut

Your last line isn't valid code, when compiling for under .NET 3.5 it gives the error:
Operator '??' cannot be applied to operands of type 'bool' and 'int'

Anyway the "??" operator is a check against null, see http://msdn.microsoft.com/en-us/library/ms173224.aspx, or am I missing something?
10/27/2010 11:22 AM | Matt Warren
Gravatar

# Null Coalescing (?? operator)

I'm afraid your final tip of using ?? as a shorthand for an if statement is incorrect: this is the null coalescing operator, not a straight conditional like the ternary ?: operator.

Null coalescing allows you to fix up a situation where you're using a variable that might be null, e.g.:

public void Foo(string bar) {
if (bar == null) {
bar = "null";
} else {
bar = "ok";
}
}

could be written as

public void Foo(String bar) {
bar = "ok" ?? "null";
}

I.e., "use the thing before the ?? if the object is not null, otherwise use the thing after." So your tip says "if (foo < five) is not null, foo = (foo < 5), otherwise foo = 6". (foo < 5) will never be null, as it is a boolean, and you will also hit compiler errors because you're trying to assign a boolean (foo < 5) to an int (foo).

Indeed, trying it out in VS, I get a syntax error before compilation, complaining that the left operand should be of reference or nullable type.

Alastair
10/27/2010 11:37 AM | Alastair Smith
Gravatar

# re: C# If Else Shortcut

You are right about the ?: ternarry operator, but not about the ?? null-coalescing operator.

The ?? is null-coalescing operator which means if the expression (foo < 5 in your case) results in null then the value/object after ?? will be used instead of null. This operator can only be applied between nullable value types or reference types. ?? is by no means an alternative for ternary operator.

If I am correct, foo < 5 in "foo = foo < 5 ?? 6;" returns a bool not a bool? (nullable type) so it should result in a compile time error about ?? cannot be applied between bool and int.


Check this link for information about null-coalescing operator
http://msdn.microsoft.com/en-us/library/ms173224.aspx
10/27/2010 10:35 PM | Sai Puli
Gravatar

# re: C# If Else Shortcut

You flipped the > in your ternary operator example.

Also ?? is the null-coalescing operator. Your "foo < 5 ? 6" will not compile. The left side is a boolean and the right is an int. ?? will only return the right side if the left side is null. It's typically used for providing a default value in case something is null.

return name ?? "default name";
10/28/2010 12:39 AM | Ryan
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 
 

Powered by: