c sharp Logo

C# Method Parameter


What are Method Parameters?

Method parameters are variables that are passed as arguments to a method when it is invoked. They act as placeholders for data that will be used within the method's code. Parameters allow methods to receive input from the caller and perform operations based on that input.

Declaring Method Parameters

Method parameters are declared within the parentheses after the method name. They specify the data type and name of each parameter. For instance:

public void PrintGreeting(string name) {

  Console.WriteLine("Hello, " + name + "!");

}

 

In this example, the PrintGreeting method has one parameter named name, which is of the string data type.

Passing Arguments to Methods

When a method is invoked, arguments are passed to the corresponding parameters. The arguments should match the data type of the parameters. For example:

PrintGreeting("Alice");

 

This statement calls the PrintGreeting method and passes the argument "Alice" to the name parameter. The method will print the greeting "Hello, Alice!".

Types of Method Parameters

  • Value Parameters: The default parameter passing mode, where the value of the argument is copied into the method's parameter.
  • Reference Parameters: The parameter is a reference to the original variable, allowing the method to modify the value of the variable itself.
  • Out Parameters: Similar to reference parameters, but the parameter must be initialized before calling the method.
  • Params Parameters: Allow a variable number of arguments to be passed to the method.

Examples of Method Parameters

// Value parameter

int CalculateSum(int number1, int number2) {

  int sum = number1 + number2;

  return sum;

}

 

int result = CalculateSum(10, 20); // result = 30

 

 

// Reference parameter

void SwapValues(ref int value1, ref int value2) {

  int temp = value1;

  value1 = value2;

  value2 = temp;

}

 

int firstValue = 5;

int secondValue = 3;

SwapValues(ref firstValue, ref secondValue); // firstValue = 3, secondValue = 5

 

 

// Out parameter

void GetMaximumAndMinimum(int[] numbers, out int maximum, out int minimum) {

  maximum = numbers[0];

  minimum = numbers[0];

  for (int i = 1; i < numbers.Length; i++) {

    if (numbers[i] > maximum) {

      maximum = numbers[i];

    }

    if (numbers[i] < minimum) {

      minimum = numbers[i];

    }

  }

}

 

int[] values = { 10, 5, 2, 15 };

GetMaximumAndMinimum(values, out int maxValue, out int minValue); // maxValue = 15, minValue = 2

 

 

// Params parameter

void PrintMultipleNumbers(params int[] numbers) {

  for (int number : numbers) {

    Console.WriteLine(number);

  }

}

 

PrintMultipleNumbers(1, 3, 5, 7); // Prints 1, 3, 5, 7

 

Conclusion

Method parameters are fundamental aspects of C# programming, enabling the exchange of data between methods and promoting code reusability. They allow methods to receive input, perform operations, and modify data effectively, making them essential building blocks for creating complex and well-structured C# programs.