Advertisement
silvi81

ExplicitInterfaces

Jul 25th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ExplicitInterfaces
  8. {
  9.     public interface IResident
  10.     {
  11.         string Name { get; set; }
  12.         string Country { get; set; }
  13.  
  14.         string GetName();
  15.     }
  16.  
  17.     public interface IPerson
  18.     {
  19.         string Name { get; set; }
  20.         int Age { get; set; }
  21.  
  22.         string GetName();
  23.     }
  24.  
  25.     public class Person : IPerson, IResident
  26.     {
  27.         private string name;
  28.  
  29.         public Person(string name, string country, int age)
  30.         {
  31.             this.name = name;
  32.             this.Country = country;
  33.             this.Age = age;
  34.         }
  35.  
  36.         string IPerson.Name { get; set; }
  37.  
  38.         public string Country { get; set; }
  39.  
  40.         public int Age { get; set; }
  41.  
  42.         string IResident.Name { get; set; }
  43.  
  44.         string IResident.GetName()
  45.         {
  46.             return $"Mr/Ms/Mrs {this.name}";
  47.         }
  48.  
  49.         string IPerson.GetName()
  50.         {
  51.             return this.name;
  52.         }
  53.     }
  54.  
  55.     class Program
  56.     {
  57.         static void Main(string[] args)
  58.         {
  59.             string input = Console.ReadLine();
  60.  
  61.             while (input != "End")
  62.             {
  63.                 string[] tokens = input.Split(
  64.                 new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  65.  
  66.                 var person = new Person(tokens[0], tokens[1], int.Parse(tokens[2]));
  67.  
  68.                 IPerson person1 = person;
  69.                 IResident person2 = person;
  70.  
  71.                 Console.WriteLine(person1.GetName());
  72.                 Console.WriteLine(person2.GetName());
  73.  
  74.                 input = Console.ReadLine();
  75.             }
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement