Sunday, October 25, 2015

out Vs ref

Both are used to pass variable(s) as a reference from one function to another function.
out: It does not carry existing value of variable and ask to initialize it in function 2, but returns the new out put value. so its one way communication.

ref: It carries existing value of variable in function 1, and after operations on existing value returns the out put value, so its two way communication.

    static void Method(out int i, ref int j)
    {
        i = 50; //initialization required for out but not for ref

  i = i + 10;
  j = j + 10;
 
    }
    static void Main()
    {
        int value1 = 20, value2 = 20;
        Method(out value1, ref value2);
        // value1 is now 60
        // value2 is now 30

    }

No comments:

Post a Comment

Node | Cluster Vs Worker Threads

Cluster: Multiple processes (scale app across CPU cores) Worker Threads: Multiple threads (handle CPU-heavy work inside one process) Cluster...