An introduction to c structures and classes

  • 2020-05-07 20:20:10
  • OfStack

We don't care about how the object is implemented internally, we care about what interface it gives me, what operations it has. Technically, a structure is a value type, and a class is a reference type. A structure cannot specify an inherited base class type; a class can. But both structures and classes implement interfaces.
1. Application situation
Application of structure:

1. Custom data types, data members are public, provide utility functions.

2. Abstract data type, whose data members are sealed, provides relevant data manipulation functions.

In a word, it's all about data.

Application of class:

1. Provide a group of classes, form an organic whole, form a system, class data members are sealed, only provide mutual communication function interface.

Class consists of a system that communicates between classes mainly through different classes. The class itself is part of the system.

2. Membership and accessibility
As tools for abstracting data types, classes and structures provide rich encapsulation capabilities.

1. Field definition data member, no encapsulation

2. Property encapsulates the access method of the field

3. Functions provide available operations

4. Events provide a model for processing messages

5. Indexers encapsulate data sets

In addition

Constructors, destructors are responsible for initializing and cleaning up the garbage (IDispose mode is required in c#)

For members, there can be three categories: 1. Class members, all instances are Shared; 2. Object public member; 3. Object private member. The public members are the most important, and the private members are the internal implementation details.

Public members are a feature of the object because, for the consumer, they can only interact with the object through public members. The exposed members can be extracted to form a separate interface that can be used to isolate specific implementations. In this way, the designer can provide different class implementations to the client.

Generics and interfaces
What classes and structures have in common is that they belong to the implementation, while the interface belongs to the specification, and the client should avoid touching the specific implementation directly, or the client will need to change according to the implementation, which is obviously uneconomic. However, there is a difference between classes and structures. Classes focus on behavior, so they are more compatible with interfaces. While the structure focuses on data, the interface has no data, and the interface belongs to the reference type. When the structure is converted into the interface as a value type, boxing will occur, which will cause performance problems. Therefore, interface 1 should not be used in conjunction with the structure, which should be treated as a simpler data unit and should not add too much functionality. If you need to design feature-rich components, it is best to implement them on a class-based basis. When a structure is a data unit, it has little scope to modify the design, because the application of a data unit is more likely to be modified by others, with less autonomy. The conclusion is that the structure itself can be the norm.

Generics are unfinished types and therefore provide opportunities for clients to customize types. Each instance of a generic type shares the same or similar code, so what is the reason for the client to need a generic type?

c# is a strongly typed system, even if logically 1, because different types are completely different code to the compiler. Generics provide a quick way to bypass strong typing restrictions when clients need to take the same logic for different types. For example, there is an int + int and float + float that looks almost like 1 to us as humans, but strongly typed compilers require you to write code repeatedly, generics do T + T, and then let the client generate any type with arguments that support the + operation.

From the client's perspective, it doesn't care what the implementation is, it CARES what the interface is. What generics mean for clients is that they enhance interfaces, from providing a specific type of interface to a new type of interface that provides a range of types that satisfy a specific condition. This makes usability much better. In practice, we should consider generics as the norm, and a single type of interface as a special state.


Related articles: