Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Creating Property for fields - getter & setter method
- //In Forest.cs file
- using System;
- namespace BasicClasses
- {
- class Forest
- {
- public string name;
- public int trees;
- public int age;
- public string biome;
- //Property for name field
- public string Name {
- get {return name;}
- set {name = value;}
- }
- //Property for trees field
- public int Trees {
- get {return trees;}
- set {trees = value;}
- }
- //Property for biome field
- public string Biome {
- get {return biome;}
- set {
- if (value == "Tropical" || value == "Temperate" || value == "Boreal")
- {biome = value;}
- else {biome = "Unknown";}
- }
- }
- }
- }
- ============================================================================================================================
- //In Program.cs file
- using System;
- namespace BasicClasses
- {
- class Program
- {
- static void Main(string[] args)
- {
- Forest f = new Forest();
- f.Name = "Congo"; //change field to Property Name
- f.Trees = 3;
- f.age = 0;
- f.Biome = "Tropical";
- Console.WriteLine(f.Name); //invoke using Property Name
- Console.WriteLine(f.Trees);
- Console.WriteLine(f.Biome);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement