The abstract modifier can be used for classes, methods, properties, events, and index indicators (indexer) to represent abstract members
Abstract cannot be used with static or virtual
Declaration as an abstract member can not include implementation code, but as long as there are not implemented abstract members (that is, abstract classes) in the class, its objects cannot be instantiated, and are often used to enforce that an inheriting class must implement a member
Example:
using System;
using System.Collections.Generic;
using System.Text;
namespace Example04
{
#region base class, abstract class
public abstract class BaseClass
{
The
//abstract property, which has both a get and set accessor, indicates that the inheriting class must implement the property as read-write
public abstract String Attribute
{
get;
set;
}
//abstract method, passing in a string parameter with no return value
public abstract void Function (String value);
//abstract event, type is a system-predefined agent (delegate): EventHandler
public abstract event EventHandler event;
//Abstract index indicator, only has a get accessor to indicate that the inheriting class must implement the index indicator as read-only
Public abstract Char This[int Index]
{
get;
}
}
#endregion
#region Inheriting class
public class Deriveclass:baseclass
{
private String attribute;
public override String Attribute
{
Get
{
return attribute;
}
Set
{
attribute = value;
}
}
public override void Function (String value)
{
attribute = value;
if (Event!= null)
{
Event (This, new EventArgs ());
}
}
public override event EventHandler event;
public override Char This[int Index]
{
Get
{
return Attribute[index];
}
}
}
#endregion
Class Program
{
static void Onfunction (object sender, EventArgs e)
{
for (int i = 0; I < ((deriveclass) sender). Attribute.length; i++)
{
Console.WriteLine ((deriveclass) sender) [i]);
}
}
static void Main (string[] args)
{
Deriveclass tmpobj = new Deriveclass ();
Tmpobj.attribute = "1234567";
Console.WriteLine (Tmpobj.attribute);
//associating the static function Onfunction with event events for Tmpobj objects
Tmpobj.event + = new EventHandler (onfunction);
tmpobj.function ("7654321");
Console.ReadLine ();
}
}
}
Results:
1234567
7
6
5
4
3
2
1