Advertisement
fcamuso

Generics - 4: metodi generic

Mar 7th, 2021
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.97 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Generics2
  4. {
  5.   class C<T>
  6.   {
  7.     public static string Membro_statico { get; set; } = "";
  8.  
  9.     void F()
  10.     {
  11.       T[] elenco = new T[100];
  12.  
  13.       for (int i = 0; i < elenco.Length; i++) elenco[i] = default;
  14.     }
  15.   }
  16.  
  17.   class Cliente : IComparable
  18.   {
  19.     public string ragioneSociale { get; set; }
  20.    
  21.     public int CompareTo(object altro)
  22.     {
  23.       if (ragioneSociale.Length < ((Cliente)altro).ragioneSociale.Length)
  24.         return -1;
  25.       else
  26.         if (ragioneSociale.Length == ((Cliente)altro).ragioneSociale.Length)
  27.           return 0;
  28.         else
  29.           return 1;
  30.     }
  31.   }
  32.  
  33.   class Program
  34.   {
  35.     static T ValoreMassimo<T>(T[] v) where T: IComparable
  36.     {
  37.       T massimo = v[0];
  38.       for (int i = 1; i < v.Length; i++)
  39.         if (v[i].CompareTo(massimo)>0) massimo = v[i];
  40.  
  41.       return massimo;
  42.     }
  43.  
  44.     static void Scambia<T>(ref T a, ref T b)
  45.     {
  46.       T temp = a;
  47.       a = b;
  48.       b = temp;
  49.     }
  50.  
  51.     static void Main(string[] args)
  52.     {
  53.       C<int>.Membro_statico = "Ciao";
  54.       C<bool>.Membro_statico = "Mondo";
  55.  
  56.       int x = 100, y = 200;
  57.       Scambia(ref x, ref y);
  58.       Console.WriteLine($"{x} - {y}");
  59.  
  60.       string s1 = "ciao", s2 = "mondo";
  61.       Scambia(ref s1, ref s2);
  62.       Console.WriteLine($"{s1} - {s2}");
  63.  
  64.       Cliente c1 = new Cliente(); c1.ragioneSociale = "Ernesto";
  65.       Cliente c2 = new Cliente(); c2.ragioneSociale = "Sparalesto";
  66.       Scambia(ref c1, ref c2);
  67.       Console.WriteLine($"{c1.ragioneSociale} - {c2.ragioneSociale}");
  68.  
  69.       string[] v = new string[] { "a", "z", "m", "n" };
  70.       Console.WriteLine($"Valore massimo nell'array: {ValoreMassimo(v)}");
  71.  
  72.       Cliente[] vcliente = new Cliente[] { c1, c2 };
  73.       Console.WriteLine($"Valore massimo nell'array: {ValoreMassimo(vcliente).ragioneSociale}");
  74.  
  75.     }
  76.   }
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.   class Derivata1<Txxx, T2> : C<Txxx>
  86.   {
  87.  
  88.   }
  89.  
  90.   class Derivata2 : C<int>
  91.   {
  92.  
  93.   }
  94.  
  95.  
  96.  
  97. }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement