Methods arguements in C#

Now as far as basics we are doing in C ,CPP ,JAVA we are passing arguments by value or by refrence or in arguments some times we pass the object as arguements included in cpp and Java.

But slightly the change is done in C#.net by this change we can overcome some problems that we will see at the end of the post.

Considering the methods arguements in C# it is divided into three parts

  1. By value 
  2. By refrence 
  3. By out parameter 
Above this we also have Params for accepting multiple arguements given by the user dynamically that we will see in the later post .

1) By value 
Here we know the clear meaning of passing by value , Those who have performed the progrmming of swapping of two numbers might have faced this situation that by swapping the two numbers at the last returning the values and seeing the output the values arent changed the reason is pass by value while calling the function the values are passed whatever changes that are done in the function that are done in the copy of values but not on the actual values. so as a result the actual values remains unchanged and there is no change in the values we print when the function returns the values.

2)By Refrence
Here also we know the refrence is passed along with the values that is if we initialze two variables and b and we were passing those variables by there refrences in some functions as &a and &b that is we are passing the refrence so the changes are done where that actual variable are residing so the changes are done on the actual values not the copy of the values so by returning the ans of the swaped values,the actual values  are changed.

I took this example  of swapping to explain by value and by refrence as it is quite popular and everybody has gone through this example atleast once.

Moving on to the third type of our category that is by out prameter

3)By Out Parameter
Here no values are taken to the called function by calling function but in return by the called function the calling function receives the values we can use this in the Function continuity and when the functions are dependent on each other that is for one function the input values are calculated by the above function and every time there is such a constraint that below function has to use the same values calculated by the above functions so while retuening from function the calling function comes by taking the values.

This was just the brushing i provided to set up an idea what are these three parameter passing methods .

TAKING THE EXAMPLES OF ALL THREE 

1) PASS BY VALUE
class PassingValByVal
{
    static void SquareIt(int x)
    // The parameter x is passed by value.
    // Changes to x will not affect the original value of x.
    {
        x *= x;
        System.Console.WriteLine("The value inside the method: {0}", x);
    }
    static void Main()
    {
        int n = 5;
        System.Console.WriteLine("The value before calling the method: {0}", n);

        SquareIt(n);  // Passing the variable by value.
        System.Console.WriteLine("The value after calling the method: {0}", n);

        // Keep the console window open in debug mode.
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }
}
/* Output:
    The value before calling the method: 5
    The value inside the method: 25
    The value after calling the method: 5
*/

2)EXAMPLE OF PASS BY REFRENCE 
class PassingValByRef
{
    static void SquareIt(ref int x)
    // The parameter x is passed by reference.
    // Changes to x will affect the original value of x.
    {
        x *= x;
        System.Console.WriteLine("The value inside the method: {0}", x);
    }
    static void Main()
    {
        int n = 5;
        System.Console.WriteLine("The value before calling the method: {0}", n);

        SquareIt(ref n);  // Passing the variable by reference.
        System.Console.WriteLine("The value after calling the method: {0}", n);

        // Keep the console window open in debug mode.
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }
}
/* Output:
    The value before calling the method: 5
    The value inside the method: 25
    The value after calling the method: 25
*/

3)EXAMPLE OF OUT PARAMETER



class OutExample
{
    static void Method(out int i)
    {
        i = 44;
    }
    static void Main()
    {
        int value;
        Method(out value);
        // value is now 44
    }
}

Here the changes are in the second  and third type that is pass by refrence and out parameter in pass by refrence ref is the keyword must be used and out keyword in the outParameter.


Comments

  1. Very much helpuful to learn and to get detailed knowledge about parameters of method

    ReplyDelete

Post a Comment

Popular posts from this blog

State space search / blocks world problem by heuristic approach /TIC TAC TOE

Navigation in Vaadin.

Drag and drop items from one Grid to another