Business Intelligence Blogs

View blogs by industry experts on topics such as SSAS, SSIS, SSRS, Power BI, Performance Tuning, Azure, Big Data and much more! You can also sign up to post your own business intelligence blog.

Ternary Operators - C# basics

  • 10 July 2013
  • Author: RCharCox
  • Number of views: 9167
  • 0 Comments

I learned a new word today.  Ternary.  Specifically speaking, ternary operators.  In programming languages, we all know that code comes down to basic conditions, right?  If a situation exists or is true, then do something else.  Well, I came across a snippet of code today in one of our products that used this little magic statement.

 

public void TernaryOperatorExamples()
        {
            var varInt = 10000;
            bool myResult;
            var example = 10 > varInt ? myResult = true : myResult = false;
        }
If written in the traditional if/else format, it would look something like this:
 public void TradtionalConditional()
        {
            var varInt = 10000;
            bool myresult;
            var example = 10;

            if (example > varInt)
            {
                myResult = true;
            }
            else
            {
                myResult = false;
            }
        }
As you can see, it is much more concise to use the ternary operators here.  However, that’s not to say that we should all kick the traditional conditionals to the curb; if the readability suffers, or it becomes difficult to maintain, you know you should go back. 

 

I know it’s a short one, but I’m getting back into the swing of writing.  More to come, good people of the internet!
Print
Categories: Analysis Services
Tags:
Rate this article:
No rating

RCharCoxRCharCox

Other posts by RCharCox

Please login or register to post comments.