
- 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
How to write the new Switch Expression in C# 8.0?
The switch expression provides for switch-like semantics in an expression context
switch is a selection statement that chooses a single switch section to execute from a list of candidates based on a pattern match with the match expression.
The switch statement is often used as an alternative to an if-else construct if a single expression is tested against three or more conditions.
Example
New way of Writing the Switch
var message = c switch{ Fruits.Red => "The Fruits is red", Fruits.Green => "The Fruits is green", Fruits.Blue => "The Fruits is blue" };
Example 1
class Program{ public enum Fruits { Red, Green, Blue } public static void Main(){ Fruits c = (Fruits)(new Random()).Next(0, 3); switch (c){ case Fruits.Red: Console.WriteLine("The Fruits is red"); break; case Fruits.Green: Console.WriteLine("The Fruits is green"); break; case Fruits.Blue: Console.WriteLine("The Fruits is blue"); break; default: Console.WriteLine("The Fruits is unknown."); break; } var message = c switch{ Fruits.Red => "The Fruits is red", Fruits.Green => "The Fruits is green", Fruits.Blue => "The Fruits is blue" }; System.Console.WriteLine(message); Console.ReadLine(); } }
Output
The Fruits is green The Fruits is green
- Related Articles
- Name the property in the following expression $\frac{ 8}{5}+0=\frac{8}{5}=0+\frac{8}{5}$
- How to switch to new window in Selenium for Python?
- How to write a conditional expression in lambda expression in Java?
- How to write a switch statement in a JSP page?
- How to write a JSP Expression?
- How to write the comparator as a lambda expression in Java?
- How to open new tab in same browser and switch between them using Selenium?
- How to write strings with new lines in R?
- How to write lambda expression code for SwingUtilities.invokeLater in Java?
- How to write a Regular Expression in JavaScript to remove spaces?
- Divide the expression$\frac{523}{0}$
- Lambda expression in Java 8
- Match Expression in PHP 8
- How to write JavaScript Regular Expression for multiple matches?
- How to write a JavaScript RegExp to match an expression?

Advertisements