Ref and Out Parameters

suggest change

The ref keyword is used to pass an Argument as Reference. out will do the same as ref but it does not require an assigned value by the caller prior to calling the function.

Ref Parameter :-If you want to pass a variable as ref parameter then you need to initialize it before you pass it as ref parameter to method.

Out Parameter :- If you want to pass a variable as out parameter you don’t need to initialize it before you pass it as out parameter to method.

static void Main(string[] args)
{
    int a = 2;
    int b = 3;
    int add = 0;
    int mult= 0;
    AddOrMult(a, b, ref add, ref mult); //AddOrMult(a, b, out add, out mult);
    Console.WriteLine(add); //5
    Console.WriteLine(mult); //6
}

private static void AddOrMult(int a, int b, ref int add, ref int mult) //AddOrMult(int a, int b, out int add, out int mult)
{
    add = a + b;
    mult = a * b;
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Function with multiple return values:
* Ref and Out Parameters

Table Of Contents
17 Regex
19 Arrays
21 Enum
22 Tuples
24 GUID
27 Looping
36 Casting
46 Methods
88 Events
92 Structs
104 Indexer
106 Stream
107 Timers
109 Threading
121 Function with multiple return values
127 Caching
135 Pointers
147 C# Script