Advertisement
iliankostov

[Homework01] Problem-01-Persons

Jan 18th, 2015
492
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.31 KB | None | 0 0
  1. using System;
  2.  
  3. class Person
  4. {
  5.     // Create fields
  6.     private string name;
  7.     private int age;
  8.     private string email;
  9.  
  10.     // Create first constructor
  11.     public Person(string name, int age, string email)
  12.     {
  13.         this.Name = name;
  14.         this.Age = age;
  15.         this.Email = email;
  16.     }
  17.  
  18.     // Create second constructor
  19.     public Person(string name, int age)
  20.             : this(name, age, null)
  21.     {
  22.     }
  23.  
  24.     // Create and validate property Name
  25.     public string Name
  26.     {
  27.         get { return this.name; }
  28.         set
  29.         {
  30.             if (string.IsNullOrEmpty(value))
  31.             {
  32.                 throw new ArgumentNullException("The name can't be empty");
  33.             }
  34.             this.name = value;
  35.         }
  36.     }
  37.  
  38.     // Create and validate property Age
  39.     public int Age
  40.     {
  41.         get { return this.age; }
  42.         set
  43.         {
  44.             if (value < 1 || value > 100)
  45.             {
  46.                 throw new IndexOutOfRangeException("The age can be in range [1...100]");
  47.             }
  48.             this.age = value;
  49.         }
  50.     }
  51.  
  52.     // Create and validate property Email
  53.     public string Email
  54.     {
  55.         get { return this.email; }
  56.         set
  57.         {
  58.             if (value == null || value.Contains("@"))
  59.             {
  60.                 this.email = value;
  61.             }
  62.             else
  63.             {
  64.                 throw new Exception("The email is not valid");
  65.             }
  66.         }
  67.     }
  68.  
  69.     // Implementing the ToString() method to enable printing persons at the console
  70.     public override string ToString()
  71.     {
  72.         if (Email != null)
  73.         {
  74.             return string.Format("Name: {0}\nAge: {1}\nEmail: {2}\n", Name, Age, Email);
  75.         }
  76.         else
  77.         {
  78.             return string.Format("Name: {0}\nAge: {1}\n", Name, Age);
  79.         }
  80.  
  81.     }
  82.  
  83.     static void Main()
  84.     {
  85.         Person george = new Person("George", 25, "[email protected]");
  86.         Person henry = new Person("Henry", 30, "@");
  87.         Person michael = new Person("Michael", 35);
  88.  
  89.         Person[] persons = new Person[] { george, henry, michael };
  90.         foreach (Person person in persons)
  91.         {
  92.             Console.WriteLine(person);
  93.         }
  94.     }
  95. }
  96.  
  97. // Why VS2015 wants to remove "this" from constructors and properties ?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement