C# Tutorial

C# String

C# Array

C# Flow Control

C# Class and Object

C# Inheritance

C# Interface

C# Collection

C# Generic

C# File I/O

C# Delegate and Event

C# Exception

C# Process and Thread

C# ADO.NET Database Operations

C# Method Parameters (actual And Formal Parameters)

In this tutorial, we will explore method parameters in C#, including the concepts of actual and formal parameters. Understanding method parameters is essential for writing modular and reusable code.

  • Formal Parameters

Formal parameters are the variables defined in a method's declaration. These variables act as placeholders for the actual values that will be passed into the method when it is called. The formal parameters are used within the method to perform calculations or other operations based on the provided arguments.

Consider the following example:

public int Add(int a, int b)
{
    return a + b;
}

In this example, a and b are formal parameters of the Add method. They represent the two integer values that will be added together when the method is called.

  • Actual Parameters

Actual parameters, also known as arguments, are the values that are passed into a method when it is called. The actual parameters are matched with the formal parameters in the method declaration based on their position.

Here's an example of calling the Add method with actual parameters:

int result = Add(3, 5);

In this case, 3 and 5 are the actual parameters. When the Add method is called, the value 3 is assigned to the formal parameter a, and the value 5 is assigned to the formal parameter b. The method then returns the sum of a and b, which is 8.

  • Pass by Value and Pass by Reference

C# supports two ways of passing arguments to methods: pass by value and pass by reference.

  • Pass by Value: In pass by value, a copy of the actual parameter's value is created and passed to the method. Changes made to the formal parameter within the method do not affect the original value of the actual parameter. This is the default behavior for value types (e.g., int, double, bool) and objects.
public void Increment(int number)
{
    number++;
}

In this example, the Increment method does not modify the original value of the input argument because the formal parameter is passed by value.

  • Pass by Reference: In pass by reference, a reference to the actual parameter's memory location is passed to the method. Changes made to the formal parameter within the method also affect the original value of the actual parameter. To pass an argument by reference, use the ref or out keyword.
public void Increment(ref int number)
{
    number++;
}

In this example, the Increment method modifies the original value of the input argument because the formal parameter is passed by reference.

  • Optional and Named Parameters

C# also supports optional and named parameters:

  • Optional Parameters: You can provide default values for parameters, making them optional when calling the method. To define an optional parameter, use the = operator followed by the default value.
public int Add(int a, int b = 0)
{
    return a + b;
}

In this example, the b parameter is optional, and its default value is 0.

  • Named Parameters: You can pass arguments to a method using their parameter names, allowing you to specify values for specific parameters regardless of their position.
int result = Add(a: 3, b: 5);

In this example, the actual parameters are passed using their parameter names, making the order of the arguments irrelevant.

This tutorial covers method parameters in C#, including the concepts of actual and formal parameters, pass by value and pass by reference, and optional and named parameters.

  1. How to define method parameters in C#

    Method parameters in C# are defined within parentheses after the method name. Example:

    void PrintMessage(string message)
    {
        Console.WriteLine(message);
    }
    
  2. Passing parameters in C# methods

    void DisplayInfo(string name, int age)
    {
        Console.WriteLine($"Name: {name}, Age: {age}");
    }
    
    // Invoking the method
    DisplayInfo("John", 25);
    
  3. C# method parameters examples

    void CalculateSum(int a, int b)
    {
        int sum = a + b;
        Console.WriteLine($"Sum: {sum}");
    }
    
  4. Default parameters in C# methods

    void PrintInfo(string name, int age = 30)
    {
        Console.WriteLine($"Name: {name}, Age: {age}");
    }
    
  5. Optional parameters in C#

    Optional parameters allow omitting arguments during method invocation:

    void PrintGreeting(string greeting, string name = "Guest")
    {
        Console.WriteLine($"{greeting}, {name}!");
    }
    
  6. Named parameters in C#

    void DisplayRectangle(int length, int width)
    {
        Console.WriteLine($"Length: {length}, Width: {width}");
    }
    
    // Named parameter usage
    DisplayRectangle(width: 10, length: 5);
    
  7. C# method parameters by reference

    Passing parameters by reference using ref or out:

    void IncrementByReference(ref int number)
    {
        number++;
    }
    
  8. Passing arrays as parameters in C#

    void PrintArray(int[] numbers)
    {
        foreach (var number in numbers)
            Console.Write($"{number} ");
    }
    
  9. Method parameters and method overloading in C#

    Method overloading involves defining multiple methods with the same name but different parameter lists:

    void DisplayInfo(string name)
    {
        Console.WriteLine($"Name: {name}");
    }
    
    void DisplayInfo(string name, int age)
    {
        Console.WriteLine($"Name: {name}, Age: {age}");
    }
    
  10. C# method parameters and type checking

    void DisplayPerson(object person)
    {
        if (person is Person)
        {
            var realPerson = (Person)person;
            Console.WriteLine($"Name: {realPerson.Name}");
        }
        else
        {
            Console.WriteLine("Invalid person type");
        }
    }
    
  11. Parameter arrays in C# methods

    Using the params keyword to allow a variable number of arguments:

    void PrintNumbers(params int[] numbers)
    {
        foreach (var number in numbers)
            Console.Write($"{number} ");
    }
    
  12. Method parameters and scope in C#

    Parameters have a scope limited to the method in which they are defined:

    void PrintValue(int value)
    {
        // value is only accessible within this method
        Console.WriteLine($"Value: {value}");
    }