C# > Core C# > Operators and Expressions > Operator Precedence and Associativity
Conditional Operator Precedence
This snippet illustrates the precedence of the conditional operator (?:) in relation to other operators, specifically the assignment operator (=). It's crucial to understand how these operators interact to avoid unexpected results. It also shows the precedence of the null-coalescing operator.
Code Demonstrating Conditional Operator Precedence
The code shows that the conditional operator has lower precedence than the assignment operator. Therefore, the entire conditional expression `a > b ? a : b` is evaluated first, and then its result is assigned to the variable `result`. In the null-coalescing example, `name ?? "Guest"` evaluates to "Guest" because `name` is null. If `name` had a value, that value would be assigned to `displayName`.
using System;
public class ConditionalOperatorPrecedence
{
public static void Main(string[] args)
{
int a = 5;
int b = 10;
int result;
// Assignment and Conditional Operator
// The conditional operator has lower precedence than the assignment operator.
result = a > b ? a : b; //Evaluates to result = (a > b ? a : b)
Console.WriteLine($"a > b ? a : b = {result}"); //output 10, b is greater than a and it gets assigned to result
//Null-Coalescing operator.
string name = null;
string displayName = name ?? "Guest";
Console.WriteLine($"displayName = {displayName}"); //output Guest
string existingName = "John";
string anotherDisplayName = existingName ?? "Guest";
Console.WriteLine($"anotherDisplayName = {anotherDisplayName}"); //output John
}
}
Concepts Behind the Snippet
The conditional operator (?:) provides a concise way to write simple if-else statements. The null-coalescing operator (??) provides a default value when the left-hand operand is null. Operator precedence dictates how these operators interact with other operators in complex expressions. Assignment operators have a lower precedence than conditional and null-coalescing operator. The null-coalescing operator is right-associative.
Real-Life Use Case
The conditional operator is commonly used for simple value selection based on a condition. The null-coalescing operator is frequently used to provide default values for variables that might be null, preventing NullReferenceExceptions. For example, in web applications, you might use the null-coalescing operator to provide a default username if the user is not logged in.
Best Practices
When to use them
Use the conditional operator for simple conditional assignments or value selections. Use the null-coalescing operator when you need to provide a default value for a potentially null variable. These operators improve code conciseness and readability when used appropriately.
FAQ
-
What is the precedence of the conditional operator compared to the assignment operator?
The conditional operator (?:) has lower precedence than the assignment operator (=). Therefore, the conditional expression is evaluated before the assignment is made. -
What is the null-coalescing operator?
The null-coalescing operator (??) returns the value of its left-hand operand if it is not null; otherwise, it returns the value of its right-hand operand. -
Is the null-coalescing operator left-associative?
No, the null-coalescing operator is right-associative.