INTERFACES OF C#
Interface
Previously in the C# Object Oriented Programming tutorial the constituent interface has been utilised to exposit the open programme of a class. The open programme contains methods, properties, indexers and another items that crapper be accessed by another classes. In this article, a second, attendant definition of programme is considered.
An programme is a cipher scheme that is kindred to an nonfigurative collection that has no objective members. An programme crapper allow open members much as properties and methods but these members staleness hit no functionality. Instead, same nonfigurative members, they delimitate items that staleness be prefabricated objective within every classes that implement the interface. This effectuation that an programme crapper be utilised to delimitate what a collection staleness do, but not how it module attain it.
Many classes haw compel a azygos interface. This increases polymorphism by sanctioning classes to bear in whatever assorted structure depending upon their usage. For example, a Customer class, an Employee collection and a Business collection haw every compel the IWriteable interface. If this programme contains properties describing an address, a method that accepts an IWriteable goal as a constant crapper be utilised to beam a honor to whatever of these entity types. NB: The “I” prefix for an programme is a constituted denotive convention.
Unlike with the azygos acquisition provided by C#, a collection haw compel whatever interfaces. These haw be implemented in constituent to acquisition from a humble class. This compounding provides a simplified alteration upon the binary acquisition construct that is not acquirable to .NET programmers.
When to Choose an Interface or Abstract Class
Interfaces and nonfigurative classes hit kindred purposes. Generally, an programme should be utilised when the feat of the members it defines is not fixed, or when it is famous that a azygos collection requires the table of individual interfaces. Where the functionality of whatever members is immobile and the restricted binary acquisition artefact is not required, nonfigurative classes haw be more appropriate.
Creating Interfaces
To shew the ingest of interfaces we module create individual and administer them to a assemble of classes. In this article we module delimitate interfaces and classes that equal predators and beast animals. Each birdlike collection module compel at small digit of the IPredator and IPrey interfaces.
To begin, create a newborn housing covering titled “InterfacesDemo”.
Declaring an Interface
An programme is proclaimed in a kindred behavior to a class. The definition of the programme appears in a namespace, or within the choice namespace. The interface’s study is prefixed with the interface keyword and requires a cipher country to allow the members that it defines.
The prototypal programme that we module create module be implemented by every animals that are beast to predators. To ingest the gathering for programme naming, this programme module be titled “IPrey”. To create the interface, add a newborn programme enter to the send titled “IPrey”. If your desirable utilization surround does not allow a model for interfaces, only add a collection enter and change the papers of the collection to correct that shown below:
interface IPrey
{
}