
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
What is a Ternary operator/conditional operator in C#?
Ternary operator is a Conditional operator in C#. It takes three arguments and evaluates a Boolean expression.
For example −
y = (x == 1) ? 70 : 100;
Above, if the first operand evaluates to true (1), the second operand is evaluated. If the first operand evaluates to false (0), the third operand is evaluated.
The following is an example −
Example
using System; namespace DEMO { class Program { static void Main(string[] args) { int a, b; a = 10; b = (a == 1) ? 20 : 30; Console.WriteLine("Value of b is {0}", b); b = (a == 10) ? 20 : 30; Console.WriteLine("Value of b is {0}", b); Console.ReadLine(); } } }
Output
Value of b is 30 Value of b is 20
- Related Articles
- Conditional ternary operator ( ?: ) in C++
- Does Python have a ternary conditional operator?
- How to implement ternary conditional operator in MySQL?
- What is a ternary operator (?:) in JavaScript?
- What is ternary operator in C#?
- What is Conditional Operator (?:) in JavaScript?
- What is the conditional operator ?: in Java?
- What is ternary operator (? X : Y) in C++?
- Ternary Operator in Java
- Ternary Operator in Python?
- Ternary Operator in C#
- C/C++ Ternary Operator
- Java Ternary Operator Examples
- Ternary Operator in Dart Programming
- Changing ternary operator into non-ternary - JavaScript?

Advertisements