Method parameters are variables defined in the method declaration inside parentheses after the method name. They specify the values that a method can receive when it is called and can be of different data types such as int, double, string, bool, etc. The values passed to these parameters are called arguments.
- Parameters allow methods to accept data and perform operations using the received values.
- A method can have multiple parameters, and the arguments passed during the method call should match their order and compatible data types.
Example: Program to demonstrates a method that takes a string as a parameter and returns a string value.
using System;
class GFG
{
static void Main()
{
// Call method and pass value to the parameter
string str = Example("GFG!");
// Print the returned value
Console.WriteLine(str);
}
// para is a parameter of type string
// The method returns a string value
static string Example(string para)
{
return "Hello " + para;
}
}
Output
Hello GFG!
Here, a and b are parameters, while 10 and 20 are arguments.
Types of Method Parameters in C#
C# supports different types of parameters that provide flexibility when passing data to methods.
1. Value Parameters
By default, C# passes parameters by value. This means a copy of the value is passed to the method. Changes made to the parameter inside the method do not affect the original variable.
using System;
class GFG
{
static void ChangeValue(int x)
{
x = 100;
}
static void Main()
{
int num = 50;
ChangeValue(num);
Console.WriteLine(num);
}
}
Output
50
Here, the value of num remains 50 because a copy of its value is passed to the method.
2. Reference Parameters
The ref keyword is used to pass a parameter by reference. It allows the method to work with the original variable, so changes made inside the method affect the original value.
using System;
class GFG
{
static void ChangeValue(ref int x)
{
x = 100;
}
static void Main()
{
int num = 50;
ChangeValue(ref num);
Console.WriteLine(num);
}
}
Output
100
Here, the ref keyword allows the ChangeValue() method to modify the original num variable.
Note: A variable must be initialized before it is passed using the ref keyword.
3. Output Parameters
The out keyword is used when a method needs to return a value through a parameter. An out parameter does not need to be initialized before it is passed to the method.
using System;
class GFG
{
static void GetValue(out int x)
{
x = 100;
}
static void Main()
{
int num;
GetValue(out num);
Console.WriteLine(num);
}
}
Output
100
Here, the GetValue() method assigns a value to the out parameter, which can then be accessed through the num variable.
Note: A method must assign a value to an out parameter before the method returns.
4. Parameter Arrays (params)
The params keyword allows a method to accept a variable number of arguments. It is useful when we do not know in advance how many values will be passed to the method.
using System;
class GFG
{
static int Sum(params int[] numbers)
{
int total = 0;
foreach (int number in numbers)
{
total += number;
}
return total;
}
static void Main()
{
int result = Sum(10, 20, 30, 40);
Console.WriteLine(result);
}
}
Output
100
Here, the params keyword allows the Sum() method to accept multiple integer arguments.
Note: The params parameter must be the last parameter in the method declaration.
5. Optional Parameters
Optional parameters are parameters that are not compulsory to pass when calling a method. They have a default value, which is used automatically when no value is provided. They provide flexibility by allowing a method to be called with fewer arguments when needed.
- They allow us to omit arguments for certain parameters while calling a method.
- Optional parameters are always declared after the required parameters.
using System;
class GFG
{
static void Scholar(string name, int age = 20)
{
Console.WriteLine("Name: " + name);
Console.WriteLine("Age: " + age);
}
static void Main()
{
// Uses the default value of age
Scholar("Ankita");
// Provides a value for age
Scholar("Siya", 25);
}
}
Output
Name: Ankita Age: 20 Name: Siya Age: 25
Here, age is an optional parameter with a default value of 20. When no value is provided, C# automatically uses the default value.
Parameters and Arguments
Parameters and arguments are related but have different meanings.
- A parameter is a variable defined in the method declaration.
- An argument is the actual value passed to the method when it is called.
using System;
class GFG
{
static int Add(int a, int b)
{
return a + b;
}
static void Main()
{
int result = Add(10, 20);
Console.WriteLine(result);
}
}
Output
30
In above example, a and b are parameters, while 10 and 20 are arguments.
Multiple Parameters
A method can have multiple parameters of the same or different data types.
using System;
class GFG
{
static void Student(string name, int age, double percentage)
{
Console.WriteLine("Name: " + name);
Console.WriteLine("Age: " + age);
Console.WriteLine("Percentage: " + percentage);
}
static void Main()
{
Student("Ankita", 20, 85.5);
}
}
Output
Name: Ankita Age: 20 Percentage: 85.5
In above example, the Student() method contains three parameters: name, age, and percentage.
The arguments passed during the method call must be provided in the corresponding order and must be compatible with their parameter types.
Number of Parameters in C#
C# does not specify a simple fixed limit such as 255 parameters for a method. The actual limit depends on the C# compiler, .NET runtime, and metadata restrictions. However, using a very large number of parameters is generally not recommended, as it makes the method difficult to read, understand, and maintain.
Instead of creating a method with many parameters:
static void Student(
string name,
int age,
string branch,
string city,
string college)
{
// Method body
}
we can group related information into a class:
class Student
{
public string Name { get; set; }
public int Age { get; set; }
public string Branch { get; set; }
public string City { get; set; }
public string College { get; set; }This approach makes the code easier to manage when many related values are required.