Function parameters in C# are variables defined in a method that receive values when the method is called. They allow you to pass data into methods and use it inside the method.
Syntax:
return_type MethodName(data_type parameter1, data_type parameter2)
{
// method body
}
Example: Below is function that takes two parameters of integer datatype and prints their sum:
using System;
class Program {
// parameters
static void Add(int a, int b) {
Console.WriteLine(a + b);
}
static void Main() {
// arguments
Add(3, 5);
}
}
Output
8
Types of Functions Parameters in C#
- Value Parameters
- Ref Parameters
- Out Parameters
- In Parameters
- Default (Optional) Parameters
- Named Parameters
- Params
1. Value Parameters
Value parameters pass a copy of the variable’s data to the method. Any changes made inside the method do not affect the original variable.
using System;
class Program
{
static void Main(){
int num = 10;
Increment(num);
// 10 (unchanged)
Console.WriteLine(num);
}
static void Increment(int value)
{
value++;
}
}
Output
10
Changes are local to the method because the argument is passed by value.
2. Ref Parameters
The ref keyword passes arguments by reference, allowing the called method to modify the original variable's value.
Rules:
- The variable must be initialized before passing to ref.
- Changes made inside the method reflect in the calling scope.
using System;
class Program
{
static void Main()
{
string name = "Dog";
Update(ref name);
Console.WriteLine(name); // Cat
}
static void Update(ref string text)
{
if (text == "Dog")
Console.WriteLine("Matched!");
text = "Cat";
}
}
Output
Matched! Cat
3. Out Parameters
The out keyword is used to pass parameters by reference, mainly to return multiple values from a method.
Rules:
- The variable need not be initialized before passing.
- The method must assign a value before returning.
using System;
class Program
{
static void Main()
{
int sum;
Add(out sum);
Console.WriteLine(sum); // 80
}
static void Add(out int result)
{
result = 40;
result += result;
}
}
Output
80
4. In Parameters
The in keyword passes arguments by reference but does not allow modification inside the method.
using System;
class Program
{
static void Show(in int x)
{
Console.WriteLine(x);
}
static void Main()
{
int num = 10;
Show(num);
}
}
5. Default (Optional) Parameters
Optional parameters allow you to omit arguments when calling a method. Each optional parameter has a default value, which is used when no argument is provided.
Rules:
- Must be defined after all required parameters.
- Can be omitted at the time of the call.
using System;
class Program
{
static void Print(string name = "Guest")
{
Console.WriteLine(name);
}
static void Main()
{
Print();
Print("Aman");
}
}
Output
Guest Aman
6. Named Parameters
Named parameters allow you to pass arguments by specifying the parameter names, instead of relying on their order. It was introduced in C# 4.0.
using System;
class Program
{
static void AddStrings(string first, string middle, string last)
{
Console.WriteLine(first + middle + last);
}
static void Main()
{
AddStrings(first: "Geeks", last: "Geeks", middle: "for");
}
}
Output
GeeksforGeeks
Named parameters must appear after all positional arguments in a method call.
7. Params Parameters
The params keyword allows a method to accept a variable number of arguments of the same type.
Rules:
- Only one params parameter is allowed per method.
- Must be the last parameter in the definition.
using System;
class Program
{
static int Multiply(params int[] numbers)
{
int result = 1;
foreach (int n in numbers)
result *= n;
return result;
}
static void Main()
{
Console.WriteLine(Multiply(2, 3, 4));
Console.WriteLine(Multiply());
}
}
Output
24 1