3
Reply

What is mean by OUT Parameters,Ref Parameters in C#?

P Narasimha

P Narasimha

15y
4.7k
0
Reply


    Ok,

    consider this method

    public int  sum(int x, int y)
    {
        int z = x + y;
        return z;  
    }

    are the x or y changed during the method process, the response is  no for sure, we call those paramters input parameters

    Now, what if you want that x recives the sum of x+y so the x will be at the same time an input and also an output so it has to be marked as ref

    public int  sum(ref int x, int y)
    {
        int x = x + y;
        return x;  
    }

    Now, what if  the x value is known as input so we dont need to set it, then it will be considered only as an output value

    public int sum(out int x, int y)
    {
       x = 20;
       x = x + y;
       return x;
    }
     

    hi,

      Out And Ref are same but the difference is using out parameter we need to assigne the value the variable.

    using ref parameter without assgineing the value ref parameter will work

     

     

    Hi,

    What is mean by OUT Parameters,Ref Parameters in C#?

    Thanks,

    Narasima