Advertisement

Responsive Advertisement

V.2- Function Parameters

 V.2- Function Parameters

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

The out keyword causes arguments to be passed by reference. It makes the formal parameter an alias for the argument, which must be a variable. In other words, any operation on the parameter is made on the argument. It is like the ref keyword, except that ref requires that the variable be initialized before it is passed. It is also like the in keyword, except that in does not allow the called method to modify the argument value. To use an out parameter, both the method definition and the calling method must explicitly use the out keyword. For example:








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:


Source Code:

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:



Post a Comment

0 Comments