C# · Playground

C# playground

Running C# inside the browser is not available yet. Use this editor to write, then download the file and run it locally with .NET SDK 8.0. The example snippets were verified that way.

// A tide table: which of the next few hours are safe to cross the causeway?
int[] depthCm = { 35, 62, 98, 121, 104, 70, 41, 28 };
const int SafeLimit = 60;

for (int hour = 0; hour < depthCm.Length; hour++)
{
    string state = depthCm[hour] <= SafeLimit ? "open" : "closed";
    Console.WriteLine($"{hour + 6,2}:00  {depthCm[hour],3} cm  {state}");
}

int openHours = depthCm.Count(d => d <= SafeLimit);
Console.WriteLine($"Causeway open for {openHours} of {depthCm.Length} hours");

Output

Press Run to see the program’s output here.

Running C# inside the browser is not available yet. Use this editor to write, then download the file and run it locally with .NET SDK 8.0. The example snippets were verified that way.

C# examples

Each one was run with .NET SDK 8.0 before it was published. Load one, change it, run it.

  •  C# · Count down with a loop
    // A kettle timer: print each remaining second, then the boil message.
    for (int seconds = 5; seconds >= 1; seconds--)
    {
        Console.WriteLine($"{seconds}...");
    }
    Console.WriteLine("Boiled!");
    
  •  C# · Define and call a method
    // A method with a return value, called three times.
    Console.WriteLine(Discount(2000, 15));
    Console.WriteLine(Discount(999, 0));
    Console.WriteLine(Discount(450, 50));
    
    // price and result in pence, percent as a whole number
    static int Discount(int pricePence, int percent)
    {
        return pricePence - pricePence * percent / 100;
    }
    
  •  C# · Work with a List
    // A parcel queue: add, remove, inspect.
    var parcels = new List<string> { "lamp", "books", "kettle" };
    parcels.Add("plant pot");
    parcels.Remove("books");
    
    Console.WriteLine($"{parcels.Count} parcels waiting");
    foreach (string p in parcels)
    {
        Console.WriteLine($"- {p} ({p.Length} letters)");
    }
    Console.WriteLine(parcels.Contains("kettle") ? "kettle is queued" : "no kettle");
    
  •  C# · String handling
    // Tidy up a mailing label typed in a hurry.
    string raw = "   ms. Rowan Ashby , unit 4b  ";
    string tidy = raw.Trim();
    string[] parts = tidy.Split(',');
    string name = parts[0].Trim().ToUpper();
    string unit = parts[1].Trim();
    
    Console.WriteLine(name);
    Console.WriteLine(unit);
    Console.WriteLine(name.Contains("ROWAN") ? "matched" : "not matched");
    Console.WriteLine(unit.Replace("unit", "Unit").PadLeft(12, '.'));
    
  •  C# · A class with a computed property
    // Bus stops know how far they are from the depot.
    var stops = new List<BusStop>
    {
        new BusStop("Depot", 0),
        new BusStop("High Street", 1800),
        new BusStop("Old Mill", 4300),
    };
    
    foreach (BusStop s in stops)
    {
        Console.WriteLine($"{s.Name,-12} {s.DistanceKm:F1} km");
    }
    
    class BusStop
    {
        public string Name { get; }
        public int DistanceMetres { get; }
    
        public BusStop(string name, int distanceMetres)
        {
            Name = name;
            DistanceMetres = distanceMetres;
        }
    
        public double DistanceKm => DistanceMetres / 1000.0;
    }
    
  •  C# · LINQ query over records
    // Pick the fastest lap per driver from a list of lap times.
    var laps = new List<Lap>
    {
        new("Ines", 1, 92.4), new("Ines", 2, 90.1), new("Ines", 3, 90.8),
        new("Tomas", 1, 91.7), new("Tomas", 2, 89.9),
    };
    
    var fastest = laps
        .GroupBy(l => l.Driver)
        .Select(g => g.MinBy(l => l.Seconds)!)
        .OrderBy(l => l.Seconds);
    
    foreach (Lap lap in fastest)
    {
        Console.WriteLine($"{lap.Driver}: lap {lap.Number} in {lap.Seconds:F1}s");
    }
    
    // type declarations go after the top-level statements
    record Lap(string Driver, int Number, double Seconds);
    

How this page was checked. Every program on it was run with .NET SDK 8.0 at build time by the publishing checks, and the output shown is what it printed. Running C# inside the browser is not available yet, so the Run button is absent rather than pretending; copy the code and run it with .NET SDK 8.0 locally.