Advertisement
fcamuso

C# 9, top level statements; pattern matching A

Jan 4th, 2022
1,528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.32 KB | None | 0 0
  1. using System;
  2.  
  3. namespace patternMatching
  4. {
  5.  
  6.   class Personaggio { }
  7.  
  8.   class Adepto : Personaggio { public int Protezione { get; set; } = 100; }
  9.   class Druido : Personaggio { public int Esperienza { get; set; } = 200; }
  10.   class Stregone : Personaggio  { public int Dominio { get; set; } = 300; }
  11.  
  12.  
  13.     static class Program
  14.     {
  15.  
  16.         //static public int MaxPotenziamentoApplicabile(this Personaggio p)
  17.         //{
  18.         //    if (p is null) throw new ArgumentNullException();
  19.         //    if (p is Adepto) return ((Adepto)p).Protezione + 10;
  20.         //    if (p is Druido) return ((Druido)p).Esperienza + 100;
  21.         //    if (p is Stregone) return ((Stregone)p).Dominio+ 10;
  22.  
  23.         //    throw new ArgumentException("Personaggio sconosciuto", nameof(Personaggio));
  24.         //}
  25.  
  26.  
  27.         //static public int MaxPotenziamentoApplicabile(this Personaggio p)
  28.         //{
  29.         //    if (p is null) throw new ArgumentNullException();
  30.         //    if (p is Adepto a) return a.Protezione + 10;
  31.         //    if (p is Druido d) return d.Esperienza + 100;
  32.         //    if (p is Stregone s) return s.Dominio + 10;
  33.  
  34.         //    throw new ArgumentException("Personaggio sconosciuto", nameof(Personaggio));
  35.         //}
  36.  
  37.         //static public int MaxPotenziamentoApplicabile(this Personaggio p)
  38.         //{
  39.         //    if (p is Adepto a)
  40.         //        return a.Protezione + 10;
  41.         //    else
  42.         //        if (p is Druido d)
  43.         //            return d.Esperienza + 100;
  44.         //        else
  45.         //            if (p is Stregone s)
  46.         //                return s.Dominio + 10;
  47.         //            else
  48.         //                if (p is null)
  49.         //                    throw new ArgumentNullException();
  50.         //                else
  51.         //                    throw new ArgumentException("Personaggio sconosciuto", nameof(Personaggio));
  52.         //}
  53.         //static public int MaxPotenziamentoApplicabile(this Personaggio p)
  54.         //{
  55.         //    switch (p)
  56.         //    {
  57.         //        case Adepto a:
  58.         //            return a.Protezione + 10;
  59.         //            break;
  60.  
  61.         //        case Druido d:
  62.         //            return d.Esperienza+ 100;
  63.         //            break;
  64.  
  65.         //        case Stregone s:
  66.         //            return s.Dominio + 10;
  67.         //            break;
  68.  
  69.         //        case null:
  70.         //            throw new ArgumentNullException();
  71.         //            break;
  72.  
  73.         //        case var _:
  74.         //            throw new ArgumentException("Personaggio sconosciuto", nameof(Personaggio));
  75.         //            break;
  76.         //    }
  77.  
  78.  
  79.         //static public int MaxPotenziamentoApplicabile(this Personaggio p) => p switch
  80.         //{
  81.         //    Adepto a => a.Protezione + 10,
  82.         //    Druido d => d.Esperienza + 100,
  83.         //    Stregone s => s.Dominio + 200,
  84.         //    null => throw new ArgumentNullException(nameof(Personaggio)),
  85.         //    _ => throw new ArgumentException("Personaggio sconosciuto", nameof(Personaggio))
  86.         //};
  87.  
  88.        
  89.         static void Main(string[] args)
  90.         {
  91.             Personaggio player = new Druido();
  92.             Console.WriteLine($"Massimo potenziamento : {player.MaxPotenziamentoApplicabile()}");            
  93.  
  94.         }
  95.     }
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement