Interface:
- Access modifiers are not allowed, and all interface members are public.
- An interface member cannot contain a code body.
- An interface cannot define a field member.
- Interface members cannot be defined by using the keyword static,vritual,abstract,sealed.
- 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