codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🔷

C# Programming

14 / 60 topics
13Methods in C#14Parameters and Arguments15Return Values from Methods16Recursion in C#
Tutorials/C# Programming/Parameters and Arguments
🔷C# Programming

Parameters and Arguments

Updated 2026-04-20
3 min read

Introduction

In C#, methods (often called functions) are blocks of code that perform specific tasks. To make methods versatile and reusable, you can pass data into them. This data is passed using Parameters and Arguments.

Understanding how data flows into and out of methods is crucial for writing clean, modular, and maintainable C# code.

Concept

Parameters vs Arguments

While often used interchangeably, there is a distinct technical difference between the two terms:

  • Parameter: A variable defined in the method signature. It acts as a receiver for the data.
  • Argument: The actual value or data you pass into the method when you invoke (call) it.
// 'name' is the Parameter
public void Greet(string name) 
{
    Console.WriteLine($"Hello, {name}!");
}

// "Alice" is the Argument
Greet("Alice"); 

Passing Mechanisms

C# supports several ways to pass arguments to methods:

  1. Pass by Value (Default): A copy of the value is passed. Changes made inside the method do not affect the original variable.
  2. Pass by Reference (ref and out): A reference to the memory location is passed. Changes made inside the method do affect the original variable.
  3. Named Arguments: Allows you to pass arguments in any order by explicitly stating the parameter name.
  4. Optional Parameters: Allows you to omit arguments by providing default values in the parameter list.
  5. Variable number of arguments (params): Allows passing a dynamic number of arguments.

Examples

1. Pass by Value (Default Behavior)

By default, value types (like int, double, bool, structs) are passed by value. The method receives a duplicate copy.

public class Program
{
    public static void Main()
    {
        int originalNumber = 10;
        Console.WriteLine($"Before method: {originalNumber}");
        
        ModifyNumber(originalNumber);
        
        // The original variable remains unchanged!
        Console.WriteLine($"After method: {originalNumber}");
    }

    public static void ModifyNumber(int num)
    {
        num = num * 2;
        Console.WriteLine($"Inside method: {num}");
    }
}
Output
Before method: 10
Inside method: 20
After method: 10

2. Pass by Reference using ref

If you want the method to permanently modify the original variable passed in, you use the ref keyword. The variable must be initialized before being passed as an argument.

public class Program
{
    public static void Main()
    {
        int score = 50;
        Console.WriteLine($"Before method: {score}");
        
        // You must include the 'ref' keyword when calling the method
        AddBonus(ref score);
        
        Console.WriteLine($"After method: {score}");
    }

    // You must include the 'ref' keyword in the parameter list
    public static void AddBonus(ref int currentScore)
    {
        currentScore += 100;
    }
}
Output
Before method: 50
After method: 150

3. The out Keyword

The out modifier is similar to ref, but it doesn't require the variable to be initialized before it is passed. However, the method must assign a value to the out parameter before it exits. It is heavily used to return multiple values from a single method.

public class Program
{
    public static void Main()
    {
        // Notice we don't initialize these variables
        int sum;
        int product;
        
        Calculate(5, 4, out sum, out product);
        
        Console.WriteLine($"Sum: {sum}, Product: {product}");
    }

    public static void Calculate(int a, int b, out int addResult, out int multResult)
    {
        addResult = a + b;
        multResult = a * b;
        // The method will not compile if addResult or multResult are left unassigned.
    }
}
Output
Sum: 9, Product: 20

4. Optional Parameters and Named Arguments

You can provide default values for parameters, making them optional. When calling the method, you can use Named Arguments to skip certain optional parameters while providing others.

public class Program
{
    public static void Main()
    {
        // Using all default values
        CreateProfile("JohnDoe");
        
        // Providing arguments sequentially
        CreateProfile("JaneDoe", 25, "Canada");
        
        // Using Named Arguments to skip 'age' but provide 'country'
        CreateProfile("Alex", country: "UK");
    }

    // 'age' and 'country' are optional parameters
    public static void CreateProfile(string username, int age = 18, string country = "USA")
    {
        Console.WriteLine($"User: {username}, Age: {age}, Country: {country}");
    }
}
Output
User: JohnDoe, Age: 18, Country: USA
User: JaneDoe, Age: 25, Country: Canada
User: Alex, Age: 18, Country: UK

5. The params Keyword

The params keyword allows a method to accept a variable number of arguments of the same type. It must be the last parameter in the method signature.

public class Program
{
    public static void Main()
    {
        PrintShoppingList("Apples", "Bananas", "Milk", "Bread");
        PrintShoppingList("Eggs");
    }

    // Accepts any number of strings
    public static void PrintShoppingList(params string[] items)
    {
        Console.WriteLine("--- Shopping List ---");
        foreach (string item in items)
        {
            Console.WriteLine("- " + item);
        }
    }
}
Output
--- Shopping List ---
- Apples
- Bananas
- Milk
- Bread
--- Shopping List ---
- Eggs

What's Next?

Mastering method parameters allows you to write highly flexible code. Now that you understand ref, out, and params, you can dive into more advanced C# features such as Method Overloading, where you define multiple methods with the same name but entirely different parameter signatures!


PreviousMethods in C#Next Return Values from Methods

Recommended Gear

Methods in C#Return Values from Methods