In C#, methods can accept arguments in multiple flexible ways using positional parameters, named parameters, and default parameter values. Understanding these features helps developers write more readable code and avoid confusion when calling functions. In this guide, we will break down each concept with examples and clear explanations.
Here is the full video tutorial on this topic:
👉 Click here to watch the video on YouTube
In positional parameters, the values are passed based on their order in the method call. C# allows you to assign default values to parameters, so callers do not need to provide all arguments.
public static void DisplayInfo(string name, int age = 30, string city = "Unknown")
{
Console.WriteLine($"Name: {name}, Age: {age}, City: {city}");
}
How the method call works:
DisplayInfo("Alice", 25, "New York"); — All parameters suppliedDisplayInfo("Bob"); — Age and city use default valuesDisplayInfo("Charlie", city: "Los Angeles"); — Mixing positional and namedNamed parameters allow you to specify which argument corresponds to which parameter by using the parameter name.
public static void DisplayDetails(string name, int age, string city)
{
Console.WriteLine($"Name: {name}, Age: {age}, City: {city}");
}
Example call:
DisplayDetails(city: "Paris", age: 28, name: "Diana");
This improves readability and allows arguments to be passed in any order.
C# allows mixing positional and named parameters, but positional arguments must always come first.
public static void SetEmployeeInfo(string name, int age, string position = "Developer", string department = "IT")
{
Console.WriteLine($"Name: {name}, Age: {age}, Position: {position}, Department: {department}");
}
Example calls:
SetEmployeeInfo("Eve", 35, position: "Manager", department: "HR");SetEmployeeInfo("John", 28);You can assign default values to every parameter, making all of them optional.
public static void PrintProductInfo(string product = "Unknown", int quantity = 1, double price = 10.0)
{
Console.WriteLine($"Product: {product}, Quantity: {quantity}, Price: ${price}");
}
Function calls:
PrintProductInfo(); — All defaults usedPrintProductInfo("Laptop", quantity: 2);PrintProductInfo("Phone", 5, 299.99);I provide personalised online and offline 1-to-1 classes on:
To learn more and contact me, visit:
🌐 https://supernovaservices.com
To keep every session productive and distraction-free, please follow these simple guidelines:
Following these guidelines helps you focus better and ensures I can deliver the best learning experience in every class.
I prefer to start with a short 10-minute free call so I can understand:
Why? Because course content, teaching pace, and fees all depend on your needs — there’s no “one-size-fits-all” pricing. Please leave your details below, and I’ll get back to you to arrange a convenient time for the call.
Note: Payment is made only after your first class, once you’re completely satisfied. However, fees paid after the first class are non-refundable. This helps maintain scheduling commitments and allows me to reserve your preferred time slot with full attention.