interface in C #

Source: Internet
Author: User

Interface (interface)

An interface refers to an abstraction that an entity provides itself to the outside world (another entity) that separates external communication methods from internal operations so that they can be modified internally without affecting the way other entities interact with the outside world.

An interface is actually a convention :

such as:iclonable, IComparable;

An interface is a collection of abstract members:

ICloneable contains Method clone ();

IComparable contains method compare ();

An interface is a reference type that is more abstract than abstract.

Help implement multiple Inheritance:

The use of interfaces:

1. implement the same behavior of unrelated classes without having to consider the hierarchical relationships of these classes;

2. interfaces allow you to understand the interface of an object without needing to know the class in which the object resides. For example: Publicsealed class string:icloneable, IComparable, IConvertible, IEnumerable.

Define an interface:

 Public Interface istringlist{voidstring  s); int Get ; } string  This [intgetset;}} // Public Abstract Default, these two keywords do not write out

Implementing the interface:

class class Name: [Parent class,] interface, interface, interface, ..... interface { public method () {...}}

Display interface member implementations:

When implementing multiple interfaces, if different interfaces have a method of the same name, in order to disambiguate, you need to write the interface name before the method name: void Iwindow.close () {...};

When invoked, it can only be called with an interface: ((Iwindow) f). Close ();

interface Example:

usingSystem;namespacetestinterface{InterfaceRunner {voidrun (); }    InterfaceSwimmer {voidswim (); }    Abstract classAnimal {Abstract  Public voideat (); }    classPerson:animal, Runner, Swimmer { Public voidRun () {Console.WriteLine ("Run"); }         Public voidswim () {Console.WriteLine ("Swim"); }         Public Override voidEat () {Console.WriteLine ("Eat"); }         Public voidspeak () {Console.WriteLine ("speak"); }    }    classProgram {Static voidM1 (Runner R) {R.run (); }        Static voidm2 (Swimmer s) {s.swim (); }        Static voidm3 (Animal a) {a.eat (); }        Static voidM4 (Person p) {p.speak (); }                 Public Static voidMain (string[] args) {Person P=NewPerson ();            M1 (P);            M2 (p);            M3 (p);            M4 (P); Runner a=NewPerson ();                        A.run (); Console.readkey (true); }    }}
using the interface sample

interface in C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.