
- 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 difference between using if/else and switch-case in C#?
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.
Switch statement is quicker. The switch statement the average number of comparisons will be one regardless of how many different cases you have So lookup of an arbitrary case is O(1)
Using Switch −
Example
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; } Console.ReadLine(); } Using If else class Program{ public enum Fruits { Red, Green, Blue } public static void Main(){ Fruits c = (Fruits)(new Random()).Next(0, 3); if (c == Fruits.Red) Console.WriteLine("The Fruits is red"); else if (c == Fruits.Green) Console.WriteLine("The Fruits is green"); else if (c == Fruits.Blue) Console.WriteLine("The Fruits is blue"); else Console.WriteLine("The Fruits is unknown."); Console.ReadLine(); } }
- Related Articles
- Difference Between if-else and switch
- Using range in switch case in C/C++
- Else and Switch Statements with initializers in C++17
- How to override multiple if-else conditions using a switch statement in TypeScript?
- What is Switch...case statement in JavaScript?
- Switch case statement in C
- What is the ‘if...else if...else’ statement in Java and how to use it?
- Difference between Router and Switch
- Difference between Hub and Switch
- Difference between Gateway and Switch
- Converting digits to word format using switch case in C language
- What is if...else if... statement in JavaScript?
- Difference between One-Way Switch and Two-Way Switch
- What's the difference between a context switch, a process switch and a thread switch in Linux?
- Explain nested switch case in C language

Advertisements