Coding/Learn/csharp/method parameters
C# Foundational Course
01.Why C# & .NET Power Modern Software
02.Anatomy of a C# Application
03.Variables & The C# Type System
04.Console I/O: Reading & Writing
05.String Interpolation & Modern Formatting
06.Arithmetic, Math Class & Modulo
07.Conditional Branching: if, else if, and else
08.Logical Operators (&&, ||, !)
09.Switch Statements & Modern Switch Expressions
10.While & Do-While Loops
11.The Standard For Loop & Counting
12.Loop Control: break and continue
13.Methods: Modular, Reusable Code Blocks
14.Optional Parameters, Named Arguments & out/ref
15.Fixed-Size Arrays & Zero-Based Indexing
16.The Sacred Foreach Loop
17.Dynamic Collections: List<T> & Generics
18.Classes & Objects: The OOP Blueprint
19.Auto-Properties & Constructors
20.Capstone: RPG Character & Inventory Engine
Course Certificate0%

Complete all lessons to earn

LearnOptional Parameters, Named Arguments & out/ref
14 / 20 0%
Lesson 14 of 20
5 mins read

Optional Parameters, Named Arguments & out/ref

PreviousNext Lesson
In Plain English

C# offers powerful parameter capabilities: optional parameters with default fallback values, named arguments for clarity at the callsite, and out/ref keywords for returning multiple values and passing by reference.

The Mental Model

"Optional parameters are like a food delivery app where napkins and sauce default to "yes" unless you specifically override them. Named arguments are like labeling every item in your grocery order so nobody confuses the flour with powdered sugar."

Deep Dive: How It Works

Optional Parameters: static void Log(string msg, string level = "INFO") { ... } (level defaults to "INFO" if omitted).

Named Arguments: Log(level: "ERROR", msg: "Database timeout"); (improves readability for methods with many parameters).

out Parameters: Allows a method to return multiple values: static bool TryDivide(int a, int b, out int result) { ... }.

ref Parameters: Passes variable by reference, modifying the caller's original variable directly.

Syntax Blueprint

// Optional Parameter
static void Connect(string host, int port = 8080) { }

// out Parameter
static void GetCoords(out int x, out int y) {
  x = 10; y = 20;
}

Default parameter values and multi-value out parameters.

PointCore Rules to Remember

Point star
Optional at the End: Optional parameters must appear after all required parameters in the signature.
Point star
out Must Be Assigned: A method using out parameters MUST assign a value before returning.
Point star
Named Arguments Clarity: Disambiguates methods with multiple boolean flags.

Common Beginner Traps & How to Fix Them

Mistake:Placing optional parameters before required parameters: void Test(int a = 5, int b)

Why it happens: Not knowing the ordering rule.

How to fix: Always place optional parameters at the end: void Test(int b, int a = 5).

Pro Tip: Use int.TryParse(text, out int num) to safely parse user numbers without try/catch overhead!

Live Interactive Example

Hit Run Code to see it live
Optional Parameters & Out Keywords in Action
C# 12 / .NET
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Output Console
Click "Run Code" to view the rendered output.
How it works: Demonstrates optional parameter fallback and out parameter extraction.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Create a greeting with default title

  • PointWrite static void Greet(string name, string title = "Guest") that prints "Welcome, {title} {name}!".
  • PointIn Main(), call Greet("Alex") without providing the second argument.
1
2
3
4
5
6
7
8
9
10
11
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates