Saturday, November 20, 2010

Understanding interface in c sharp.

An interface defines a contract. A class or struct that implements an interface must adhere to its contract. An interface looks like a class, but has no implementation. The only thing it contains are definitions of events, indexers, methods and/or properties. The reason interfaces only provide definitions is because they are inherited by classes and structs, which must provide an implementation for each interface member defined. Interfaces in C# are provided as a replacement of multiple inheritances. Because C# does not support multiple inheritance,

The example:
Using System;
Using System.Collections;
Public delegate void ChangeRegistrar(object sender, object event);
Public interface IBroker
{
String GetRating(String stock);
Decimal PricePerTrade
{
get;
set;
}
decimal this[String StockName]
{
get;
set;
}
event ChangeRegistrar PriceChange;
}

Note: - The interface name begins with the conventional I in IBroker. It has four members the GetRating() method, the PricePerTrade property, an indexer and the PriceChange event. The delegate type of the PriceChange property, ChangeRegistrar, is defined also. As mentioned earlier, interface members do not have implementations. It is up to a class or struct to implement interface member declarations.

No comments:

Recent Posts