Learn C# Programming

Master C# programming from basics to advanced concepts with our comprehensive tutorial series. Perfect for beginners and experienced developers.

C# switch Statement

The switch statement allows you to execute different blocks of code based on the value of a variable.

Basic switch Statement

int dayNumber = 3;

switch (dayNumber)
{
    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;
    case 6:
        Console.WriteLine("Saturday");
        break;
    case 7:
        Console.WriteLine("Sunday");
        break;
    default:
        Console.WriteLine("Invalid day");
        break;
}

switch with String

string grade = "A";

switch (grade)
{
    case "A":
        Console.WriteLine("Excellent!");
        break;
    case "B":
        Console.WriteLine("Good job!");
        break;
    case "C":
        Console.WriteLine("Average");
        break;
    case "D":
        Console.WriteLine("Below average");
        break;
    case "F":
        Console.WriteLine("Failed");
        break;
    default:
        Console.WriteLine("Invalid grade");
        break;
}

Multiple Cases (Fall-through)

char operation = '+';

switch (operation)
{
    case '+':
    case 'a':
        Console.WriteLine("Addition");
        break;
    case '-':
    case 's':
        Console.WriteLine("Subtraction");
        break;
    case '*':
    case 'm':
        Console.WriteLine("Multiplication");
        break;
    case '/':
    case 'd':
        Console.WriteLine("Division");
        break;
    default:
        Console.WriteLine("Unknown operation");
        break;
}

switch Expression (C# 8.0+)

// Traditional switch
string GetDayType(int day)
{
    return day switch
    {
        1 or 2 or 3 or 4 or 5 => "Weekday",
        6 or 7 => "Weekend",
        _ => "Invalid day"
    };
}

// Pattern matching
string DescribeObject(object obj)
{
    return obj switch
    {
        int i when i > 0 => "Positive integer",
        int i when i < 0 => "Negative integer",
        int => "Zero",
        string s when s.Length > 0 => "Non-empty string",
        string => "Empty string",
        null => "Null value",
        _ => "Unknown type"
    };
}

Enum with switch

enum Status
{
    Pending,
    Approved,
    Rejected,
    Cancelled
}

Status currentStatus = Status.Approved;

switch (currentStatus)
{
    case Status.Pending:
        Console.WriteLine("Waiting for approval");
        break;
    case Status.Approved:
        Console.WriteLine("Request approved");
        break;
    case Status.Rejected:
        Console.WriteLine("Request rejected");
        break;
    case Status.Cancelled:
        Console.WriteLine("Request cancelled");
        break;
}

💡 Best Practices

  • Always include a default case
  • Don't forget break statements to prevent fall-through
  • Use switch expressions for simple mappings
  • Consider using if-else for complex conditions
  • Group related cases together

Frequently Asked Questions

C# is a modern, object-oriented programming language developed by Microsoft. It's part of the .NET framework and is used for developing various applications including web applications, desktop software, mobile apps, and games.

No! You can start learning C# immediately using our online compiler. For advanced development, you can install the .NET SDK and Visual Studio or VS Code on your computer.

Yes! C# has a clean syntax and strong type system that makes it beginner-friendly. Our tutorial starts from the basics and gradually progresses to advanced topics.

C# is versatile and can be used to build web applications (ASP.NET), desktop applications (WPF, WinForms), mobile apps (Xamarin), games (Unity), APIs, microservices, and much more.

The time varies based on your programming background. Complete beginners can learn the basics in 2-3 months with consistent practice. Our structured tutorial helps you progress systematically.

Master C# Programming with Our Comprehensive Tutorial

Our C# programming tutorial is designed to take you from a complete beginner to an advanced developer. Whether you're looking to build web applications, desktop software, or mobile apps, C# provides the foundation you need for modern software development.

Start with the basics like variables, data types, and control structures, then progress to advanced topics including object-oriented programming, LINQ, async programming, and more. Each lesson includes practical examples and exercises to reinforce your learning.

Join thousands of developers who have learned C# programming through our step-by-step tutorials. Begin your journey today and unlock the power of .NET development.