Advertisement
NPSF3000

Ref vs Val

Feb 15th, 2012
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. /*
  7. Value1: 1
  8. Pass By Value: 5
  9. Value1: 1
  10.  
  11. -------
  12.  
  13. Value2: 1
  14. Pass By Ref: 5
  15. Value2: 5
  16.  
  17. -------
  18.  
  19. Ref1: 1
  20. Pass By Value: 5
  21. Ref1: 5
  22.  
  23. -------
  24.  
  25. Ref2: 1
  26. Pass By Ref: 10
  27. Ref2: 10
  28. */
  29.  
  30.  
  31.  
  32. namespace RefVsVal
  33. {
  34.     class Program
  35.     {
  36.         static void Main(string[] args)
  37.         {
  38.  
  39.             int v1 = 1;  //Value Type
  40.  
  41.             Console.WriteLine("Value1: {0}", v1);
  42.  
  43.             //Passing value by value
  44.             ValByVal(v1);
  45.  
  46.             //Even though we've passed the value to a function and changed it, the value is unchanged.
  47.             //This is because we created a copy!
  48.             Console.WriteLine("Value1: {0}", v1);
  49.  
  50.             Console.WriteLine(); Console.WriteLine("-------"); Console.WriteLine();
  51.  
  52.             int v2 = 1;  //Value Type
  53.  
  54.             Console.WriteLine("Value2: {0}", v2);
  55.  
  56.             //Passing value by referance
  57.             ValByRef(ref v2);
  58.  
  59.             //This time we pass by value, so the change occurs here too!
  60.             Console.WriteLine("Value2: {0}", v2);
  61.  
  62.             Console.WriteLine(); Console.WriteLine("-------"); Console.WriteLine();
  63.  
  64.             int[] r1 = new[] { 1 };  //Referance Type
  65.  
  66.             Console.WriteLine("Ref1: {0}", r1[0]);
  67.  
  68.             //passing referance by value
  69.             RefByVal(r1);
  70.  
  71.             //We changed a value inside the array,
  72.             //but not change the referance [to a new array]
  73.             Console.WriteLine("Ref1: {0}", r1[0]);
  74.  
  75.             Console.WriteLine(); Console.WriteLine("-------"); Console.WriteLine();
  76.  
  77.             int[] r2 = new[] { 1 };  //Referance Type
  78.  
  79.             Console.WriteLine("Ref2: {0}", r2[0]);
  80.  
  81.             //passing referance by referance
  82.             RefByRef(ref r2);
  83.  
  84.             //We changed the referance - in this case by pointing it to a new array!
  85.             Console.WriteLine("Ref2: {0}", r2[0]);
  86.  
  87.             Console.ReadLine();
  88.  
  89.         }
  90.  
  91.         static void ValByVal(int i)
  92.         {
  93.             i = 5;  
  94.             Console.WriteLine("Pass By Value: {0}", i);
  95.         }
  96.  
  97.         static void ValByRef(ref int i)
  98.         {
  99.             i = 5;
  100.             Console.WriteLine("Pass By Ref: {0}", i);
  101.         }
  102.  
  103.         static void RefByVal(int[] a)
  104.         {
  105.             a[0] = 5;
  106.             Console.WriteLine("Pass By Value: {0}", a[0]);
  107.             a = new[] { 10 };
  108.         }
  109.  
  110.         static void RefByRef(ref int[] a)
  111.         {
  112.             a = new[] { 10 };
  113.             Console.WriteLine("Pass By Ref: {0}", a[0]);
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement