Advertisement
fcamuso

C# 9, covariant return types

Feb 11th, 2022
1,408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace CovariantReturnType
  6. {
  7.     class SkillsBase {
  8.         public int Salute { get; set; } = 10;
  9.     }
  10.  
  11.     class Personaggio {
  12.  
  13.         SkillsBase sb = new SkillsBase();
  14.  
  15.         public virtual SkillsBase GetSkills()
  16.         {
  17.             return sb;
  18.         }
  19.     }
  20.  
  21.     class SkillsRPG : SkillsBase{
  22.         public int SaluteExtra { get; set; } = 0;  
  23.     }
  24.  
  25.     class PersonaggioRPG : Personaggio {
  26.        
  27.         public SkillsRPG Skills  = new SkillsRPG();
  28.  
  29.         public override SkillsRPG GetSkills()
  30.         {
  31.             return Skills;
  32.         }
  33.     }
  34.  
  35.     internal class Program
  36.     {
  37.         static void Main(string[] args)
  38.         {
  39.  
  40.             Personaggio p = new Personaggio();
  41.             SkillsBase skills = p.GetSkills();
  42.  
  43.             PersonaggioRPG pRPG = new PersonaggioRPG();
  44.             SkillsRPG skillsRPG = pRPG.GetSkills();
  45.  
  46.            
  47.  
  48.         }
  49.     }
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement