1. Basic Example
int number = 3;
switch (number)
{
case 1:
Console.WriteLine("Number is 1");
break;
case 2:
Console.WriteLine("Number is 2");
break;
case 3:
Console.WriteLine("Number is 3");
break;
default:
Console.WriteLine("Number not recognized");
break;
}
Output
Number is 3
2. Switch with Multiple Case Label
In some cases, you may want to execute the same code for multiple values. You can combine multiple values in a single case.'
int dayOfWeek = 2;
switch (dayOfWeek)
{
case 1:
case 2:
case 3:
case 4:
case 5:
Console.WriteLine("Weekday");
break;
case 6:
case 7:
Console.WriteLine("Weekend");
break;
default:
Console.WriteLine("Invalid day");
break;
}
Output
Weekday
3. Switch with Patterns (C# 7.0 and Later)
Starting with C# 7.0, you can use pattern matching in switch statements. This allows more flexible matching, such as checking the type of an object or applying more complex conditions.
Type Pattern Matching Example:
object obj = 42;
switch (obj)
{
case int i:
Console.WriteLine($"Integer: {i}");
break;
case string s:
Console.WriteLine($"String: {s}");
break;
default:
Console.WriteLine("Unknown type");
break;
}
Result
Integer: 42
4. Constant Pattern Matching (C# 9.0 and Later):
You can also match against specific constants with the switch statement.
int month = 3;
switch (month)
{
case 1:
case 2:
case 3:
Console.WriteLine("Winter");
break;
case 4:
case 5:
case 6:
Console.WriteLine("Spring");
break;
default:
Console.WriteLine("Other season");
break;
}
Result
Winter
5. Switch Expression (C# 8.0 and Later)
In C# 8.0, you can use the switch as an expression, where it returns a value instead of simply executing statements. This is particularly useful for assigning values based on conditions
int number = 2;
var result = number switch
{
1 => "One",
2 => "Two",
3 => "Three",
_ => "Unknown" // _ is the default case
};
Console.WriteLine(result);
Result
Two
6.Null Checking in Switch Expression (C# 9.0 and Later)
You can also use switch expressions to check for null values and other patterns more easily.
string input = null;
var result = input switch
{
null => "Input is null",
string s when s.Length > 0 => "Non-empty string",
_ => "Empty string"
};
Console.WriteLine(result);
Result
Input is null
1. Basic Example
int number = 3; switch (number) { case 1: Console.WriteLine("Number is 1"); break; case 2: Console.WriteLine("Number is 2"); break; case 3: Console.WriteLine("Number is 3"); break; default: Console.WriteLine("Number not recognized"); break; }
Output
Number is 3
2. Switch with Multiple Case Label
In some cases, you may want to execute the same code for multiple values. You can combine multiple values in a single case.'
int dayOfWeek = 2; switch (dayOfWeek) { case 1: case 2: case 3: case 4: case 5: Console.WriteLine("Weekday"); break; case 6: case 7: Console.WriteLine("Weekend"); break; default: Console.WriteLine("Invalid day"); break; }
Output
Weekday
3. Switch with Patterns (C# 7.0 and Later)
Starting with C# 7.0, you can use pattern matching in switch statements. This allows more flexible matching, such as checking the type of an object or applying more complex conditions.
Type Pattern Matching Example:
object obj = 42; switch (obj) { case int i: Console.WriteLine($"Integer: {i}"); break; case string s: Console.WriteLine($"String: {s}"); break; default: Console.WriteLine("Unknown type"); break; }
Result
Integer: 42
4. Constant Pattern Matching (C# 9.0 and Later):
You can also match against specific constants with the switch statement.
int month = 3; switch (month) { case 1: case 2: case 3: Console.WriteLine("Winter"); break; case 4: case 5: case 6: Console.WriteLine("Spring"); break; default: Console.WriteLine("Other season"); break; }
Result
Winter
5. Switch Expression (C# 8.0 and Later)
In C# 8.0, you can use the switch as an expression, where it returns a value instead of simply executing statements. This is particularly useful for assigning values based on conditions
int number = 2; var result = number switch { 1 => "One", 2 => "Two", 3 => "Three", _ => "Unknown" // _ is the default case }; Console.WriteLine(result);
Result
Two
6.Null Checking in Switch Expression (C# 9.0 and Later)
You can also use switch expressions to check for null values and other patterns more easily.
string input = null; var result = input switch { null => "Input is null", string s when s.Length > 0 => "Non-empty string", _ => "Empty string" }; Console.WriteLine(result);
Result
Input is null
C# is primarily used on the Windows . NET framework, although it can be applied to an open source platform. This highly versatile programming language is an object-oriented programming language (OOP) and comparably new to the game, yet a reliable crowd pleaser.
The C# language is also easy to learn because by learning a small subset of the language you can immediately start to write useful code. More advanced features can be learnt as you become more proficient, but you are not forced to learn them to get up and running. C# is very good at encapsulating complexity.
The decision to opt for C# or Node. js largely hinges on the specific requirements of your project. If you're developing a CPU-intensive, enterprise-level application where stability and comprehensive tooling are crucial, C# might be your best bet.
C# is part of .NET, a free and open source development platform for building apps that run on Windows, macOS, Linux, iOS, and Android. There's an active community answering questions, producing samples, writing tutorials, authoring books, and more.
Copyrights © 2024 letsupdateskills All rights reserved