Part 2.
Defining an Interface:
You define an interface similar to defining a class. Like classes, interfaces are reference types. The big difference is that there is no implementation code in an interface; it is pure specification. Also note that an interface can have properties as well as methods (it could also have other members, such as indexers).
For example this IAccount interface specifies operations to be performed on a bank account.
Code:
interface IAccount
{
void Deposit(decimal amount);
void Withdraw(decimal amount);
decimal Balance {get;}
void Show();
}
----------------------------
Implementing An Interface
You specify that a class or struct implements an interface by using the colon notation that is employed for class inheritance. A class can also inherit both from a class and from an interface. In this case the base class should appear first in the derivation list after the colon.
Code:
public class AccountC : Account, IAccount
{
public void Show()
{
Console.WriteLine("balance = {0}", Balance);
}
}
In that example the class AccountC inherits from the class Account, and it implements the interface IAccount. The methods of the interface must all be implemented by Account (which I was too lazy to do), either directly or in one of the base classes in its inheritance hierarchy.
More Solid Real World Examples To Come Later