Read only and write only property
In this chapter you will learn:
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;/*from ja va 2 s.com*/
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:
Combine readonly and read only property
using System;/*from j av a2 s . c o m*/
public sealed class Value
{
public int intValue = 10;
}
public sealed class MyController
{
public MyController( Value pimpl ) {
this.pimpl = pimpl;
}
public int IntValue {
get {
return pimpl.intValue;
}
}
private readonly Value pimpl;
}
public sealed class MainClass
{
static void Main() {
Value someNumber = new Value( );
}
}
Next chapter...
What you will learn in the next chapter:
Home » C# Tutorial » Class