Last Updated:

Generic interfaces in .NET.

When using interfaces, uncertainty may arise if two interfaces implemented by a class have methods with the same names and signatures. Let's look, for example, at the following versions of the lAccount and IStatement interfaces. Note that each of them has a Show method.

_gc _interface lAccount
// garbage collector - IAccount
{
void Deposit(Decimal amount); // Deposit (Decimal
// amount)
void Withdraw(Decimal amount); // Remove (Decimal)
_property Decimal get_Balance(); // Decimal number
void Show(); // Show
};
_gc _interface IStatement // garbage collector - IStatement
{
_property int get_Transactions();
void Show(); // Show
};

How, in this case, to tell the class the desired implementation of the method? Such a task is solved by using the name of the interface together with the name of the method being implemented, as demonstrated by the example of the Program Ambiguous (Ambiguous Program).  The version of the Show method related to the lAccount interface displays only the status of the account, and the IStatement::Show method displays the number of trades and the balance.

//Account.h
_gc class Account: public lAccount, public IStatement
// garbage collector class Invoice
{
private: // private
Decimal decBalance; // Decimal number
int nNumXact; public:
Account(Decimal decBalance): nNumXact(O)
// Score (Decimal number decBalance)
{
this › decBalance = decBalance;
}
void Deposit(Decimal decAmount)
// Deposit (decimal decAmount)
{
decBalance = decBalance + decAmount;
++nNumXact; } void Withdraw(Decimal decAmount) // Withdraw(Decimal
// decAmount number)
{
decBalance = decBalance - decAmount;
++nNumXact;
}
_property Decimal get_Balance() // Decimal number
{
return decBalance;
}
void lAccount::Show() // Show
{
Console::WriteLine(
"balance = {0}", _box(decBalance)); // balance
}
_property int get_Transactions()
{
return nNumXact;
}
void IStatement::Show() // Show
{
Console::WriteLine(
"{0} transactions, balance = {!}", // transactions, balance
_box(nNumXact),
_box(decBalance));
}
};

The lAccount::Show and IStatement::Show methods cannot be accessed using a pointer to a class instance. These methods can only be accessed by using a pointer to the interface of the type explicitly specified in the method declarations. This program demonstrates that the lAccountShow method can only be called when using a pointer to the lAccount interface, not by using a pointer to an instance of the Account class. Attempting to call such a method using a pointer to an instance of the class is an error and will be stopped by the compiler. By getting a pointer to the IStatement interface, you can call the IStatement::Show method. The result of the program will be as follows:

0 transactions, balance = 100
0 transactions, balance = 100
balance = 115
2 transactions, balance = 115

Here's the translation:

0 trades, balance = 100
0 trades, balance = 100
balance = 115
2 trades, balance = 115

Sometimes, and in the absence of uncertainty, it is desirable to use explicit interface implementations to force the client program to call interface methods by using a pointer to the interface. This approach provides assurance that the source code of the client program explicitly indicates that the method belongs to a particular interface, and not to a large set of methods of the class. Such code is easy to adapt to implement other classes that use the same interface.

Generic Interfaces in .NET

Most of the features provided by the .NET Framework are implemented in generic interfaces that are implemented in different variants by the classes of the environment itself or can be implemented by the generated classes in order to modify or supplement the standard features defined by the environment.

In this section, we will look at several categories of operations performed by standard, generic interfaces. This is:

  • work with collections;
  • copying objects;
  • Comparison of objects.

Our review can by no means be considered exhaustive, but it will give you enough information to understand how generic interfaces work in the .NET Framework.