Click here to Skip to main content
15,880,972 members
Articles / Programming Languages / C#
Tip/Trick

Using C# Ternary (?) Operator Statement as a Method Parameter

Rate me:
Please Sign up or sign in to vote.
3.08/5 (8 votes)
25 Aug 2014Public Domain2 min read 89.7K   3   22
Using C# Ternary (?) operator statement as a method parameter

Introduction

The '?' operator is one of the oldest conditional operators that has been coming along with object oriented languages such as C++, Java and C#. The most significant advantage of using it is, it will reduce a couple of lines of code than using the traditional if-else or switch statements, where there is a need to return values.

I first came across the functionality and power of using this, just a couple of years ago, while assigned with a task to re-factor some existing code of a senior. More often, I have seen the use of this being used with assigning the results to variables. And subsequently, my coding behaviour also followed suit.

Recently, I was wondering if the usage could only be limited to assigning variables, and tried adding the statement directly to a method passing it as a variable. It just works fine, since as long as the returning type matches the variable type of the method parameter, such an assignment succeeds. Though this is maybe already a straight forward understanding for C# or Java experts, yet this explanation could serve for those in doubt, and to understand the beauty of the language. Ok, without any further exaggeration, let's have a look at what I am talking about.

Let’s assume you have a method to display a name by passing in a name parameter like the following:

Code

C#
void DisplayName(string name)
{
//
}

Now simply let's assume two names, 'Ahmed' and 'Jack', and we are to display the name which has less than 5 characters and pass that name to the above method to be further processed. Using the ternary operator, the approach would be:

Code

C#
string name1 = "Ahmed";
string name2 = "Jack";
var nameLessThanFive = name1.Length < 5 ? name1: name2;
DisplayName(nameLessThanFive);

Eliminating the intermediate variable assignment, this could be achieved also as the following:

Code

C#
DisplayName(name1.Length < 5 ? name1: name2);

Note: This approach would work only for methods that pass arguments by value and not for those that pass arguments by reference (i.e., the arguments with ref and out keywords will not work).

Personally, I would not recommend the over usage of this approach on method parameters as it could negate the very purpose of simplicity of reading (i.e., you might end up with a longer line in the method parameters mixed up with '?'s, ':'s and ","s thereby making it complex to read).

I find the following scenarios where you can advocate this approach:

  1. Methods that have parameters which have a need to pass arguments which require results from a condition.
  2. Using this approach on no more than two or three arguments of a method to reduce reading complexity.
  3. Using this approach on methods that could be called on parts of other lengthy conditional statements such as switch or if-else. This way, we could save a lot of unnecessary variable assignments prior to method calls.

That's it. I would like to hear opinions of others on this. Happy coding ahead...!

Source originally from my blog: http://maturedknowledge.blogspot.com/2014/08/using-c-ternary-operator-statement-as.html.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Software Developer (Senior) ASSETTE
Sri Lanka Sri Lanka
Getting fascinated with discovering new stuff.

Comments and Discussions

 
Question[My vote of 2] Observations Pin
ozbear27-Aug-14 12:53
ozbear27-Aug-14 12:53 
AnswerRe: [My vote of 2] Observations Pin
mafaz32127-Aug-14 18:30
professionalmafaz32127-Aug-14 18:30 
QuestionMy vote of 2 Pin
Member 188040327-Aug-14 1:32
Member 188040327-Aug-14 1:32 
AnswerRe: My vote of 2 Pin
mafaz32127-Aug-14 1:41
professionalmafaz32127-Aug-14 1:41 
GeneralMy vote of 2 Pin
Member 188040327-Aug-14 1:24
Member 188040327-Aug-14 1:24 
GeneralMy vote of 2 Pin
PeejayAdams26-Aug-14 2:28
PeejayAdams26-Aug-14 2:28 
GeneralRe: My vote of 2 Pin
mafaz32126-Aug-14 6:03
professionalmafaz32126-Aug-14 6:03 
Question[My vote of 2] Improve terminology usage. Pin
frankazoid26-Aug-14 0:39
frankazoid26-Aug-14 0:39 
AnswerRe: [My vote of 2] Improve terminology usage. Pin
mafaz32126-Aug-14 6:06
professionalmafaz32126-Aug-14 6:06 
Suggestion[My vote of 2] You are not passing the Ternary (?) Operator as your title suggests Pin
DelphiCoder25-Aug-14 21:57
DelphiCoder25-Aug-14 21:57 
GeneralRe: [My vote of 2] You are not passing the Ternary (?) Operator as your title suggests Pin
mafaz32125-Aug-14 23:09
professionalmafaz32125-Aug-14 23:09 
GeneralRe: [My vote of 2] You are not passing the Ternary (?) Operator as your title suggests Pin
DelphiCoder26-Aug-14 15:28
DelphiCoder26-Aug-14 15:28 
>To be clearer we are passing the 'Ternary Operator Statement' and not the 'Ternary Operator' here.

Thanks for your reply, but that is not the case. The compiler is evaluating your IF STATEMENT (Ternary Operator in this case) **before** the method gets called. Therefore the Ternary Operator isn't being passed as a parameter at all.

Even in this case:
C#
DisplayName(name1.Length < 5 ? name1: name2);
The if statement is evaluated fully and then the method call is made. Again, the Ternary Operator isn't passed in.

Later...
GeneralRe: [My vote of 2] You are not passing the Ternary (?) Operator as your title suggests Pin
mafaz32126-Aug-14 18:04
professionalmafaz32126-Aug-14 18:04 
GeneralRe: [My vote of 2] You are not passing the Ternary (?) Operator as your title suggests Pin
DelphiCoder1-Sep-14 22:56
DelphiCoder1-Sep-14 22:56 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun25-Aug-14 19:20
Humayun Kabir Mamun25-Aug-14 19:20 
GeneralMy vote of 1 Pin
Samer Aburabie25-Aug-14 17:47
Samer Aburabie25-Aug-14 17:47 
GeneralRe: My vote of 1 Pin
mafaz32125-Aug-14 18:18
professionalmafaz32125-Aug-14 18:18 
GeneralRe: My vote of 1 Pin
Samer Aburabie25-Aug-14 18:52
Samer Aburabie25-Aug-14 18:52 
GeneralThoughts Pin
PIEBALDconsult25-Aug-14 16:46
mvePIEBALDconsult25-Aug-14 16:46 
GeneralRe: Thoughts Pin
mafaz32125-Aug-14 18:26
professionalmafaz32125-Aug-14 18:26 
GeneralRe: Thoughts Pin
PIEBALDconsult26-Aug-14 5:01
mvePIEBALDconsult26-Aug-14 5:01 
GeneralRe: Thoughts Pin
mafaz32126-Aug-14 6:11
professionalmafaz32126-Aug-14 6:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.