Download presentation
Presentation is loading. Please wait.
1
5.1 Being Objects and A Glimpse into Coding
Microsoft .NET 5.1 Being Objects and A Glimpse into Coding Pan Wuming 2017
2
Topics Sematic Conformity: Object Class Comparisons Between Objects
To Overload the Equality operator To Override The GetHashCode Method To Override The ToString Method MemberwiseClone Method The GetType Method References: Object Class(MSDN) C# Operators(MSDN) Hash function (Wikipedia)
3
Sematic Conformity: Object Class
The ultimate base class of all classes in the .NET Framework. To provides low-level services to derived classes. Languages typically do not require a class to declare inheritance from Object. The root of the type hierarchy The inheritance is implicit
4
The Definition of Object Class
From metadata of #region 程序集 mscorlib, Version= , Culture=neutral, PublicKeyToken=b77a5c561934e089 // C:\Program Files (x86)\Reference Assemblies \Microsoft\Framework \.NETFramework\v4.0\mscorlib.dll #endregion
5
Derived classes can override these methods:
Equals Finalize GetHashCode ToString
6
The destructor implicitly calls Finalize on the base class of the object. Therefore, the destructor code is implicitly translated to the following code:
7
Supports comparisons between objects
Determines whether the specified Object instances are the same instance. public static bool Equals(Object objA, Object objB); public static bool ReferenceEquals(Object objA, Object objB); public virtual bool Equals(Object obj);
8
The Equals(Object) method
The current instance is a reference type: Tests for reference equality The current instance is a value type: The two objects are of the same type The values of the public and private fields of the two objects are equal Equivalent to a call to the ReferenceEquals method.
9
The static Equals(Object, Object) method
IF The two objects represent the same object reference, or both are null, it returns True. If Either objA or objB is null, it returns false. If the two objects do not represent the same object reference and neither is null, it calls objA.Equals(objB) if objA overrides the Object. Equals(Object) method, this override is called.
11
To overload the equality operator (==)
To overload an operator on a custom class requires creating a method on the class with the correct signature. The method must be named "operator X" where X is the name or symbol of the operator being overloaded. Unary operators have one parameter, and binary operators have two parameters. In each case, one parameter must be the same type as the class or struct that declares the operator.
17
The GetHashCode method
A hash code is a numeric value that is used to insert and identify an object in a hash-based collection Dictionary<TKey, TValue> class, Hashtable class For algorithms that need quick checks of object equality. You should not assume that equal hash codes imply object equality.
18
Properties of Good hash functions
Determinism Uniformity Defined range Variable range Data normalization Continuity Non-invertible
19
Hash functions are primarily used in hash tables, to quickly locate a data record (e.g., a dictionary definition) given its search key (the headword). Specifically, the hash function is used to map the search key to an index; the index gives the place in the hash table where the corresponding record should be stored. Hash tables, in turn, are used to implement associative arrays and dynamic sets. Typically, the domain of a hash function (the set of possible keys) is larger than its range (the number of different table indices), and so it will map several different keys to the same index. Therefore, each slot of a hash table is associated with (implicitly or explicitly) a set of records, rather than a single record. For this reason, each slot of a hash table is often called a bucket, and hash values are also called bucket indices. Thus, the hash function only hints at the record's location — it tells where one should start looking for it. Still, in a half-full table, a good hash function will typically narrow the search down to only one or two entries. Hash tables
20
Override The GetHashCode method
One way to generate a hash code is to combine these fields using an XOR (eXclusive OR) operation
23
When Commenting out the Equality methods and operations
27
When Commenting out the GetHashCode method
28
Under the Visual objects
31
ToString Method Object. ToString is the major formatting method in the .NET Framework. Default implementations of the Object. ToString method return the fully qualified name of the object's type.
33
MemberwiseClone Method
The MemberwiseClone method creates a shallow copy by creating a new object, and then copying the nonstatic fields of the current object to the new object. If a field is a value type, a bit-by-bit copy of the field is performed. If a field is a reference type, the reference is copied but the referred object is not; therefore, the original object and its clone refer to the same object.
34
The GetType Method Type is the root of the System.Reflection functionality Type is the primary way to access metadata.
36
See you next class! You have known about: The root of the type hierarchy Comparisons between objects Overload of Equality Operator GetHashCode Method Type Class To understand a language is not just to understand its syntax
37
Appendices: operators in C#
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.