C# Positional Parameters, Named Parameters and Default Values Explained

C# Positional Parameters, Named Parameters and Default Values Explained

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.

Watch Video Explanation

Here is the full video tutorial on this topic:

👉 Click here to watch the video on YouTube


1. Positional Parameters with Default Values

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 supplied
  • DisplayInfo("Bob"); — Age and city use default values
  • DisplayInfo("Charlie", city: "Los Angeles"); — Mixing positional and named

2. Named Parameters in C#

Named 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.


3. Combining Positional and Named Parameters

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:

  • Positional + named: SetEmployeeInfo("Eve", 35, position: "Manager", department: "HR");
  • Defaults used: SetEmployeeInfo("John", 28);

4. Methods Where All Parameters Have Default Values

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 used
  • PrintProductInfo("Laptop", quantity: 2);
  • PrintProductInfo("Phone", 5, 299.99);

Summary

  • Positional parameters — order matters
  • Named parameters — order does not matter
  • Default values — make parameters optional

Learn C# with 1-to-1 Training

I provide personalised online and offline 1-to-1 classes on:

  • C# Programming
  • ASP.NET Core
  • MVC and Web API
  • Blazor
  • SQL Server
  • Interview Preparation

To learn more and contact me, visit:

🌐 https://supernovaservices.com

Class Guidelines for Effective 1-on-1 Learning

To keep every session productive and distraction-free, please follow these simple guidelines:

  • Quiet Environment: Join from a calm, private room with minimal background noise. Avoid public or noisy places.
  • No Interruptions: Inform family/roommates in advance. Keep doors closed during class.
  • Mobile on Silent / DND: Set your phone to Silent or Do Not Disturb to prevent calls and notifications.
  • Be Fully Present: Do not multitask. Avoid attending to other calls, visitors, or errands during the session.
  • Stable Setup: Use a laptop/desktop with a stable internet connection and required software installed (Visual Studio/.NET, SQL Server, etc.).
  • Punctuality: Join on time so we can utilize the full session effectively.
  • Prepared Materials (If any): Keep project files, notes, and questions ready for quicker progress.

Following these guidelines helps you focus better and ensures I can deliver the best learning experience in every class.

Schedule a Quick 10-Minute Call

I prefer to start with a short 10-minute free call so I can understand:

  • Your learning objectives and career goals
  • Your current skill level
  • The exact topics you want to learn

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.

Google Review Testimonials

.NET Online Training
Average Rating: 4.9
Votes: 50
Reviews: 50