Home >Backend Development >C#.Net Tutorial >What is System.Object? How to use System.Object?

What is System.Object? How to use System.Object?

零下一度
零下一度Original
2017-06-23 15:26:422669browse
Explanation of public methods in Object:
Public methods:
Equals:
public class Object {
public virtual Boolean Equals(Object obj )
{
//If two references point to the same object, they must contain the same value
if (this == obj) return true;
return false;
}
}
Assuming that this and obj instances refer to the same object, return true. It seems reasonable because Equals knows that the object must contain the same value as itself. But if the instances refer to different objects, Equals is not sure whether the objects contain the same value, so it returns false. In other words, for the default implementation of Object's Equals method, what it actually implements is identity, not equality.
Correct implementation of the Equals method:
1. If the obj actual parameter is null, return false, because when calling the non-static Equals method, the current object identified by this obviously cannot be null.
2, if this and obj actual parameters refer to the same object, return true. This step can help improve performance when comparing objects that contain a large number of fields
3. If the this and obj arguments refer to objects of different types, return false. A string object is obviously not equal to a FileStream object
4. For each field defined by the type, compare the value in this object with the value in the obj object. If any field is not equal, return false
5, call the Equals method of the base class to compare any fields defined by it. If the Equals method of the base class returns false, it returns false, otherwise it returns true
Note:
The test for identity issues uses Object’s static method ReferenceEquals, whose prototype is as follows:
public static Boolean ReferenceEquals(Object objA, object objB)
{
return (objA==objB);
}
Check the identity issue (to see if the two references point to the same object). Be sure to call ReferenceEquals. You should not use the C# == operator (unless First convert both operands to Object), because the type of a certain operand may be overloaded with the == operator, and its assigned value has different semantics than identity.
System. ValueType (the base class of all value types) overrides the Equals method of Object and implements the correct implementation to perform equality checking of values ​​(rather than identity checking). The internal implementation of Equals of ValueType is as follows:
1, if the obj actual parameter is null, return false
2, if this and obj actual parameters refer to objects of different types, return false
3, for each type definition Instance fields, all compare the value in this object with the value in obj object (instead of identity check). The internal implementation of Equals of ValueType is as follows:
1, if the obj actual parameter is null, return false
2, if this and obj actual parameters refer to objects of different types, return false
3. For each instance field defined by the type, compare the value in this object with the value in the obj object (by calling the equals method of the field). If any field is not equal, return false
4, returns true. The Equals method of ValueType does not call the Equals method of Object
Internally, the Equals method of ValueType uses reflection to complete step 3 above. Since the CLR reflection mechanism is slow, you should override the Equals method to provide your own when defining your own value type. Implementation, thereby improving the performance of equality comparisons with the strength of your own type. Of course, your own implementation does not call base.Equals.
Note:
When defining your own type, override Equals must comply with the four characteristics of equality
1, Equalse must be reflexive: x.Equals(x) must return true
2, Equals must be symmetrical: x.Equals(y) and y.Equals(x) returns the same value
3, Equals must be transferable: x.Equals(y) returns true, y.Equals(z) returns true, then x.Equals(z) must return true
4, Equals must be consistent. Comparing two values ​​remains unchanged, and the return value of Equals remains unchanged
GetHashCode:
When the class you define overrides the Equals method, you must also override the GetHashCode method . The reason for this requirement is that the implementation of the System.Collections.Hashtable type, System.Collection.Generic.Dictionary type and some other collections requires that two objects must have the same hash code to be considered equal. Therefore, if you rewrite Equals, you must rewrite GetHashCode to ensure the consistency of the equality algorithm and the object hash code algorithm. To put it simply, to add a key/value pair to a collection, you must first obtain the hash code of the object. The hash code indicates into which hash bucket the key/value pair is to be stored. When a collection needs to look up a key, it obtains the hash code of the specified key object, which identifies the hash bucket that is now to be searched in a sequential manner, where it will look for key objects equal to the specified key object. Using this algorithm to store and lookup keys means that once a key object in the collection is modified, the collection can no longer find the object. Therefore, when you need to modify the key object in the hash table, the correct approach is to remove the original key/value pair, modify the key object, and then add the new key/value pair back to the hash table.
It may not be difficult to customize the GetHashCode method, but it depends on the data type and data distribution. An example and rules will be given below:
internal sealed class Point
{
private readonly Int32 m_x, m_y;
public override int GetHashCode()
{
return m_x ^ m_y; //Return the XOR result of m_x and m_y
}
}
Rule:
1, this algorithm must provide Good random distribution enables the hash table to obtain the best performance
2. The accumulated GetHashCode method can be called in the algorithm and includes its return value. But generally do not call the GetHashCode method of Object or ValueType, because the implementation of both has nothing to do with high-performance hashing algorithms
3, the algorithm execution speed should be as fast as possible
4, containing the same value Different objects should return the same hash value. For example, two string objects containing the same text return the same hash code
5. The GetHashCode method implemented by System.Object knows nothing about derived types and other fields, so it returns a Guaranteed unchanged number.
ToString:
The full name of the default return type (this.GetType().FullName)
GetType:
Returns a type derived from Type An instance of Indicates the type of the object when GetType is called. The returned Type object can be used with reflection classes to obtain metadata information related to the object's type. In addition, GetType is a non-virtual method. The purpose is to prevent the class from overriding the method, concealing the enterprise type, and thereby destroying type safety.
Protected method:
MemberwiseClone: ​​
This non-virtual method creates a new instance of the class and sets the instance field of the Ajing object to this The instance fields of the objects are exactly the same. Returns a reference to the new instance.
Finalize:
This virtual method will be called after the garbage collector determines that the object is recycled as garbage and before the object's memory is actually recycled. Types that need to perform cleanup work before recycling should override this method.

The above is the detailed content of What is System.Object? How to use System.Object?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn