C# अभ्यास

Switch Expression Calculator

This exercise combines tuples, expressions, and exhaustive operator handling.

How it works

Build a calculator with a C# switch expression.

Matching the operator selects exactly one calculation, while the discard arm rejects unsupported values.

Runnable C# example

C#
Run code →
Program.cs
using System;
class Program
{
    static double Calculate(double a, string op, double b) => op switch
    {
        "+" => a + b, "-" => a - b, "*" => a * b,
        "/" when b != 0 => a / b,
        _ => throw new ArgumentException("Unsupported operation")
    };
    static void Main() => Console.WriteLine(Calculate(6, "*", 7));
}
Expected output
42

Practice with the debugger

Set a breakpoint on an important assignment or condition, click Debug, and inspect the local values before stepping to the next line.