Interfaces in C#
Interface names start with I. Interface can only contain declarations of methods, properties,indexers, and events.You can never instantiate an interface; it contains only the signatures of its members. An interface hasneither constructors (how can you construct something that you can’t instantiate?) nor fields (because thatwould imply some internal implementation). An interface definition is also not allowed to contain operatoroverloads it is becauseinterfaces are usually intended to be public contracts, and having operator overloads would cause someincompatibility problems with other.NET languages, such as Visual Basic.NET, which do not supportoperator overloading.It is also not permitted to declare modifiers on the members in an interface definition. Interface members arealways implicitly public, and cannot be declared as virtual or static Example:
public interface IDisposable
{void Dispose();}
class SomeClass: IDisposable
public void Dispose()
{// implementation of Dispose() method}
// rest of class}
Derived interfaces
It’s possible for interfaces to inherit from each other in the same way that classes do. For example, let define new interface, ITransferBankAccount, which has the same features as IBankAccount but also defines a method to transfer money directly to a different account:
namespace Wrox.ProCSharp
{
public interface ITransferBankAccount: IBankAccount
{bool TransferTo(IBankAccount destination, decimal amount);
}}
Because ITransferBankAccount is derived from IBankAccount, it gets all the members of IBankAccount as well as its own. That means that any class that derives from ITransferBankAccount must implement all the methods of IBankAccount, as well as the new TransferTo() method defined in ITransferBankAccount.
Дата добавления: 2015-09-18 | Просмотры: 539 | Нарушение авторских прав
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
|