Properties and Encapsulation
Computed Properties
A computed property calculates its value from other object state.
Computed Properties
ComputedProperties.cs
using System;
class Rectangle
{
public int Width { get; set; }
public int Height { get; set; }
public int Area
{
get { return Width * Height; }
}
}
class Program
{
static void Main()
{
int height = ;
Rectangle rectangle = new Rectangle();
rectangle.Width = 5;
rectangle.Height = height;
Console.WriteLine($"height={height}");
Console.WriteLine($"area={rectangle.Area}");
}
}
using System;
class Rectangle
{
public int Width { get; set; }
public int Height { get; set; }
public int Area
{
get { return Width * Height; }
}
}
class Program
{
static void Main()
{
int height = ;
Rectangle rectangle = new Rectangle();
rectangle.Width = 5;
rectangle.Height = height;
Console.WriteLine($"height={height}");
Console.WriteLine($"area={rectangle.Area}");
}
}
using System;
class Rectangle
{
public int Width { get; set; }
public int Height { get; set; }
public int Area
{
get { return Width * Height; }
}
}
class Program
{
static void Main()
{
int height = ;
Rectangle rectangle = new Rectangle();
rectangle.Width = 5;
rectangle.Height = height;
Console.WriteLine($"height={height}");
Console.WriteLine($"area={rectangle.Area}");
}
}
computed property
A computed property returns a value instead of storing one directly.