Advertisement
fcamuso

C# 9, pattern matching B

Jan 9th, 2022
1,135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.44 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.         static public int MaxPotenziamentoApplicabile(this Personaggio p) => p switch
  89.         {
  90.             Adepto  => 150,
  91.             Druido  => 250,
  92.             Stregone => 400,
  93.             null => throw new ArgumentNullException(nameof(Personaggio)),
  94.             _ => throw new ArgumentException("Personaggio sconosciuto", nameof(Personaggio))
  95.         };
  96.  
  97.         static void Main(string[] args)
  98.         {
  99.             Personaggio player = new Druido();
  100.             Console.WriteLine($"Massimo potenziamento : {player.MaxPotenziamentoApplicabile()}");
  101.  
  102.             //if (player is Druido || player is Stregone)
  103.             //if (player is Druido or Stregone)
  104.  
  105.             //if (player is not Adepto and (Druido or Stregone) )
  106.  
  107.             if (player is not Stregone)
  108.             if (player is not null)
  109.  
  110.             switch (player)
  111.             {
  112.                 case Adepto or Stregone:
  113.                     break;
  114.  
  115.                 default: Console.WriteLine("beccato"); break;
  116.  
  117.             }
  118.  
  119.  
  120.             //int valorePotenziale = player.MaxPotenziamentoApplicabile() switch
  121.             //{
  122.  
  123.             //}
  124.             Console.WriteLine(player.MaxPotenziamentoApplicabile());
  125.  
  126.             switch (player.MaxPotenziamentoApplicabile())
  127.             {
  128.                 case <=180:
  129.                     break;
  130.  
  131.             }
  132.  
  133.         }
  134.     }
  135. }
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement