Monday, July 16, 2007

Properties

Properties
Properties can be marked as public, private, protected, internal, or protected internal. These access modifiers define how users of the class can access the property. The get and set accessors for the same property may have different access modifiers. For example, the get may be public to allow read-only access from outside the type, and the set may be private or protected.
A property may be declared as a static property using the static keyword. This makes the property available to callers at any time, even if no instance of the class exists.
A property may be marked as a virtual property using the virtual keyword. This allows derived classes to override the property behavior using the override keyword.
A property overriding a virtual property can also be sealed, specifying that for derived classes it is no longer virtual. Lastly, a property can be declared abstract, meaning there is no implementation in the class, and derived classes must write their own implementation.

*It is an error to use a virtual, abstract, or override modifier on an accessor of a static property

Abstract Properties
-------------------
A property inside a class can be declared as abstract by using the keyword abstract. Remember that an abstract property in a class carries no code at all. The get/set accessors are simply represented with a semicolon. In the derived class we must implement both set and get assessors.
If the abstract class contains only set accessor, we can implement only set in the derived class.
The following program shows an abstract property in action.

using System;
abstract class Abstract
{
public abstract int X
{
get;
set;
}
}
class Concrete : Abstract
{
public override int X
{
get
{
Console.Write(" GET");
return 10;
}
set
{
Console.Write(" SET");
}
}
}
class MyClient
{
public static void Main()
{
Concrete c1 = new Concrete();
c1.X = 10;
Console.WriteLine(c1.X);//Displays 'SET GET 10'
}
}

Static Properties
-----------------
C# also supports static properties, which belongs to the class rather than to the objects of the class. All the rules applicable to a static member are applicable to static properties also.
The following program shows a class with a static property.

using System;
class MyClass
{
private static int x;
public static int X
{
get {return x;}
set {x = value;}
}
}
class MyClient
{
public static void Main()
{
MyClass.X = 10;
int xVal = MyClass.X;
Console.WriteLine(xVal);//Displays 10
}
}
Remember that set/get accessor of static property can access only other static members of the class. Also static properties are invoking by using the class name.
Properties & Inheritance
------------------------
The properties of a Base class can be inherited to a Derived class.

using System;
class Base
{
public int X
{
get{Console.Write("Base GET");return 10;}
set{Console.Write("Base SET");}
}
}
class Derived : Base
{}
class MyClient
{
public static void Main()
{
Derived d1 = new Derived();
d1.X = 10;
Console.WriteLine(d1.X);//Displays 'Base SET Base GET 10'
}
}
The above program is very straightforward. The inheritance of properties is just like inheritance any other member.

Properties & Polymorphism
-------------------------
A Base class property can be polymorphicaly overridden in a Derived class. But remember that the modifiers like virtual, override etc are using at property level, not at accessor level.

using System;
class Base
{
public virtual int X
{
get{Console.Write("Base GET");return 10;}
set{Console.Write("Base SET");}
}
}
class Derived : Base
{
public override int X
{
get{Console.Write("Derived GET");return 10;}
set{Console.Write("Derived SET");}
}
}
class MyClient
{
public static void Main()
{
Base b1 = new Derived();
b1.X = 10;
Console.WriteLine(b1.X);//Displays 'Derived SET Derived GET 10'
}
}

No comments: