Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Nbsp;   Defining an Interface

As mentioned in the previous section, an interface is a named set of method signatures. Note that interfaces can also define events, parameterless properties, and parameterful properties (indexers in C#) because all of these are just syntax shorthands that map to methods anyway, as shown in previ- ous chapters. However, an interface cannot define any constructor methods. In addition, an interface is not allowed to define any instance fields.


Although the CLR does allow an interface to define static methods, static fields, constants, and static constructors, a Common Language Specification (CLS)–compliant interface must not have any of these static members because some programming languages aren’t able to define or access them. In fact, C# prevents an interface from defining any of these static members.

In C#, you use the interface keyword to define an interface, giving it a name and its set of in- stance method signatures. Here are the definitions of a few interfaces defined in the Framework Class Library (FCL).

 

public interface IDisposable { void Dispose();

}

 

public interface IEnumerable { IEnumerator GetEnumerator();

}

 

public interface IEnumerable<out T> : IEnumerable { new IEnumerator<T> GetEnumerator();

}

 

public interface ICollection<T> : IEnumerable<T>, IEnumerable { void Add(T item);

void Clear();

Boolean Contains(T item);

void CopyTo(T[] array, Int32 arrayIndex); Boolean Remove(T item);

Int32 Count { get; } // Read­only property Boolean IsReadOnly { get; } // Read­only property

}

 

To the CLR, an interface definition is just like a type definition. That is, the CLR will define an inter- nal data structure for the interface type object, and reflection can be used to query features of the interface type. Like types, an interface can be defined at file scope or defined nested within another type. When defining the interface type, you can specify whatever visibility/accessibility (public, protected, internal, etc.) you want.

By convention, interface type names are prefixed with an uppercase I, making it easy to spot an interface type in source code. The CLR does support generic interfaces (as you can see from some of the previous examples) as well as generic methods in an interface. I will discuss some of the many fea- tures offered by generic interfaces later in this chapter and in Chapter 12, “Generics,” in which I cover generics more broadly.

An interface definition can “inherit” other interfaces. However, I use the word inherit here rather loosely because interface inheritance doesn’t work exactly like class inheritance. I prefer to think of interface inheritance as including the contract of other interfaces. For example, the ICollection<T> interface definition includes the contracts of the IEnumerable<T> and IEnumerable interfaces.


This means that:

 

■ Any class that inherits the ICollection<T> interface must implement all of the methods



defined by the ICollection<T>, IEnumerable<T>, and IEnumerable interfaces.

 

■ Any code that expects an object whose type implements the ICollection<T> interface can assume that the object’s type also implements the methods of the IEnumerable<T> and IEnumerable interfaces.

 

 


Date: 2016-03-03; view: 678


<== previous page | next page ==>
Nbsp;   Verifiability and Constraints | Nbsp;   Inheriting an Interface
doclecture.net - lectures - 2014-2025 year. Copyright infringement or personal data (0.038 sec.)