Parameters allow information to be passed in and out of a method. To pass a parameter, you can include them inside the braces that follow the method name.
The syntax to declare a parameter is similar to the syntax used to declare variable. When more than one parameter is passes, they are separated using commas. When a method is called, you must pass the parameter to the method.
You can pass a parameter as a reference to the memory location. In this case, a function parameter in C# references the memory location of the parameter. A new memory location is not created in this case.
V.2.1-Value
Parameter
This is the default mechanism for passing parameters to a method. In this mechanism, when a method is called, a new storage location is created for each value parameter.
The values of the actual parameters are copied into them. Hence, the changes made to the parameter inside the method have no effect on the argument. The following example demonstrates the concept
V.2.2-Out
Parameter
V.2.3-Ref Parameter
In this guide, we will look at the difference between using in, out, and ref when passing reference and value types as parameters in C# methods. These techniques allow you to change how C# handles altering data locally in the method as well as outside the method.
Before reading this article, you should have an understanding of passing reference and value types.
V.2.4-Params Parameter
By using the params keyword, you can specify a method parameter that takes a variable number of arguments. The parameter type must be a single-dimensional array.
No additional parameters are permitted after the params keyword in a method declaration, and only one params keyword is permitted in a method declaration.
If the declared type of the params parameter is not a single-dimensional array, compiler error CS0225 occurs.
When you call a method with a params parameter, you can pass in:
- A comma-separated list of arguments of the type of the array elements.
- An array of arguments of the specified type.
- No arguments. If you send no arguments, the length of the params list is zero.
Example:
private void
btnCount_Click(object sender, EventArgs e)
{
if (txtParam1.Text==""
&& txtParam2.Text=="" &&
txtParam3.Text == "")
{
MessageBox.Show("Invalid
Paramter");
return;
}
else if (txtParam1.Text
!="" && txtParam2.Text !="" &&
txtParam3.Text != "")
{
txtResult.Text =
TestParam(txtParam1.Text, txtParam2.Text, txtParam3.Text).ToString();
}
else if (txtParam1.Text !=
"" && txtParam2.Text != "")
{
txtResult.Text =
TestParam(txtParam1.Text, txtParam2.Text, txtParam3.Text).ToString();
}
else if (txtParam1.Text !=
"" && txtParam3.Text != "")
{
txtResult.Text =
TestParam(txtParam1.Text, txtParam2.Text, txtParam3.Text).ToString();
}
else if (txtParam2.Text !=
"" && txtParam3.Text != "")
{
txtResult.Text =
TestParam(txtParam1.Text, txtParam2.Text, txtParam3.Text).ToString();
}
else if (txtParam1.Text !=
"")
{
txtResult.Text =
TestParam(txtParam1.Text).ToString();
}
else if (txtParam2.Text !=
"")
{
txtResult.Text =
TestParam(txtParam2.Text).ToString();
}
else if (txtParam3.Text !=
"")
{
txtResult.Text =
TestParam(txtParam3.Text).ToString();
}
}
private int TestParam(params string[]
arr)
{
return arr.GetLength(0);
}
Result:
0 Comments