In this example, the class TimePeriod stores a time period. Internally the class stores the time in seconds, but a property called Hours is provided that allows a client to specify a time in hours. The accessors for the Hours property perform the conversion between hours and seconds.
class TimePeriod
{
private double seconds;
public double Hours
{
get { return seconds / 3600; }
set { seconds = value * 3600; }
}
}
class Program
{
static void Main()
{
TimePeriod t = new TimePeriod();
// Assigning the Hours property causes the 'set' accessor to be called.
t.Hours = 24;
// Evaluating the Hours property causes the 'get' accessor to be called.
System.Console.WriteLine("Time in hours: " + t.Hours);
}
}
Output
Time in hours: 24
Properties Overview
Resource: ASP.NET Controls ASP.NET 2.0 .NET Deployment SQL Server 2005 SQL SQL Server Basics ADO.NET Basics ASP.NET MVC ASP.NET Basics ASP.NET Architecture ASP.NET Security .NET Security .NET 2.0 .NET 3.5 .NET Architecture .NET Basics C# VB.NET SQL Server security