0Pricing
C# Academy · Lesson

Optional/named params; ref/out/in (basics)

Use optional parameters and named arguments for clarity; pass by reference with ref; return extra values with out; note that in parameters are not available in C# 6.

Lesson overview

Goal: Use optional parameters and named arguments for readability; understand ref and out. Note: in parameters are not available in C# 6.

Optional params

Add defaults for simple customization without many overloads.

using System;

public static class Notifier
{
  // Optional parameter: suffix has a default value
  public static void Notify(string message, string suffix = "!")
  {
    Console.WriteLine(message + suffix);
  }
}

public class Program
{
  public static void Main(string[] args)
  {
    Notifier.Notify("Saved");          // uses "!"
    Notifier.Notify("Warning", "!!");  // overrides default
  }
}

All lessons in this course

  1. Signatures, returns; expr-bodied; optional/named; ref/out; overloading
  2. Optional/named params; ref/out/in (basics)
  3. Local functions (C# 6 note) & overloading
← Back to C# Academy