Home > DeveloperSection > Articles > Passing Parameters in C Sharp

Passing Parameters in C Sharp


C# C# 
Ratings:
0 Comment(s)
 3551  View(s)
Rate this:

Passing Parameters in C#.Net


We need parameters when we want to pass values to a method.

There are four ways of passing values to a method.

·        Value

·         Ref (Reference)

·         Out (Reference)

·         Params (Parameter Array)

Passing parameters by Value

When we want to pass value to a method and we do not want any change in passed value. By passing parameter by value a duplicate copy of variables is created hence change in the value of passed parameter in method doesn’t affect the original passed value.

Example

public class value

    {

        public int sum(int a, int b)

        {

//Changing values of passed parameter

            a = a + 10;

            b = b + 20;

            return (a + b);

        }

    }

private void btnValue_Click(object sender, EventArgs e)

        {

            int a, b;

            a = 5;

            b = 10;

            value v = new value();

//displaying the returned value from method sum and value of passed parameter after function call.

            MessageBox.Show("Sum: "+v.sum(a, b).ToString()+"\nValue of a: " + a + "\nValue of b: " + b ,"Value");

        }

 

Passing parameter by Ref (Reference)

When we want change in passed parameter to the method then we pass it by reference with the help of ref keyword in actual and formal parameters. By passing parameter by reference actual address of the parameter is passed to the method and changes made to the parameter in method reflect in actual parameter passed.

Ref is bi-direction, i.e. it needs passed arguments to be initialized.

Example

public class refrenceRef

    {

        public int sum(ref int a, ref int b)

        {

//making changes in passes parameters.

            a = a + 10;

            b = b + 20;

            return (a + b);

        }

    }

private void btnRef_Click(object sender, EventArgs e)

        {

            int a, b;

            a = 5;

            b = 10;

            refrenceRef r = new refrenceRef();

//displaying the returned value from method sum and value of passed parameter after function call.

            MessageBox.Show("Sum: " + r.sum(ref a, ref b).ToString()+"\nValue of a: " + a + "\nValue of b: " + b , "Refrence(Ref)");

        }

Passing parameter by Out (Reference)

Passing parameter by out is somewhat like ref but the difference is that it is unidirectional i.e. we do not need to initialize the passed variable but we get back the processed value. Likewise Reference parameter we need to use out before variable in output parameter.

Example

public class refrenceOut

    {

        public int sum(int a, int b, out int c, out int d)

        {

//processing values of c & d

            c = a + b;

            d = c + 100;

            return (a + b);

        }

    }

private void btnOut_Click(object sender, EventArgs e)

        {

            int a, b, c, d;

            refrenceOut ro = new refrenceOut();

            a = 5;

            b = 10;

//passing uninitialized variable c & d

            MessageBox.Show("\nSum: "+ro.sum(a,b,out c,out d)+"\nValue of a: "+a+"\nValue of b: "+b+"\nValue of c: "+c+"\nValue of d: "+d);

        }

Passing parameter by params (Parameter Array)

When number of parameter to be passed to a method is not known i.e. we have variable parameter then in that case we use parameter array. To use parameter array we use params before variable name of array type in formal parameter.

Example

public class ParamsArray

    {

        public void sum(params int[] a)

        {

            int y = 0;

//adding values of all passed parameters.

            foreach (int x in a)

                y += x;

            MessageBox.Show("Sum is : " + y);

        }

    }

private void btnArray_Click(object sender, EventArgs e)

        {

            int[] a = { 1, 2, 3, 4, 5 };

            int x=5, y=10, z=15;

            ParamsArray p = new ParamsArray();

//passing array to method sum

            p.sum(a);

//passing 3 parameters to method sum

            p.sum(x, y, z);

        }

Screen shot for array passed as parameter

Screen shot for 3 variables passed as parameter


Don't want to miss updates? Please click the below button!

Follow MindStick