Implementation of the c#--interface

Source: Internet
Author: User

Interface:

      1. Access modifiers are not allowed, and all interface members are public.
      2. An interface member cannot contain a code body.
      3. An interface cannot define a field member.
      4. Interface members cannot be defined by using the keyword static,vritual,abstract,sealed.
      5. The type definition member is forbidden.

If you want to hide members that inherit from an interface, you can define them with the keyword new.

public interface imyinterface{    void DoSomething ();} Public interface imydeinterface:imyinterface{    new void DoSomething ();}

The properties defined in the interface can confirm which of the access blocks get and set can be used for the property.

public interface imyinterface{    int myNum    {        get;        set;    }}

Note: You cannot specify a field in an interface.

To implement an interface in a class:

When a class implements an interface, you can use abstract or virtual to execute the members of the interface, instead of using static or const.

If an excuse is implemented for a class, the derived class of the class implicitly supports the interface.

Explicitly executing an interface member

An interface member can be explicitly executed by a class. If you do this, then this member can only be accessed through the interface. and cannot be accessed through the class.

public interface imyinterface{    void DoSomething ();} public class myclass:imyinterface{        void Imyinterface.dosomething ()    {        throw new NotImplementedException ( );    }}
    protected void Page_Load (object sender, EventArgs e)    {        //There is no method for MC at this time.        MyClass mc = new MyClass ();        At this point my has a method dosomething ()        imyinterface my = mc;        My. DoSomething ();    }
Implicitly executing interface members
The default is to execute an interface member implicitly.
public interface imyinterface{    void DoSomething ();} public class myclass:imyinterface{public        void DoSomething ()    {        throw new notimplementedexception (); c19/>}}
    protected void Page_Load (object sender, EventArgs e)    {        MyClass mc = new MyClass ();        Mc. DoSomething ();    }

Class implements interface properties

public interface imyinterface{    int MyNum    {        get;    }} public class myclass:imyinterface{    protected int myNum;    public int MyNum    {        get        {             return MyNum;         }        Set        {            myNum = value;}}    }
    protected void Page_Load (object sender, EventArgs e)    {        MyClass mc = new MyClass ();        Mc. MyNum = n;    }

Implementation of the c#--interface

Related Article

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.