Guest User

p

a guest
Oct 30th, 2017
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.89 KB | None | 0 0
  1. using System;
  2.  
  3. class Person
  4. {
  5.     private string name;
  6.     private int age;
  7.  
  8.     public Person(string name, int age)
  9.     {
  10.         Name = name;
  11.         Age = age;
  12.     }
  13.  
  14.     public string Name
  15.     {
  16.         get
  17.         {
  18.             return name;
  19.         }
  20.         protected set
  21.         {
  22.             if (value.Length < 3)
  23.             {
  24.                 throw new ArgumentException("Name's length should not be less than 3 symbols!");
  25.             }
  26.  
  27.             name = value;
  28.         }
  29.     }
  30.  
  31.     public virtual int Age
  32.     {
  33.         get
  34.         {
  35.             return age;
  36.         }
  37.         set
  38.         {
  39.             if (age < 0)
  40.             {
  41.                 throw new ArgumentException("Age must be positive!");
  42.             }
  43.  
  44.             age = value;
  45.         }
  46.     }
  47.  
  48.     public override string ToString()
  49.     {
  50.         return $"Name: {Name}, Age: {Age}";
  51.     }
  52. }
Add Comment
Please, Sign In to add comment