Properties:
Usually inside a class we provide function GET and SET, similarly C# provides a built in mechanism called properties.
Example:
class MyClass
{
private int x;
public int X
{
get
{
return x;
}
set
{
x = value;
}
}
}
After .net 3.0
class MyClass
{
public int X{get;set;} // Auto generates private feild with the name of property.
}
Note:
Static Properties:
C# also supports static properties.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.
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.
Example:
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");
}
}
}
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.
Example:
class MyClient
{
public static void Main()
{
Base b1 = new Derived();
b1.X = 10;
Console.WriteLine(b1.X);//Displays 'Derived SET Derived GET 10'
}
}
Usually inside a class we provide function GET and SET, similarly C# provides a built in mechanism called properties.
Example:
class MyClass
{
private int x;
public int X
{
get
{
return x;
}
set
{
x = value;
}
}
}
After .net 3.0
class MyClass
{
public int X{get;set;} // Auto generates private feild with the name of property.
}
Note:
Static Properties:
C# also supports static properties.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.
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.
Example:
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");
}
}
}
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.
Example:
class MyClient
{
public static void Main()
{
Base b1 = new Derived();
b1.X = 10;
Console.WriteLine(b1.X);//Displays 'Derived SET Derived GET 10'
}
}
Comments
Post a Comment