

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- Lambda expression in Java 8
- Match Expression in PHP 8
- How to write a JSP Expression?
- 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 the comparator as a lambda expression in Java?
- How to write strings with new lines in R?
- New Date-Time API in Java 8
- How to read and write unicode (UTF-8) files in Python?
- How to write JavaScript Regular Expression for multiple matches?
- How to write lambda expression code for SwingUtilities.invokeLater in Java?
- How to write a Regular Expression in JavaScript to remove spaces?
- How to write a Python regular expression to use re.findall()?
- How to write a Python Regular Expression to validate numbers?
Advertisements