Ad space (banner)
#๏ธโƒฃC# Lessons
Lesson 26 / 68

Default Parameter Values

In this lesson you will learn how to give a C# parameter a default value, so callers can leave it out. This is for anyone searching for "C# default parameter how to use".

Writing = value on a parameter sets a default that is used automatically when the caller omits that argument. Picture an order form where leaving a field blank gets you the usual house set.

The sample code defines void Greet(string name = "Guest"), which handles both Greet("Alice"), where a name is supplied, and Greet(), where nothing is passed.

A common stumbling block for beginners is the ordering rule. When several parameters have defaults, the ones without a default value have to come first.

In real-world development, letting callers omit arguments gives you a flexible method that serves both the simple case and the finely tuned one.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run C# directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)