In C#, the keywordsparams, in, out, and
ref are used to control how arguments are passed to methods. They allow more flexible method signatures, especially when dealing with dynamic argument counts or modifying variables inside a method. Here's a breakdown of each:
params – Variable Number of Arguments
The params keyword allows you to pass a variable number of arguments of a specified type to a method. It must be the
last parameter in the method signature, and only one params parameter is allowed.
Example:
public void PrintNumbers(params int[] numbers)
{
foreach (int number in numbers)
{
Console.WriteLine(number);
}
}
You can call it like:
PrintNumbers(1, 2, 3, 4);
Use Case: Simplifies calling methods with a list of values without explicitly creating an array.
ref – Pass by Reference (Input/Output)
The ref keyword is used when a method needs to read and modify the caller's variable. The caller must
initialize the variable before passing it.
Example:
public void DoubleValue(ref int number)
{
number *= 2;
}
int x = 10;
DoubleValue(ref x); // x is now 20
Use Case: When you want the method to change the original value passed from the caller.
out – Output Parameter (Only for Returning)
The out keyword is used to return multiple values from a method. Unlike
ref, the variable does not need to be initialized before being passed. However, the method
must assign a value before returning.
Example:
public void GetDetails(out string name, out int age)
{
name = "Alice";
age = 30;
}
string name;
int age;
GetDetails(out name, out age); // name = "Alice", age = 30
Use Case: Useful when you want to return more than one value from a method, especially before
Tuple or ValueTuple became popular.
in – Pass by Reference (Read-Only)
Introduced in C# 7.2, the in keyword passes arguments by reference, but they are
read-only inside the method. It improves performance for large value types by avoiding copies, while still preventing modification.
Example:
public void ShowPoint(in Point p)
{
Console.WriteLine($"X: {p.X}, Y: {p.Y}");
// p.X = 5; // ❌ Not allowed
}
Use Case: Best used with large structs to avoid copying but ensure immutability.
Summary Table
Keyword
Passed By
Needs Initialization?
Can Modify in Method?
Can Return Value?
params
Value
Optional
No
No
ref
Reference
Yes
Yes
Yes
out
Reference
No
Yes (must assign)
Yes
in
Reference
Yes
No
No
When to Use What?
Use params for flexible argument counts.
Use ref to read and modify a variable.
Use out to return multiple results.
Use in for performance on large structs with read-only intent.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In C#, the keywords
params,in,out, andrefare used to control how arguments are passed to methods. They allow more flexible method signatures, especially when dealing with dynamic argument counts or modifying variables inside a method. Here's a breakdown of each:params– Variable Number of ArgumentsThe
paramskeyword allows you to pass a variable number of arguments of a specified type to a method. It must be the last parameter in the method signature, and only oneparamsparameter is allowed.Example:
You can call it like:
Use Case: Simplifies calling methods with a list of values without explicitly creating an array.
ref– Pass by Reference (Input/Output)The
refkeyword is used when a method needs to read and modify the caller's variable. The caller must initialize the variable before passing it.Example:
Use Case: When you want the method to change the original value passed from the caller.
out– Output Parameter (Only for Returning)The
outkeyword is used to return multiple values from a method. Unlikeref, the variable does not need to be initialized before being passed. However, the method must assign a value before returning.Example:
Use Case: Useful when you want to return more than one value from a method, especially before
TupleorValueTuplebecame popular.in– Pass by Reference (Read-Only)Introduced in C# 7.2, the
inkeyword passes arguments by reference, but they are read-only inside the method. It improves performance for large value types by avoiding copies, while still preventing modification.Example:
Use Case: Best used with large structs to avoid copying but ensure immutability.
Summary Table
paramsrefoutinWhen to Use What?
paramsfor flexible argument counts.refto read and modify a variable.outto return multiple results.infor performance on large structs with read-only intent.