Mega Code Archive

 
Categories / C# Book / 01 Language Basics
 

0122 read only property

If a property has only get accessor we call it read-only property. The following code add read-only area property to the Rectangle. using System; class Rectangle { public int Width; public int Height; public int Area { get { return Width * Height; } } } class Program { static void Main(string[] args) { Rectangle r = new Rectangle(); r.Width = 5; r.Height = 6; Console.WriteLine(r.Area); } } The output: 30