DevAcademia
C++C#CPythonJava
  • C# Basics

  • C# Introduction
  • C# Get Started
  • C# Syntax
  • C# Output
  • C# Comments
  • C# Variables
  • C# Data Types
  • C# Type Casting
  • C# User Input
  • C# Operators
  • C# Math
  • C# Strings
  • C# Booleans
  • C# If...Else
  • C# Switch Statement
  • C# While Loop
  • C# For Loop
  • C# Break and Continue
  • C# Arrays
  • C# Files
  • C# OOP

  • C# OOP Introduction
  • C# Classes and Objects
  • C# Class Members
  • C# Constructors
  • C# Destructors
  • C# Access Modifiers
  • C# Properties
  • C# Inheritance
  • C# Polymorphism
  • C# Abstraction
  • C# Interfaces
  • C# Enums
  • C# Exceptions
  • C# Quizzes

  • C# Quiz Introduction
  • C# Basics

  • C# Introduction
  • C# Get Started
  • C# Syntax
  • C# Output
  • C# Comments
  • C# Variables
  • C# Data Types
  • C# Type Casting
  • C# User Input
  • C# Operators
  • C# Math
  • C# Strings
  • C# Booleans
  • C# If...Else
  • C# Switch Statement
  • C# While Loop
  • C# For Loop
  • C# Break and Continue
  • C# Arrays
  • C# Files
  • C# OOP

  • C# OOP Introduction
  • C# Classes and Objects
  • C# Class Members
  • C# Constructors
  • C# Destructors
  • C# Access Modifiers
  • C# Properties
  • C# Inheritance
  • C# Polymorphism
  • C# Abstraction
  • C# Interfaces
  • C# Enums
  • C# Exceptions
  • C# Quizzes

  • C# Quiz Introduction

Loading Cs tutorial…

Loading content
C# BasicsTopic 29 of 55
←PreviousPrevNextNext→

C# Switch Statement

Introduction to Switch

The switch statement in C# is used to execute one block of code from multiple possible options based on the value of an expression.

It is a cleaner alternative to multiple if-else conditions when you are checking a single variable against different constant values.

Syntax of Switch

The switch statement evaluates an expression and matches it with case labels.

Each case should end with a 'break' (or return, throw, or goto) to prevent fall-through unless explicitly intended.

Example
using System;

namespace SwitchExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int day = 3;

            switch (day)
            {
                case 1:
                    Console.WriteLine("Monday");
                    break;
                case 2:
                    Console.WriteLine("Tuesday");
                    break;
                case 3:
                    Console.WriteLine("Wednesday");
                    break;
                case 4:
                    Console.WriteLine("Thursday");
                    break;
                case 5:
                    Console.WriteLine("Friday");
                    break;
                default:
                    Console.WriteLine("Weekend");
                    break;
            }
        }
    }
}
Output
Wednesday

Switch with Strings

In addition to numbers, C# switch statements support string values.

This makes it useful for handling text-based options in a structured way.

Example
using System;

namespace SwitchString
{
    class Program
    {
        static void Main(string[] args)
        {
            string color = "red";

            switch (color)
            {
                case "red":
                    Console.WriteLine("Color is Red");
                    break;
                case "blue":
                    Console.WriteLine("Color is Blue");
                    break;
                default:
                    Console.WriteLine("Unknown color");
                    break;
            }
        }
    }
}
Output
Color is Red

Modern Switch Expressions (C# 8.0+)

C# 8.0 introduced switch expressions, which provide a more concise syntax.

They allow returning values directly instead of writing full case blocks.

Example
using System;

namespace SwitchExpressionExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int day = 3;

            string result = day switch
            {
                1 => "Monday",
                2 => "Tuesday",
                3 => "Wednesday",
                4 => "Thursday",
                5 => "Friday",
                _ => "Weekend"
            };

            Console.WriteLine(result);
        }
    }
}
Output
Wednesday
Test your knowledge: C# Switch Statement
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
C# BasicsTopic 29 of 55
←PreviousPrevNextNext→