C# Switch | C# Switch case | C# Switch Statement - c# - c# tutorial - c# net
C# Switch
What is C# Switch case ?
- Switch case is also another condition constructs in C# programming.
- The switch statement is a controlstatement that selects a switch section to execute from a list of values.
- Each value is called a case, and the variable being switched on is checked for each switch case.
- It also includes a default value in Default statements. If no any case matches, then Default statements executes and run the code.
Syntax:
C# Sample Code - C# Examples:
Code Explanation:
- charwikitechy = '1'; specifies the character wikitechy = 1 these holding the values of 1.
- Here,switch (wikitechy) will call the "wikitechy" in the code, which we have specified as "1".
- We have theswitch-cases as case "1", case "2" and case "3" which executes based on the function call for that wikitechy. If these cases fail it will execute the default case statement. Here break; is a statement that executes a line break in the code execution. And Console.WriteLine, the Main method specifies its behavior with the statement "WikiTechy says this is C#" to be displayed on the screen.
- If no case label contains a matching value, control is transferred to the default section.
- Here Console.ReadLine(); specifies to reads input from the console. When the user presses enter, it returns a string.
Sample C# examples - Output :
- Here in this output we display the "Wikitechy says this is C#" which specifies to console statement.