|
Introduction To C# Interfaces
|
Interfaces
As an interface defines a contract, a Class or Struct that implements an interface must
comply to its contract. An interface may inherit from multiple base-interfaces, and a
Class or Struct may implement multiple interfaces. An Interface can contain:
The interface itself does not provide implementations for the members that it
defines, but the interface merely specifies the members that must be supplied by
the classes or the structs that implemented the interface.
Interface declarations
An interface-declaration is a type-declaration which declares a new interface type.
An interface declaration may declare none or more members.
The members of an interface must be one or more of
An interface cannot contain:
Note 1: All interface members implicitly have public access.
Note 2: It is a compile-time error for interface member declarations to include any modifiers and the interfaces members cannot be declared with the modifiers public, protected, private, abstract, virtual, override, static, or internal.
Example:
public delegate void ISampleInterface ( IStringList sender);
public interface IStringList
{
void Concat (string s); // Declares A Method
int nTotal { get; } // Declares A Property
event StrEventTriggered; // Declares An Event
string this [int index] { get; set; } // Declares An Indexer
}
The name of a property or event must differ from the names of all other members declared in the same interface.
The signature of an indexer must differ from the signatures of all other indexers declared in the same interface.
The inherited members of an interface are specifically not part of the declaration space of the interface. Thus, an interface is allowed to declare a member with the same name or signature as an inherited member. When this occurs, the derived interface member is said to hide the base interface member. Hiding an inherited member is not considered an error, but it does cause the compiler to issue a warning. To suppress the warning, the declaration of the derived interface member must include a new modifier to indicate that the derived member is intended to hide the base member.
Fully Qualified Interface Member Names
An interface member is sometimes referred to by its fully qualified name.
The fully qualified name of an interface member consists of:
The fully qualified name of a member references the interface in which the member is declared.
For example, given the declarations
interface IControl
{
void Paint();
}
interface ITextBox: IControl
{
void SetText(string text);
}
The fully qualified name of Paint is IControl.Paint and the fully qualified name of SetText is ITextBox.SetText.
In the example above, it is not possible to refer to Paint as ITextBox.Paint.
When an interface is part of a namespace, the fully qualified name of an interface member includes the namespace name. For example
namespace System
{
public interface ICloneable
{
object Clone();
}
}
Here, the fully qualified name of the Clone method is System.ICloneable.Clone.
|
Home •
Site Map •
Contact Us •
Webmaster |