elena1234

Ref vs Out in C#

Dec 10th, 2021 (edited)
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.65 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Ref
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             {
  10.                 int a = 4;
  11.                 int b = 3;
  12.                 int result; // unnecessary assigment of a value to result when we use out
  13.                 GetSum(a, b, out result);
  14.                 Console.WriteLine($"The sum from {a} and {b} is {result}.");
  15.             }
  16.         }
  17.  
  18.         private static int GetSum(int first, int second, out int result) // first and second are passed by value, but result is by reference
  19.         {
  20.             result = first + second;
  21.             return result;
  22.         }
  23.     }
  24. }
  25.  
Add Comment
Please, Sign In to add comment