SoFunction
Updated on 2025-03-07

Detailed explanation of the connections and differences based on C# classes, interfaces, and structures

1. The difference between C# class and structure
1. Value type and reference type
Structure is a value type: the value type allocates an address on the stack, and all base types are structure types. For example: int corresponds to System.int32 structure, string corresponds to structure. By using structure, more value types can be created.
Class is a reference type: the reference type allocates the address on the heap
The execution efficiency of the stack is higher than that of the heap, but the resources of the stack are limited and are not suitable for handling large, logically complex objects. So the structure process is treated as a small object treated as a base type, while the class processes a certain business logic
Because structures are value types, assignments between structures can create new structures, while classes are reference types, and assignments between classes are just copying references
1).Although the structure and class type are different, their base types are objects (objects). All types of base types in C# are objects
2).Although the structure initialization also uses the New operator, the structure object is still allocated on the stack rather than on the heap. If "new" is not used, the fields will remain unassigned until all fields are initialized and the object is not available.

2. Inheritance
structure:
It cannot be inherited from another structure or class, and itself cannot be inherited. Although the structure is not explicitly declared with sealed, the structure is an implicit sealed class: it is completely extensible. Unless the displayed declaration sealed, the class can inherit other classes and interfaces, and itself can be inherited.
Note:Although the structure cannot be inherited, the structure can inherit the interface. Methods are the same as class inheritance interfaces. The structure does not have a default constructor or a destructor, but it can only construct a constructor with parameters. The structure cannot initialize fields: such as
struct a{ int i=0} is wrong, it should struct a{int i;}, and can only be assigned during initialization: a a1=new a();=1;

1. C# abstract class:
C# abstract classes are special classes, but they cannot be instantiated; in addition, they have other characteristics of classes; what is important is that abstract classes can include abstract methods, which ordinary classes cannot. Abstract methods can only be declared in abstract classes and do not contain any implementations, and derived classes must override them. In addition, abstract classes can be derived from an abstract class, and abstract methods of base classes can be overwritten or not. If not overwritten, their derived classes must overwritten them.

2. C# interface:
The C# interface is a reference type, similar to a class, and has three similarities with abstract classes:
1. Cannot be instantiated;
2. Contains unimplemented method declarations;
3. The derived class must implement unimplemented methods. Abstract classes are abstract methods, and interfaces are all members (not only methods, including other members);
in addition,The interface has the following characteristics:
In addition to methods, interfaces can also contain attributes, indexers, and events, and these members are defined as public. Apart from this, no other members can be included, such as: constants, domains, constructors, destructors, and static members. A class can directly inherit multiple interfaces, but can only directly inherit one class (including abstract classes).

3. The difference between C# abstract classes and interfaces:
1. Classes are abstractions of objects. Abstract classes can be understood as treating classes as objects. The abstract class is called abstract classes. Interfaces are just a norm or regulation of behavior. Microsoft's custom interfaces are always followed by a able field, proving that they are expressing a class of classes "I can do it...". Abstract classes are more defined between a series of closely related classes, while most interfaces are classes with loose relationships but all implement a certain function.
2. The interface basically does not have any specific inheritance characteristics, it only promises methods that can be called;
3. A class can implement several interfaces at a time, but can only extend one parent class
4. Interfaces can be used to support callbacks, but inheritance does not have this feature.
#Abstract classes cannot be sealed.
#The specific methods implemented by abstract class are virtual by default, but the interface methods in the class that implements interfaces are defaulted to non-virtual, and of course you can also declare them as virtual.
7. (Interface) Similar to non-abstract classes, abstract classes must also provide its own implementation for all members of the interface listed in the base class list of that class. However, abstract classes are allowed to map interface methods to abstract methods.
8. Abstract classes implement a principle in OOP, separating mutable and immutable. Abstract classes and interfaces are defined as immutable, and the variable seat subclass is implemented.
9. A good interface definition should be specific and functional, rather than multifunctional, otherwise it will cause interface contamination. If a class only implements one of the functions of this interface and has to implement other methods in the interface, it is called interface pollution.
10. Try to avoid using inheritance to achieve the assembly function, but use black box reuse, that is, object combination. Because the inheritance levels increase, the most direct consequence is that when you call a certain category in this group, you must load them all into the stack! The consequences can be imagined. (understanding in combination with the stack principle). At the same time, friends who are interested can notice that Microsoft often uses the method of object combination when building a class. For example, the Page class has properties such as Server Request, but in fact they are all objects of a certain class. Using this object of the Page class to call the methods and properties of another class is a very basic design principle.
11. If an abstract class implements an interface, the methods in the interface can be mapped into the abstract class as abstract methods without having to be implemented, and methods in the interface can be implemented in the subclass of the abstract class.

4. Use of C# abstract classes and C# interfaces:
1. If you are expected to create multiple versions of the component, create an abstract class. Abstract classes provide simple ways to control component versions.
2. If the created function will be used between a wide range of different objects, use the interface. If you want to design small and concise function blocks, use the interface.
3. If you want to design large functional units, use abstract classes. If you want to provide common implemented functions among all implementations of the component, use abstract classes.
4. Abstract classes are mainly used for closely related objects; while interfaces are suitable for providing general functions for unrelated classes.

The following are a few image metaphors I saw on the Internet. They are really good, haha:
1. Airplane flying, birds can fly, and they all inherit the same interface "flying"; but F22 belongs to the aircraft abstract category, and pigeons belong to the bird abstract category.
2. Just like iron doors and wooden doors are all doors (abstract class), I can’t give you a door if you want (can’t be instantiated), but I can give you a specific iron door or wooden door (polymorphism); and it can only be a door, you can’t say it is a window (single inheritance); a door can have a lock (interface) or a doorbell (multiple implementation). The gate (abstract class) defines what you are, and the interface (lock) specifies what you can do (it is best for an interface to do only one thing, you cannot require the lock to make sounds (interface pollution).