Basics: C# Operators

I might be the only one but I find myself, time after time, needing to go back to some of the most basic elements of programming.

Probably the element that I have checked more times than anything else, is the ternary operator.

I find it in some colleague’s code, or I find myself needing to use it, and BAM! I can’t remember how it works.

Right. So, ternary operator “?:”

What it does, is this simple operation: if a condition is true, then return one value, otherwise, return another value. For example, in the following piece of code:

t_Id != Guid.Empty ? t_Id.GetHashCode() : Id.GetHashCode();

What this means is: If Guid is NOT empty, t_Id will equal t_Id.GetHashCode(), otherwise, t_Id will equal Id.GetHashCode().

In this example, “t_Id” is the “result”, “!=Guid.Empty” is the condition and “t_Id.GetHashCode()” is “x” and “Id.GetHashCode” is “y” in the following more generic line of code:

[24.01.2020_update] What the above line means is: if t_Id is not equal to the empty Guid, return t_Id’s hash code. Otherwise, return the hash code of Id.

If the line was “var hash = t_Id != Guid.Empty ? t_Id.GetHashCode() : Id.GetHashCode();” the result of the above ternary operation would be stored in “hash”. In that example, “hash” is the “result”, “t_Id!=Guid.Empty” is the condition “a>b”, “t_Id.GetHashCode()” is “x” and “Id.GetHashCode()” is “y” in the following more generic line of code:

result = (a > b) ? x : y;

which is the same as having said:

if (a > b) { result = x; } else { result = y; }

Yes, you could have just said that. For some, the ternary operator serves to give a more readable piece of code. In any case, it looks cool.

The following image details the available operators in C#.

C Sharp Operators

The highlighted operator is another interesting “simplifier”. Called the null-coalescing operator, it is used to define a default value for nullable value types or reference types. It returns the left hand operand (much like the ternary operator) if the operand is not null, otherwise, it returns the right hand operand.

ie.

int y = x ?? -1;

which means that y will equal x, unless x is null, in which case, y will equal –1.

Another cool thing about the null-coalescing operator is that it can be used with reference types like so:

Console.WriteLine(s ?? “Unspecified”);

which means that the contents of “s” will be printed to the console, unless “s” is null, in which case, the word “Unspecified” will be printed to the console.

I overuse “if” statements in my code. These operators ought to take at least half of my ifs out of my projects.

MGR: the Intelogist

About MGR: the Intelogist

SharePoint Server developer, turned Sitefinity developer, turned Angular developer, turned SharePoint Online consultant, turned Unily consultant, turned O365 consultant... Never a dull moment!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

This site uses Akismet to reduce spam. Learn how your comment data is processed.