Using Optional Parameters in C#
📖 Scenario: You are creating a simple greeting program that can say hello to a person. Sometimes you want to include their title, and sometimes you just want to say hello with their name only.
🎯 Goal: Build a method called Greet that uses optional parameters to greet a person with or without their title.
📋 What You'll Learn
Create a method called
Greet with two parameters: string name and an optional string title.The optional parameter
title should have a default value of an empty string.If
title is provided, the greeting should include it before the name.If
title is not provided, the greeting should just use the name.Call the
Greet method twice: once with both name and title, and once with only name.Print the greetings to the console.
💡 Why This Matters
🌍 Real World
Optional parameters help make your methods flexible and easier to use when some information is not always needed.
💼 Career
Understanding optional parameters is important for writing clean, maintainable code in many C# applications, including web, desktop, and mobile development.
Progress0 / 4 steps