C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C# Named and Optional ArgumentsC# Named Arguments
This feature allows us to associate argument name with its value at the time of function calling. When we make named arguments, the arguments are evaluated in the order in which they are passed. It helps us, not to remember the order of parameters. If we know the parameters names, we can pass that in any order. Let's see an example. C# Named Arguments Example
using System;
namespace CSharpFeatures
{
public class NamedArgumentsExample
{
static string GetFullName(string firstName, string lastName)
{
return firstName + " " + lastName;
}
public static void Main(string[] args)
{
string fullName1 = GetFullName("Rahul", "Kumar"); // Without named arguments
string fullName2 = GetFullName(firstName:"Rahul", lastName:"Kumar"); // Named arguments
string fullName3 = GetFullName(lastName:"Rahul", firstName:"Kumar"); // Changing order
Console.WriteLine(fullName1);
Console.WriteLine(fullName2);
Console.WriteLine(fullName3);
}
}
}
Output Rahul Kumar Rahul Kumar Kumar Rahul C# Optional Arguments
In C#, a method may contain required or optional parameters. A method that contains optional parameters does not force to pass arguments at calling time. It means we call method without passing the arguments. The optional parameter contains a default value in function definition. If we do not pass optional argument value at calling time, the default value is used. Note: We must define optional parameters at the end of parameter list.C# Optional Arguments Example 1
using System;
namespace CSharpFeatures
{
public class OptionalArgumentsExample
{
public static void Main(string[] args)
{
add(12,12); // Passing both arguments
add(10); // Passing only required argument
}
static void add(int a, int b = 10) // second parameter is optional
{
Console.WriteLine(a+b);
}
}
}
Output 24 20 We can make any number of optional parameter s. let's see an example. C# Optional Arguments Example 2
using System;
namespace CSharpFeatures
{
public class OptionalArgumentsExample
{
public static void Main(string[] args)
{
add(12,12); // Passing both arguments
add(12); // Passing one argument
add(); // Passing No argument
}
static void add(int a = 12, int b = 12) // Making all parameters optional
{
Console.WriteLine(a+b);
}
}
}
Output 24 24 24 Make sure, all the optional parameters are placed at the end of the parameter list. Otherwise, compiler throws a compile-time error. Let's see an example. C# Optional Arguments Example 3
using System;
namespace CSharpFeatures
{
public class OptionalArgumentsExample
{
public static void Main(string[] args)
{
add(12,12); // Passing both arguments
add(12); // Passing one argument
add(); // Passing No argument
}
static void add(int a = 12, int b) // Making first parameter as optional
{
Console.WriteLine(a+b);
}
}
}
Output error CS1737: Optional parameters must appear after all required parameters
Next TopicC# Asynchronous Methods
|