Advertisement
stefanpu

Modifying refe and value types

Jan 12th, 2013
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.18 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ModifyingOfRefAndValueTypes
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.            
  10.             int[] array = { 1, 2, 3, 4, 5 };
  11.  
  12.             int value = 6;
  13.  
  14.             //Array and value before modification
  15.             Console.Write("array before modification: ");
  16.             PrintArray(array);
  17.  
  18.             Console.Write("value before modification: ");
  19.             Console.WriteLine(value);
  20.  
  21.             ModifyArray(array);
  22.             ModifyValue(value);
  23.  
  24.             Console.Write("array after modification: ");
  25.             PrintArray(array);
  26.  
  27.             Console.Write("value after modification: ");
  28.             Console.WriteLine(value);
  29.  
  30.  
  31.         }
  32.  
  33.         private static void ModifyValue(int value)
  34.         {
  35.             value += 45;
  36.         }
  37.  
  38.         private static void ModifyArray(int[] array)
  39.         {
  40.             array[0] = +45;
  41.         }
  42.  
  43.         private static void PrintArray(int[] array)
  44.         {
  45.             foreach (var item in array)
  46.             {
  47.                 Console.Write(item + " ");
  48.                
  49.             }
  50.             Console.WriteLine();
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement