CSharp examples for Custom Type:Property
Create Bicycle class With Properties
using System;/* w w w . j ava 2s .co m*/ class Bicycle { const byte MaxSpeed = 40; private byte speed = 0; private int speedAccessCounter = 0; public byte Speed { get { speedAccessCounter++; return speed; } set { if (value > MaxSpeed) Console.WriteLine("Error. {0} exceeds the speed limit {1}", value, MaxSpeed); else if (value < 0) Console.WriteLine("Error. {0} is less than 0", value); else speed = value; } } public int SpeedAccessCounter { get { return speedAccessCounter; } } } class BicycleTester { public static void Main() { byte speedInMilesPerHour; Bicycle myBike = new Bicycle (); myBike.Speed = 60; myBike.Speed = 30; Console.WriteLine("Current speed of myBike: {0}", myBike.Speed); speedInMilesPerHour = (byte)(myBike.Speed * 0.621); Console.WriteLine("Number of times speed of myBike has been retrieved: {0}", myBike.SpeedAccessCounter); } }