In C#, default parameters (also called optional parameters) allow you to define parameters that do not require a value to be passed when the method is called. These parameters have default values that are used when no argument is provided for them. This feature enables you to call methods with fewer arguments and simplifies the method signature.
Default Parameters in C#
When you define a method, you can specify a default value for one or more parameters. If the caller does not provide an argument for that parameter, the default value is used.
Syntax
returnType MethodName(type param1 = defaultValue, type param2 = defaultValue)
{
// Method body
}
defaultValue is the value that will be used if no argument is provided for that parameter when the method is called.
Example
using System;
class Calculator
{
// Method with default parameters
public void Add(int x, int y = 5)
{
Console.WriteLine($"The result of adding {x} and {y} is: {x + y}");
}
}
class Program
{
static void Main()
{
Calculator calc = new Calculator();
// Calling with both arguments
calc.Add(10, 15); // Output: The result of adding 10 and 15 is: 25
// Calling with only one argument, using default value for y
calc.Add(10); // Output: The result of adding 10 and 5 is: 15
}
}
Explanation:
Use Case for Default Parameters
User Preferences: Consider a method that displays a message. You might want the method to have a default color for the text, but you also want the ability to change the color if needed.
public void DisplayMessage(string message, string color = "blue")
{
Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), color, true);
Console.WriteLine(message);
Console.ResetColor();
}
Usage
// Using the default color (blue)
DisplayMessage("Hello World!");
// Using a custom color
DisplayMessage("Warning!", "Red");
Advantages of Default Parameters
In C#, default parameters (also called optional parameters) allow you to define parameters that do not require a value to be passed when the method is called. These parameters have default values that are used when no argument is provided for them. This feature enables you to call methods with fewer arguments and simplifies the method signature.
Default Parameters in C#
When you define a method, you can specify a default value for one or more parameters. If the caller does not provide an argument for that parameter, the default value is used.
Syntax
returnType MethodName(type param1 = defaultValue, type param2 = defaultValue) { // Method body }
defaultValue is the value that will be used if no argument is provided for that parameter when the method is called.
Example
using System; class Calculator { // Method with default parameters public void Add(int x, int y = 5) { Console.WriteLine($"The result of adding {x} and {y} is: {x + y}"); } } class Program { static void Main() { Calculator calc = new Calculator(); // Calling with both arguments calc.Add(10, 15); // Output: The result of adding 10 and 15 is: 25 // Calling with only one argument, using default value for y calc.Add(10); // Output: The result of adding 10 and 5 is: 15 } }
Explanation:
Use Case for Default Parameters
User Preferences: Consider a method that displays a message. You might want the method to have a default color for the text, but you also want the ability to change the color if needed.
public void DisplayMessage(string message, string color = "blue") { Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), color, true); Console.WriteLine(message); Console.ResetColor(); }
Usage
// Using the default color (blue) DisplayMessage("Hello World!"); // Using a custom color DisplayMessage("Warning!", "Red");
Advantages of Default Parameters
C# is primarily used on the Windows . NET framework, although it can be applied to an open source platform. This highly versatile programming language is an object-oriented programming language (OOP) and comparably new to the game, yet a reliable crowd pleaser.
The C# language is also easy to learn because by learning a small subset of the language you can immediately start to write useful code. More advanced features can be learnt as you become more proficient, but you are not forced to learn them to get up and running. C# is very good at encapsulating complexity.
The decision to opt for C# or Node. js largely hinges on the specific requirements of your project. If you're developing a CPU-intensive, enterprise-level application where stability and comprehensive tooling are crucial, C# might be your best bet.
C# is part of .NET, a free and open source development platform for building apps that run on Windows, macOS, Linux, iOS, and Android. There's an active community answering questions, producing samples, writing tutorials, authoring books, and more.
Copyrights © 2024 letsupdateskills All rights reserved