Advertisement
karlakmkj

Properties - getter & setter (automatic)

Nov 23rd, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.73 KB | None | 0 0
  1. //In Forest.cs file
  2. using System;
  3.  
  4. namespace BasicClasses
  5. {
  6.   class Forest
  7.   {
  8.     //No need the field for name and trees since using automatic property
  9.     public int age;
  10.     public string biome;
  11.    
  12.     public string Name
  13.     {
  14.       get; set;  //No need to define as a hidden field is defined in the background for us
  15.     }
  16.    
  17.     public int Trees
  18.     {
  19.       get; set;
  20.     }
  21.    
  22.     public string Biome
  23.     {
  24.       get { return biome; }
  25.       set
  26.       {
  27.         if (value == "Tropical" || value == "Temperate" || value == "Boreal")
  28.         {
  29.           biome = value;
  30.         }
  31.         else
  32.         {
  33.           biome = "Unknown";
  34.         }
  35.       }
  36.     }
  37.   }
  38. }
  39.  
  40. //Program.cs file remains the same
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement