Geeks With Blogs

News View Steve Michelotti's profile on LinkedIn

Mark Pearl


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;
    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;
Posted on Wednesday, October 27, 2010 6:49 AM C# | Back to top

Comments on this post: C# If Else Shortcut

# re: C# If Else Shortcut
Requesting Gravatar...
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.

Left by josh on Oct 27, 2010 9:10 AM

# re: C# If Else Shortcut
Requesting Gravatar...
The last example isn't correct, the the null-coalescing operator ("??") can't be used for that.
Left by Erwin on Oct 27, 2010 9:31 AM

# re: C# If Else Shortcut
Requesting Gravatar...
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, or am I missing something?
Left by Matt Warren on Oct 27, 2010 11:22 AM

# Null Coalescing (?? operator)
Requesting Gravatar...
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.

Left by Alastair Smith on Oct 27, 2010 11:37 AM

# re: C# If Else Shortcut
Requesting Gravatar...
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
Left by Sai Puli on Oct 27, 2010 10:35 PM

# re: C# If Else Shortcut
Requesting Gravatar...
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";
Left by Ryan on Oct 28, 2010 12:39 AM

Your comment:
 (will show your gravatar)

Copyright © MarkPearl | Powered by: