Based on the c class interface structure of the links and differences

  • 2020-05-17 06:12:00
  • OfStack

1. Difference between C# class and structure
1. Value type and reference type
Structures are value types: value types assign addresses on the stack, and all base types are structure types, such as int for System.int32 and string for system.string. You can create more value types by using structures
A class is a reference type: the reference type assigns an address on the heap
The stack is more efficient than the heap, but the stack has limited resources and is not suitable for handling large, logically complex objects. So the structure handles small objects that are treated as base types, while the class handles some business logic
Because structures are value types, assignments between structures can create new structures, while classes are reference types, and assignments between classes simply copy references
1). Although the structure is not the same as the type of the class, their base type is all objects (object), and the base type of all types in c# is object
2). Although the New operator is also used for the initialization of the structure, the structure object is still allocated on the stack rather than on the heap. If "new" (new) is not used, the field will remain unassigned and the object will not be available until all the fields are initialized

2. inheritance
Structure: cannot be inherited from another structure or class, nor can it itself be inherited. Although the structure is not explicitly declared with sealed, the structure is an implicit sealed class: fully extensible, unless the class is explicitly declared with sealed, it can inherit from other classes and interfaces, as well as itself
Note: although structures cannot be inherited, they can inherit interfaces, methods, and classes. Structures have no default constructors, no destructors, but can and can only construct constructors with arguments. Structure cannot initialize field: as
struct a{int i=0} is wrong, struct a{int i; }, which can only be assigned at initialization time :a a1=new a(); a1. i = 1;

1. C# abstract class:
The C# abstract class is a special class that simply cannot be instantiated; In addition, it has other properties of the class. The important thing is that abstract classes can include abstract methods, which ordinary classes can't. Abstract methods can only be declared in abstract classes and do not contain any implementations, which derived classes must override. In addition, an abstract class can derive from an abstract class, and the abstract methods of a base class can be overridden or unoverridden, if not, their derived classes must overwrite them.

2. C# interface:
The C# interface is a reference type, similar to a class, and similar to an abstract class in three ways:
1. Cannot instantiate;
2. Contains unimplemented method declarations;
3. Derived classes must implement unimplemented methods, abstract classes are abstract methods, and interfaces are all members (not only methods but other members);
In addition, the interface has the following features:
Interfaces can contain properties, indexers, events, as well as methods, and these members are all defined as common. You must not include any other members, such as constants, fields, constructors, destructors, or static members. A class can directly inherit multiple interfaces, but only one class (including abstract classes) can be directly inherited.

3. Difference between C# abstract class and interface:
Class is the abstraction of the object, which can be understood as taking the class as an object, and the abstract class is called abstract class. The interface is only a behavior specification or regulation, and Microsoft's custom interface is always followed by able field, proving that it is the expression of class 1 class "I can do...". Abstract classes are more often defined between closely related classes in series 1, while interfaces are mostly classes with loose relationships that all implement a function.
2. The interface basically does not have any specific characteristics of inheritance, it only promises methods that can be called;
3.1 classes can implement several interfaces at a time, but only one parent class can be extended
4. Interfaces can be used to support callbacks, which inheritance does not.
5.C# abstract class cannot be sealed.
The concrete methods implemented by the abstract class C# are virtual by default, but the interface methods in the class that implements the interface are non-virtual by default, or you can declare them virtual.
7. (interface) similar to a non-abstract class, an abstract class must provide its own implementation for all members of the interface listed in the base class list of that class. However, an abstract class is allowed to map interface methods to abstract methods.
8. Abstract classes implement one of the principles of oop, separating the mutable from the immutable. Abstract classes and interfaces are defined as immutable, and mutable seat subclasses are implemented.
9. A good interface definition should be one that is functional, not multifunctional, otherwise it will pollute the interface. If a class implements only one of the functions of the interface and has to implement other methods in the interface, it is called interface pollution.
10. Avoid using inheritance to implement component functionality, and instead use black-box reuse, or object composition. Because the hierarchy of inheritance increases, the most immediate consequence is that when you call a class 1 in this class group, you have to load them all onto the stack! The consequence can be imagined. At the same time, those of you who are interested will notice that Microsoft USES a lot of object composition when building a class. For example, in asp.net, the Page class has properties like Server Request, but they are all objects of a class. Using this object of class Page to call methods and properties of other classes is a very basic design principle.
11. If an abstract class implements an interface, the methods in the interface can be mapped to the abstract class as abstract methods without implementation, and the methods in the interface can be implemented in subclasses of the abstract class.

4. Use of C# abstract class and C# interface:
1. If you expect to create multiple versions of the component, create an abstract class. Abstract classes provide simple ways to control component versioning.
2. Use interfaces if the functionality you create will be used between a wide range of disparate objects. If you want to design small, concise chunks of functionality, use interfaces.
If you want to design large units of functionality, use abstract classes. If you want to provide common implemented functionality across all implementations of a component, use abstract classes.
4. Abstract classes are mainly used for closely related objects; Interfaces are suitable for providing generic functionality for unrelated classes.

Here are a few imagery metaphors I've seen online that are really good, hehe:
1. Airplanes can fly, birds can fly, they all inherit the same interface "fly"; But F22 belongs to the abstract class of airplanes, and pigeons belong to the abstract class of birds.
2. Just like iron door and wood door are all doors (abstract class), I can't give you a door if you want (can't instantiate), but I can give you a concrete iron door or wood door (polymorphic); And it can only be a door. You can't call it a window. A door can have either a lock (interface) or a doorbell (multiple implementation). The door (abstract class) defines what you are, and the interface (lock) defines what you can do (one interface should only do one thing, you can't ask the lock to make a sound (interface pollution).


Related articles: