The previous article is also the first question of Lao Zhao's interview questions. This article continues, hoping to find the key points of our study through these interview questions, instead of carrying these things, and hopes to systematically learn these points. net.
What is the difference between class and struct? Do they affect performance ?. What are the classes (structures) in the net BCl, and why are they not structures (classes )? How do you select a class or structure for custom types?
Let's take a look at the C # language standard definition on msdn:
Class and structure are two basic structures of the general type system in. NET Framework. Both of them are essentially data structures that encapsulate a group of data and behavior as a logical unit. Data and behavior are "members" of the class or structure. They contain their respective methods, attributes, and events.
The declaration of a class or structure is similar to a blueprint for creating an instance or object at runtime. If you define a class or structure named person, person is the type name. If the Variable P of the person type is declared and initialized, P is called the object or instance of the person type. You can create multiple instances of the same person type. Each instance has different values in its attributes and fields.
Class is a "reference type ". When creating a class object, the variables assigned to the object only save the reference to the memory. When an object reference is assigned to a new variable, the new variable references the original object. Changes made using one variable are reflected in another variable because the two reference the same data.
Structure is a value type. When a structure is created, the variable assigned to the structure stores the actual data of the structure. When a structure is assigned to a new variable, the structure is copied. Therefore, the new and original variables contain two different copies of the same data. Changes to one copy do not affect the other.
Classes are usually used to model complex behaviors or to model data that needs to be modified after a class object is created. The structure is most suitable for some small data structures. The data contained in these data structures is mainly data that is not modified after the structure is created.
The structure shares most of the same syntax with the class, but the structure is more restrictive than the class.
- In structure declaration, initialization fails unless the field is declared as const or static.
The default constructor (constructor without parameters) or destructor cannot be declared in the structure.
Structure is copied when values are assigned.
When a structure is assigned to a new variable, all data is copied, and any modifications made to the new copy do not change the data of the original copy. When using a set of value types (such
Dictionary <string, mystruct>.
The structure is the value type, and the class is the reference type.
Unlike classes, the new operator can be used for structure instantiation.
You can declare constructors with parameters.
A structure cannot inherit from another structure or class, and cannot be the base of a class.
All structures are directly inherited from system. valuetype, and the latter is inherited from system. object. (Actually, the difference between the value type and the reference type)
Structure can implement interfaces.
The structure can be null, so null values can be assigned to it.
What are classes (structures) in. Net Bcl and why are they not structures (classes )?BCL (Base
Class Library) is the class library used by all languages in the dot NET framework (which classes and structures are available, you can just say, how much is the system namespace ~ It should be noted that datetime is actually a struct .) Why are they not structures (classes? This question and the following small question are actually a question, that is, when to define the structure and when to define the class. The following answers how to choose a class or structure for the custom type?
The struct type is applicable to lightweight objects such as point, rectangle, and color. Although it is convenient to use an automatically implemented attribute to represent a vertex as a class, the structure is more effective in some cases. For example, if you declare
An array consisting of 1000 point objects. In order to reference each object, more memory needs to be allocated. In this case, the structure can save resources. Because
. NET Framework contains an object named point. Therefore, the structure in this example is named "coords ". Complex Numbers, vertices in the coordinate system, or "key-value" pairs in the dictionary are typical examples of structures.
Do not define a value type (struct) unless all the conditions are met)First, the type has the behavior of the primitive type. The type is simple. No member modifies any instance fields of the type.Second, the type does not need to be inherited from any other type.Third, the type does not derive any other types.In addition to all the preceding conditions, one of the following conditions must be met.First, the instance type is small (about 16 bytes or smaller ).Second, the type instance is large, but it is not passed as the real parameter of the method or returned through the method.