C # interface basics,
The interface only contains the signature of the method, attribute, event, or indexer.. The class or structure that implements the interface must implement the interface member specified in the interface definition.
Can the interface contain fields?
When I was asked this question for the first time, I was stunned. I can only answer: I have never seen variables defined in interfaces, But I just defined some behaviors and contracts, obviously, the answer to this question is ambiguous !!! Why?
I personally verified it. For example, if it proves that my impression is correct..., I am glad that I am. Maybe cainiao only has this pursuit,
But why?
The first is an article from a senior in CSDN. Click to enter.
This is a good description, so you can extract it for better understanding.
Interfaces describe rules and are used to define contracts and constraints. constraints are for behaviors;
Interfaces can define attributes, methods, index indicators, and events;
But it cannot contain constants, fields (fields), operators, constructors, and destructor, and it cannot contain any static members;
An interface cannot exist independently and cannot be instantiated just like an instantiated class;
The field must be allocated for memory instantiation;
Besides, the interface cannot contain any code (including fields?) that implements its members ?);
Only members can be defined. The implementation process must be completed in the class that implements the interface.
At this point, I finally found a better answer to this question. If not, please correct me.