Advertisement
wingman007

OOP_C#_Person_Indexer_default_params

Apr 5th, 2018
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.75 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 WorldMI
  8. {
  9.   class Person
  10.   {
  11.     private string name;
  12.     private int age;
  13.     private decimal balance;
  14.     private string[] friends;
  15.  
  16.     public string this[int i]
  17.     {
  18.       get { return friends[i]; }
  19.       set { friends[i] = value; }
  20.     }
  21.  
  22.     public string Name
  23.     {
  24.       get { return name; }
  25.       set { name = value; }
  26.     }
  27.  
  28.     public int Age
  29.     {
  30.       get { return age; }
  31.       set {
  32.         if (value > 120 || value < 0) value = 0;
  33.         age = value;
  34.       }
  35.     }
  36.  
  37.     public Person()
  38.       :this("Default", 0)
  39.     {
  40.       //this.name = "Default";
  41.       //this.age = 0;
  42.     }
  43.  
  44.     // working horse
  45.     public Person(string name, int age, decimal balance = 0.0M, params string[] friends)
  46.     {
  47.       this.name = name;
  48.       this.age = age;
  49.       this.balance = balance;
  50.       this.friends = friends;
  51.     }
  52.  
  53.     public Person(int age, string name)
  54.       :this(name, age)
  55.     {
  56.       //this.name = name;
  57.       //this.age = age;
  58.     }
  59.  
  60.     //public string GetName()
  61.     //{
  62.     //  return name;
  63.     //}
  64.  
  65.     //public void SetName(string name)
  66.     //{
  67.     //  this.name = name;
  68.     //}
  69.  
  70.     //public int GetAge()
  71.     //{
  72.     //  return age;
  73.     //}
  74.  
  75.     //public void SetAge(int age)
  76.     //{
  77.     //  if(age > 120 || age < 0)
  78.     //  {
  79.     //    age = 0;
  80.     //  }
  81.     //  this.age = age;
  82.     //}
  83.  
  84.     public void IntroduceYourSelf()
  85.     {
  86.       Console.WriteLine("My name is {0}. I am {1} years old! Last year I was {2} years old!", name, age, GetLastYearAge(age));
  87.     }
  88.  
  89.     private int GetLastYearAge(int age)
  90.     {
  91.       return age - 1;
  92.     }
  93.  
  94.   }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement