tagged [conditional-operator]

Benefits of using the conditional ?: (ternary) operator

Benefits of using the conditional ?: (ternary) operator What are the benefits and drawbacks of the ?: operator as opposed to the standard if-else statement. The obvious ones being: - - - - Readability...

5 Jun at 03:28

Ternary operator in PowerShell

Ternary operator in PowerShell From what I know, PowerShell doesn't seem to have a built-in expression for the so-called [ternary operator](https://en.wikipedia.org/wiki/%3F:). For example, in the C l...

What is the OR operator in an IF statement

What is the OR operator in an IF statement In C#, how do I specify OR: I couldn't find it in the help. My code is: and my error is: > Error 1 Operator '||' cannot be applied to operands of type 'bool...

2 Feb at 17:5

What is the difference between logical and conditional AND, OR in C#?

What is the difference between logical and conditional AND, OR in C#? > [What is the diffference between the | and || or operators?](https://stackoverflow.com/questions/35301/what-is-the-diffference-...

Ternary operator behaviour inconsistency

Ternary operator behaviour inconsistency Following expression is ok But when you use it like below, syntax error occurs Cannot implicitly convert type 'int' to 'short'. An explicit conversion exists (...

C# 6.0 multiple identical null conditional operator checks vs single traditional check

C# 6.0 multiple identical null conditional operator checks vs single traditional check Which out of the following two equivalent ways would be best for the null conditional operator in terms of primar...

6 Dec at 12:22

Nullable types and the ternary operator: why is `? 10 : null` forbidden?

Nullable types and the ternary operator: why is `? 10 : null` forbidden? I just came across a weird error: Then, in another method, something like this: Simple, if the method returns true, assign 10 t...

20 Apr at 08:19

Does the "?." operator do anything else apart from checking for null?

Does the "?." operator do anything else apart from checking for null? As you might know, `DateTime?` does not have a parametrized `ToString` (for the purposes of formatting the output), and doing some...

Multiple Ternary Operators

Multiple Ternary Operators I need a bit of syntax help with a ternary operator which will help me to put the correct marker icons on to my good map. I have three areas 0, 1 and 2 which have unique ico...

16 May at 09:21

Nullable type issue with ?: Conditional Operator

Nullable type issue with ?: Conditional Operator Could someone explain why this works in C#.NET 2.0: ...but this doesn't: The latter form gives me an compile error "Type of conditional expression cann...

why do we prefer ? to ?? operator in c#?

why do we prefer ? to ?? operator in c#? I recently found that we can use ?? operator to check nulls. Please check the below code samples: This is exactly similar to I checked my whole project source ...

9 Oct at 12:55

What is the idiomatic Go equivalent of C's ternary operator?

What is the idiomatic Go equivalent of C's ternary operator? In C/C++ (and many languages of that family), a common idiom to declare and initialize a variable depending on a condition uses the ternary...

`elif` in list comprehension conditionals

`elif` in list comprehension conditionals Consider this example: Rather than `print`ing the results, I want to use a list comprehension to create a list of results, like `['yes', 'no', 'idle', 'idle',...

How to do one-liner if else statement?

How to do one-liner if else statement? Please see [https://golangdocs.com/ternary-operator-in-golang](https://golangdocs.com/ternary-operator-in-golang) as pointed by @accdias (see comments) Can I wri...

26 Sep at 07:26

"Cannot be determined because there is no implicit conversion" with ternery if return

"Cannot be determined because there is no implicit conversion" with ternery if return I have the following ASP.NET Web Api 2 action with a ternary if return: I receive a > Type of conditional expressi...

Using conditional operator in lambda expression in ForEach() on a generic List?

Using conditional operator in lambda expression in ForEach() on a generic List? Is it not allowed to have a conditional operator in a lambda expression in ForEach? ``` List items = new List{"Item 1",...

Trying to understand ?. (null-conditional) operator in C#

Trying to understand ?. (null-conditional) operator in C# I have this very simple example: ``` class Program { class A { public bool B; } static void Main() { System.Collections.Arra...

? operator without else-part

? operator without else-part I use C# ? operator when I have if-statements that affects one row and it's all good. But lets say I have this code (using classic if-statements): This can be achieved on ...

24 Jul at 13:18

PHP if not statements

PHP if not statements This may be the way my server is set up, but I'm banging my head against the wall. I'm trying to say that if `$action` has no value or has a value that is not "add" or "delete" t...

Legible or not: C# multiple ternary operators + Throw if unmatched

Legible or not: C# multiple ternary operators + Throw if unmatched Do you find the following C# code legible? ``` private bool CanExecuteAdd(string parameter) { return this.Script == null ? fals...

C# Conditional Operator Not a Statement?

C# Conditional Operator Not a Statement? I have a simple little code fragment that is frustrating me: At compile time, it generates the error: > Only assignment, call, increment, decrement, and new ob...

24 Sep at 21:32

twig: IF with multiple conditions

twig: IF with multiple conditions It seem I have problem with a twig if statement. The error is: I can't understand why this doesn't work, it's like if twig was lost with all the pipes. I've tried thi...

5 Oct at 00:43

What is the preferred order for operands in boolean expressions?

What is the preferred order for operands in boolean expressions? Is there any benefit to structuring boolean expressions like: I have always used the second way, always putting the variable as the fir...

13 Feb at 06:58

Is C# 6 ?. (Elvis op) thread safe? If so, how?

Is C# 6 ?. (Elvis op) thread safe? If so, how? Apologies in advance: this question comes from a hard-core, unreformed C++ developer trying to learn advanced C#. Consider the following: This is obvious...

3 Mar at 23:54

HttpContext.Current.Session unclear behaviour boolean

HttpContext.Current.Session unclear behaviour boolean I'm having a weird behaviour trying to get the value of a boolean property stored at [HttpContext.Current.Session](https://msdn.microsoft.com/en-u...