In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to define an interface in C#", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to define an interface in C#".
Technically, an interface is a set of data structures that contain functional methods. Through this set of data structures, the client code can invoke the function of the component object.
The general form of defining an interface is:
[attributes] [modifiers] interface identifier [: base-list] {interface-body} [;]
Description:
Attributes (optional): additional defining information.
Modifiers (optional): allowed modifiers are new and four access modifiers. They are: new, public, protected, internal, private. The same modifier is not allowed to occur multiple times in an interface definition, and the new modifier can only appear in a nested interface, indicating that inherited members of the same name are overwritten. The The public, protected, internal, and and private modifiers define access to the interface.
Indicators and events.
Identifier: interface name.
Base-list (optional): contains a list of one or more explicit base interfaces separated by commas.
Interface-body: the definition of interface members.
An interface can be a member of a namespace or class and can contain the signatures of the following members: methods, properties, indexers.
An interface can inherit from one or more base interfaces.
The concept of interface is very similar in C # and Java. The keyword of an interface is interface, and an interface can extend one or more other interfaces. By convention, the name of the interface begins with the capital letter "I". The following code is an example of the C# interface, which is exactly the same as the interface in Java:
Interface IShape {void Draw ();}
If you derive from two or more interfaces, the list of parent interface names is separated by a comma, as shown in the following code:
Interface INewInterface: IParent1, IParent2 {}
However, unlike Java, the C# interface cannot contain a Field. Also note that in C #, all methods within an interface are common methods by default. In Java, a method definition can have a public modifier (even if it is not necessary), but in C # it is illegal to explicitly specify a public modifier for a method of an interface. For example, the following C# interface will generate a compilation error.
Interface IShape {public void Draw ();}
The following example defines an interface called IControl, which contains a member method Paint:
Interface IControl {void Paint ();}
In the following example, interface IInterface inherits from two base interfaces, IBase1 and IBase2:
Interface IInterface: IBase1, IBase2 {void Method1 (); void Method2 ();}
Interfaces can be implemented by classes. The identifier of the implemented interface appears in the base list of the class. For example:
Class Class1: Iface1, Iface2 {/ / class member. }
When the base list of a class contains both the base class and the interface, the base class appears first in the list. For example:
Class ClassA: BaseClass, Iface1, Iface2 {/ / class members. }
The following code snippet defines the interface IFace, which has only one method:
Interface IFace {void ShowMyFace ();}
You cannot instantiate an object from this definition, but you can derive a class from it. Therefore, the class must implement the ShowMyFace abstract method:
Class CFace:IFace {public void ShowMyFace () {Console.WriteLine ("implementation");}}
Base interface
An interface can inherit from zero or more interfaces, which are called explicit base interfaces for this interface. When an interface has more explicit base interfaces than zero, the interface identifier takes the form of an interface identifier followed by a colon ":" and a separate list of base interface identifiers with a comma.
Interface base:
Description of interface type list:
The explicit base interface of an interface must be at least as accessible as the interface itself. For example, it is wrong to specify a private or internal interface in the base interface of a public interface.
It is wrong for an interface to inherit directly or indirectly from itself.
The base interfaces of interfaces are explicit base interfaces and are their base interfaces. In other words, the collection of base interfaces consists entirely of explicit base interfaces, their explicit base interfaces, and so on. In the following example
Interface IControl {void Paint ();} interface ITextBox: IControl {void SetText (string text);} interface IListBox: IControl {void SetItems (string [] items);} interface IComboBox: ITextBox, IListBox {}
The base interfaces of IComboBox are IControl, ITextBox, and IlistBox.
An interface inherits all members of its base interface. In other words, the above interface IComboBox inherits the members SetText and SetItems just like Paint.
A class or structure that implements an interface also implicitly implements the base interface of all interfaces.
Interface body
The interface principal of an interface defines the members of the interface.
Interface-body: {interface-member-declarationsopt}
Define interface members
An interface can contain one or more members, which can be methods, properties, index indicators, and events, but cannot be constants, fields, operators, constructors, or destructors, and cannot contain any static members. The interface definition creates a new definition space, and the interface member definition that the interface definition contains directly introduces the new member into the definition space.
Description:
The members of an interface are members inherited from the base interface and defined by the interface itself.
An interface definition can define zero or more members. Members of an interface must be methods, properties, events, or indexers. Interfaces cannot contain constants, fields, operators, instance constructors, destructors, or types, nor can they contain static members of any kind.
Define an interface that contains one for each possible category of members: methods, properties, events, and indexers.
The default access method for interface members is public. The interface member definition cannot contain any modifiers, such as abstract,public,protected,internal,private,virtual,override or static modifiers before the member definition.
Members of an interface cannot have the same name as each other. Inherited members no longer need to be defined, but interfaces can define members with the same name as inherited members. In this case, we say that interface members overwrite inherited members, which does not cause an error, but the compiler gives a warning. Turn off the warning prompt by adding a new keyword before the member definition. However, if members in the parent interface are not overridden, using the new keyword causes the compiler to issue a warning.
The name of the method must be different from the names of all properties and events defined in the same interface. In addition, the signature of a method must be different from that of all other methods defined in the same interface.
The name of the property or event must be different from that of all other members defined in the same interface.
The signature of one indexer must be different from that of all other indexers defined in the same interface.
The properties (attributes), return type (return-type), identifier (identifier), and formal parameter list (formal-parameter-lis) in the interface method declaration have the same meaning as those in the method declaration of a class. An interface method declaration does not allow you to specify a method body, and the declaration usually ends with a semicolon.
The accessor declared by the interface property corresponds to the accessor declared by the class property, except that the accessor body must usually use a semicolon. Therefore, the accessor is fully determined whether the property is read-write, read-only, or write-only.
The attributes (attributes), type (type), and formal parameter list (formal-parameter-list) in the interface index declaration have the same meaning as those declared by the class index.
In the following example, the interface IMyTest contains members such as index indicator, event E, method F, and property P:
Interface IMyTest {string this [int index] {get; set;} event EventHandler E; void F (int value); string P {get; set;}} public delegate void EventHandler (object sender, EventArgs e)
In the following example, the interface IStringList contains the interface for each possible type member: a method, a property, an event, and an index.
Public delegate void StringListEvent (IStringList sender); public interface IStringList {void Add (string s); int Count {get;} event StringListEvent Changed; string this [int index] {get; set;}}
Full names of interface members
Interface members can also use full names (fully qualified name). The full name of the interface is made up of this. Add a dot to the interface name. " And then with the member name, for example, for the following two interfaces:
Interface IControl {void Paint ();} interface ITextBox: IControl {void GetText (string text);}
The full name of Paint is IControl.Paint,GetText and the full name of ITextBox. GetText . Of course, the member name in the plenipotentiary name must be defined in the interface, such as using ITextBox.Paint. It's just unreasonable.
If the interface is a member of the namespace, the full name must also contain the name of the namespace.
Namespace System {public interface IDataTable {object Clone ();}}
Then the full name of the Clone method is System. IDataTable.Clone .
After defining the interface, we are concerned about how to implement access to the C# interface.
The above is all the content of the article "how to define Interface in C#". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.