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.
Optional/named params; ref/out/in (basics) is a free C# Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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
}
}
Named arguments
Named arguments make calls self-documenting and allow safe reordering.
using System;
public static class Greeter
{
public static void Greet(string name, string prefix, int repeat)
{
for (int i = 0; i < repeat; i = i + 1)
{
Console.WriteLine(prefix + " " + name);
}
}
}
public class Program
{
public static void Main(string[] args)
{
// Named args improve readability and allow reordering at call site
Greeter.Greet(name: "Ada", prefix: "Hello", repeat: 2);
Greeter.Greet(repeat: 1, name: "Alan", prefix: "Hi");
}
}
ref parameter
ref passes a variable by reference; the caller must initialize it first.
using System;
public static class RefDemo
{
public static void Increment(ref int n)
{
n = n + 1; // modifies caller's variable
}
}
public class Program
{
public static void Main(string[] args)
{
int value = 5; // must be assigned before passing by ref
RefDemo.Increment(ref value);
Console.WriteLine("value = " + value); // 6
}
}
out parameter
out lets a method return extra values via parameters; the method must assign them.
using System;
public static class OutDemo
{
public static bool TryDivide(int a, int b, out int result)
{
if (b == 0)
{
result = 0; // out must be assigned before return
return false;
}
result = a / b;
return true;
}
}
public class Program
{
public static void Main(string[] args)
{
int r; // not required to be assigned before passing as out
bool ok = OutDemo.TryDivide(9, 0, out r);
Console.WriteLine("ok=" + ok + " r=" + r);
}
}
About "in" (C# 6)
Note: in (readonly by-ref) parameters arrive in later C# versions. In C# 6 you can:
- Pass by value (copy) for small types
- Use ref only when mutation is required
- Make method parameters readonly by convention (do not reassign)
ref vs out basics
Recap
Recap: Use optional params for defaults, named args for clarity, ref to mutate caller data, and out to return extra values. C# 6 does not support in parameters.
Frequently asked questions
Is the “Optional/named params; ref/out/in (basics)” lesson free?
Yes — the full text of “Optional/named params; ref/out/in (basics)” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “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. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Optional/named params; ref/out/in (basics)” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Signatures, returns; expr-bodied; optional/named; ref/out; overloading
- Optional/named params; ref/out/in (basics)
- Local functions (C# 6 note) & overloading