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
bool shout = falsegives callers a default they can skip.shout: truenames the argument, so the call explains itself.- Expression-bodied
Areawrites a one-line method in one line.
Keywords and builtins used here
AreaGreeterHelloboolclassdoublefalsepublicreturnstaticstringtruevar
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.
Step 1 of 3 in Methods, step 15 of 29 in Language basics.