Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (7.45 KB, 2 trang )
Indexers in Interfaces
You can declare indexers in an interface. To do this, specify the get and/or set keyword,
but replace the body of the get or set accessor with a semicolon. Any class or struct that
implements the interface must implement the indexer accessors declared in the interface.
For example:
interface IRawInt
{
bool this [ int index ] { get; set; }
}
struct RawInt : IRawInt
{
public bool this [ int index ]
{
get { }
set { }
}
}
If you implement the interface indexer in a class, you can declare the indexer
implementations as virtual. This allows further derived classes to override the get and set
accessors. For example:
class RawInt : IRawInt
{
public virtual bool this [ int index ]
{
get { }