![]() |
VOOZH | about |
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:
8
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.
10
Changes are local to the method because the argument is passed by value.
The ref keyword passes arguments by reference, allowing the called method to modify the original variable's value.
Rules:
Matched! Cat
The out keyword is used to pass parameters by reference, mainly to return multiple values from a method.
Rules:
80
The in keyword passes arguments by reference but does not allow modification inside the method.
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:
Guest Aman
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.
GeeksforGeeks
Named parameters must appear after all positional arguments in a method call.
The params keyword allows a method to accept a variable number of arguments of the same type.
Rules:
24 1