Advertisement
bero_0401

ref & out Keywords

Aug 17th, 2024 (edited)
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.98 KB | Source Code | 0 0
  1.  
  2. namespace CsharpCourse
  3. {
  4.  
  5.     internal class Program
  6.     {
  7.  
  8.         // ref
  9.         // must be initialized
  10.  
  11.  
  12.         // out
  13.         // may not be initialized
  14.         // must be assigned to a value in all paths in the method
  15.  
  16.  
  17.      
  18.  
  19.         static void Main(string[] args)
  20.         {
  21.  
  22.             int x = 2;
  23.             bool IsEven;
  24.             DoubleOut(x, out IsEven);
  25.  
  26.  
  27.             bool ref_IsEven = false;
  28.             DoubleRef(x, ref ref_IsEven);
  29.  
  30.         }
  31.  
  32.  
  33.         static void DoubleOut(int x, out bool IsEven)
  34.         {
  35.             if (x % 2 == 0)
  36.             {
  37.                 IsEven = true;
  38.                 return;
  39.             }
  40.             IsEven = false;
  41.             return;
  42.         }
  43.  
  44.  
  45.         static void DoubleRef(int x, ref bool IsEven)
  46.         {
  47.             if (x % 2 == 0)
  48.             {
  49.                 IsEven = true;
  50.                 return;
  51.             }
  52.            // IsEven = false;
  53.             return;
  54.         }
  55.     }
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement