Question

I don't understand the syntax used in the following lines, except that it follows a basic structure of what seems to be called a ternary operator.

string path = args == null || args.Length == 0 ?
    @"C:\GENERIC\SYSTEM\PATH" :
    args[1];

I'm new to this syntax. Would someone help me translate it into real English (or pseudocode), much in the way an if statement can be turned into "if this then that"?

EDIT: Thank you everyone for your answers, you've all been extremely helpful. Unfortunately I can only vote one of you, but I'll upvote a bunch of you!

Was it helpful?

Solution 2

What you are seeing is a special conditional operator, the ternary operator. (And here is a nice tutorial)

It is used like so:

condition ? first_expression : second_expression;

Basically if the statement is true, the first expression is executed, if not, the second is. Generally speaking it is a small shortcut for if/else blocks, and should be used for only small statements. Nesting the ternary operator is largely frowned upon.

So if args == null || args.Length == 0 Then path = @"C:\GENERIC\SYSTEM\PATH", if not, it equals args[1]

It is equivalent to your standard if block

string path;
if(args == null || args.Length == 0)
{
   path = @"C:\GENERIC\SYSTEM\PATH";
}
else
{
   path = args[1];
}

OTHER TIPS

This is equivalent to

string path;
if(args == null || args.Length == 0)
    path = @"C:\GENERIC\SYSTEM\PATH" ;
else
    path = args[1];

You can break down a ternary operator to this

VariableToStoreResult = BooleanCondition ? ValueIfConditionIsTrue : ValueIfConditionIsFalse
string path = "";

if(args==null || args.Length==0)
{
   path = @"C:\GENERIC\SYSTEM\PATH";
}
else
{
   path = args[1];
}

This is a translation. Ternary operator looks like:

result = (condition)?firstResult:otherResult

your ternary operator means: if args are null or empty -> use default path | else -> use path from args

it can be rewritten as:

string path;

if(args == null || args.Length == 0)
    path = @"C:\GENERIC\SYSTEM\PATH";
else
    path = args[1];

Basically

If args is null or length of args is zero
Then
Path = "C:\Generic\System\Path"
Else
Path = args[1]

Like Jon Skeet has said in the comments, this operator is called the conditional operator. The reason behind is name is that it works very much like an if-statement. It's often called the ternary operator, because it's currently the only operator with three operands.

Now, the explanation:

int myInt = myBool ? valueWhenTrue : valueWhenFalse;

This translates into something like:

int myInt;
if(myBool)
   myInt = valueWhenTrue;
else
   myInt = valueWhenFalse;

Important note: The conditional operator can only be used for expressions (and is evaluated itself as an expression), not statements. This, for example, is invalid code:

myBool ? DoSomething() : DoSomethingElse();

The structure is quite basic

variable = value;

but now the value depends on a condition that renders true or false;

variable = condition ? true : false;

Condition can be anything, even a function that returns this true or false state.

What you see in the example you submitted is a combined condition.

string path = args == null || args.Length == 0 ?
    @"C:\GENERIC\SYSTEM\PATH" :
    args[1];

Here the conditions renders true if one of the statements in the "OR" is true

read

string path = 

(if "args == null" is true)  OR (if "args.Length == 0" is true) then value = @"C:\GENERIC\SYSTEM\PATH" 
 else
(if both false) then  value = args[1]

From high level to low level, here the operators precedence;

==, ||, ?:, =

So basicly, your code equavalent to;

string path;
if((args == null) || (args.Length == 0))
{
    path = @"C:\GENERIC\SYSTEM\PATH" ;
}
else
{
    path = args[1];
}

Take a look at ?: Operator (C# Reference)

The conditional operator (?:) returns one of two values depending on the value of a Boolean expression. Following is the syntax for the conditional operator.

condition ? first_expression : second_expression;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top