
- 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 ternary operator in C#?
Ternary operator is a Conditional operator in C#. It takes three arguments and evaluates a Boolean expression.
For example −
y = (z == 1) ? 100 : 180;
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 x, y; x = 25; y = (x == 25) ? 20 : 30; Console.WriteLine("Value of x = {0}", y); y = (x == 1) ? 50 : 90; Console.WriteLine("Value of y = {0}", y); Console.ReadLine(); } } }
Above we have two conditions using the ternary operators −
y = (x == 25) ? 20 : 30; y = (x == 1) ? 50 : 90;
- Related Articles
- What is a Ternary operator/conditional operator in C#?
- What is a ternary operator (?:) in JavaScript?
- What is ternary operator (? X : Y) in C++?
- Ternary Operator in Java
- Ternary Operator in Python?
- Ternary Operator in C#
- Changing ternary operator into non-ternary - JavaScript?
- Conditional ternary operator ( ?: ) in C++
- Ternary Operator in Dart Programming
- C/C++ Ternary Operator
- Java Ternary Operator Examples
- How Ternary operator in PowerShell Works?
- Is there an equivalent of C’s “?:” ternary operator in Python?
- How to overload python ternary operator?
- Difference between the Ternary operator and Null coalescing operator in php

Advertisements