Friday, October 23, 2020

Ref Vs Out in C#

 Out and Ref are C# keywords help to pass variables by reference.


In the ref no need to initialize variable in function definition. Ref is Two-way from caller to callee.

        static void SomeFunction(ref int value)

        {

            value = value + 10;

        }

int value = 20;

SomeFunction(ref value);

Console.WriteLine(value);

Here out is  30.

In the Out we need to initialize variable in function definition.

Data sent from the caller has been discarded and its mandatory to initialize the variable inside the callee.

"Out is One-Way from caller to callee"


 static void SomeFunction(out int value)

        {

            value = 0; // In out we need to initialize the variable value

            value = value + 10;

        }

   int value = 20;

  SomeFunction(out value);

  Console.WriteLine(value);

Here output is 10.

 

No comments:

Post a Comment

Featured Post

What is JavaScript? What is the role of JavaScript engine?

  The JavaScript is a Programming language that is used for converting static web pages to interactive and dynamic web pages. A JavaScript e...