First, if you know what the value type is and what the reference type is, you also understand that the value type variables are directly stored on the stack, the reference type stores an address on the stack, which is the real location of the Instance Object that references the type variable on the hosting stack. The following are some small conclusions, some of which may not have very detailed and in-depth Logical Reasoning Processes.
Next, let's take a look at Objective C # Item 9: differentiate and recognize the four classification functions, http://www.cnblogs.com/tonytonglx/articles/208020..html. Referenceequals, static equals, specific equals and = operators, which are the following two comparison functions, that is, the equals method and = operator of the specific type.
1. valuetype is a base class of all value types, but valuetype itself is also integrated with objects;
2. The object type is the parent class of all types, including all value types and reference types. However, the first point is that the value type is inherited from valuetype, and the reference type is inherited from object. Why? Because valuetype has changed many things when inheriting objects, including rewriting the equals method and reloading the = Operator, the difference between the value type and the reference type is rooted in this;
3. Start with the = Operator. For the value type, = is used to compare whether the two types are equal in numerical values. For example, if 1 = 1.0 is true, there is no doubt. For the reference type, = compares whether two variables of the reference type point to the same instance object, and the effect is equal to referenceequals ();
4. Let's talk about equals. In an object, the public override bool equals (Object OBJ) method supports reference equality (for reference type) and bitwise equality (for value type) by default ).
Equality of reference refers to the comparison of Multiple object references that reference the same object. Bitwise equality means that multiple objects to be compared have the same binary representation. Note that bitwise equality and Value Equality are different. Value Equality means that the compared objects have the same value, even if they have different binary representations. It can be simply concluded that the values are equal as long as the two objects are equal from the numerical point of view, while the values are equal. The two objects are of the same type, and then the values are equal from the numerical point of view.
Therefore, when the equals method of the value type is called, the system first determines whether it is of the same type. If yes, it determines whether the two values are equal in mathematics, if yes, true is returned. For example, int A = 1;. equals (1.0) returns false because 1.0 is of the double type by default in C # and is not of the same type as int type A. Therefore, false is returned, although they are equal in numbers.
For the reference type, equals compares the reference equality by default, which has the same effect as the referenceequals method and the = Operator. Of course, you can override the equals method.
Summary: For the value type, = is a numerical value, equals compares whether it is of the same type first, if yes, then the numerical value. For the reference type, = and equals compare whether the same instance object is referenced by default.
As for the principle of rewriting the equals method or the overload = Operator, it is another problem, which is not involved here.
The following describes the particularity of the string and object types.
The special feature of the string type is that the string is "immutable". When CLR is initialized, it creates an internal hash with the key as the string, value is a reference to a string object in the managed heap. When constructing a new String object, it first goes to the hash list to check whether there is a string with the same value as it. If yes, it directly points the New String object to that string, if it does not exist, a New String object will be constructed in the managed heap, and the string value and reference pointing to the object will be added to the hash list.
However, the dynamically created string does not query the hash list, but directly creates a new String object in the managed heap. See http://www.cnblogs.com/qingxia/archive/2011/01/06/1928896.html
To obtain the variable string type, use stringbuilder.
The object type is also immutable. As follows:
int a = 2; object obj1 = a; object obj2 = obj1; obj2 = 10; Console.WriteLine("{0}、{1}、{2}",a,obj1,obj2);
Output: 2, 2, 10. That is to say, the change of obj2 does not affect obj1.
Console. writeline (object. referenceequals (obj1, obj2); outputs false, which means that obj1 and obj2 have already pointed to different instance objects.
Newbie, welcome to discuss and put forward your own views.