typestar

Method parameters in C#

Defaults and named arguments make call sites read like sentences.

Console.WriteLine(Greeter.Hello("Ada"));
Console.WriteLine(Greeter.Hello("Grace", shout: true));
Console.WriteLine(Greeter.Area(3, height: 4));

static class Greeter
{
    public static string Hello(string name, bool shout = false)
    {
        var text = $"hello, {name}";
        return shout ? text.ToUpper() : text;
    }

    public static double Area(double width, double height) =>
        width * height / 2;
}

How it works

  1. bool shout = false gives callers a default they can skip.
  2. shout: true names the argument, so the call explains itself.
  3. Expression-bodied Area writes a one-line method in one line.

Keywords and builtins used here

The run, in numbers

Lines
15
Characters to type
379
Tokens
98
Three-star pace
90 tpm

At the three-star pace of 90 tokens a minute, this run takes about 65 seconds.

Type this snippet

Step 1 of 3 in Methods, step 15 of 29 in Language basics.

← Previous Next →