Advertisement
fcamuso

Errata Corrige al video n. 53

Aug 22nd, 2021
1,477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.56 KB | None | 0 0
  1. using System;
  2.  
  3. namespace test16_8
  4. {
  5.   class ClassePunto
  6.   {
  7.     public int X;
  8.     public int Y;
  9.     public ClassePunto(int x, int y) { X = x; Y = y; }
  10.   }
  11.  
  12.   struct StructPunto
  13.   {
  14.     public int X;
  15.     public int Y;
  16.     public StructPunto(int x, int y) { X = x; Y = y; }
  17.   }
  18.  
  19.  
  20.   class Program
  21.   {
  22.     static void ClasseSenzaRef(ClassePunto p)
  23.     {
  24.       //p è una copia dell'INDIRIZZO DEL PUNTO ESTERNO
  25.  
  26.       //p = new(1, 1); //il punto nel main non cambia
  27.       p.X = 1; //ma i valori dei membri interni sì
  28.     }
  29.  
  30.     static void StructSenzaRef(StructPunto p)
  31.     {
  32.       //p è una copia dei DATI esterni
  33.       //p = new(2, 2); //il punto nel main non cambia
  34.       p.X = 2;
  35.     }
  36.  
  37.     static void ClasseConRef(ref ClassePunto p)
  38.     {
  39.       //p è dove memorizzato l'indirizzo del punto esterno, non una copia
  40.       //p = new(5, 5); //il punto nel main CAMBIA
  41.       p.X = 99;
  42.     }
  43.  
  44.     static void StructConRef(ref StructPunto p)
  45.     {
  46.       //p = new(6, 6); //il punto nel main CAMBIA
  47.       p.X = 99;
  48.     }
  49.  
  50.  
  51.     static void Main(string[] args)
  52.     {
  53.       ClassePunto puntoComeClasse = new(77, 77);
  54.       StructPunto puntoComeStruct = new(88,88);
  55.  
  56.       //ClasseSenzaRef(puntoComeClasse);
  57.       //Console.WriteLine(puntoComeClasse.X);
  58.  
  59.       //StructSenzaRef(puntoComeStruct);
  60.       //Console.WriteLine(puntoComeStruct.X);
  61.  
  62.  
  63.       ClasseConRef(ref puntoComeClasse);
  64.       Console.WriteLine(puntoComeClasse.X);
  65.  
  66.       StructConRef(ref puntoComeStruct);
  67.       Console.WriteLine(puntoComeStruct.X);
  68.  
  69.  
  70.  
  71.     }
  72.   }
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement