Net Framework Common Object operations

Source: Internet
Author: User
I. Adequacy and uniqueness of Objects

The system. Object Type provides a virtual equals method to determine whether two objects have the same "value ".

Code
S/N? Dfjvn0p/ciwo (E; k0itpub personal space 7tc ,'~ Zcu
Code highlighting produced by actipro codehighlighter (freeware)
& 'F2pyoj. g; oq0http: // www.codehighlighter.com/
NL + '8leb' 0
1c7 @, n? 6 V & hj0 --> 1 class Object
# BMT. w6wc] B0 2 {itpub personal space/Q @ + \ % a qb
3 Public Virtual Boolean equals (Object OBJ) itpub personal space * g )~ "? EP
4 {
Nsjyr + g2d0 5 // if the two references point to the same object
@ H4c1m qw0 6 // they must be equal
9ffv | x) XM] K0 7 if (this = OBJ) itpub personal space {_ 9 [fclyau
8 {
6} He Z _ c3l1j] 0 9 return (true); itpub personal space 0 s? 3t9 | @ l
10}
* F + RO (f6l ~ V011 itpub personal space ~ _ Cm, h7abf
12 Return (false); itpub personal space SCSI! Z, h rc # I & L7 \ 2y
13
. ^ 5aoq K/ki014} itpub personal space # d' # @ m2r {5uq
15}

 

This is perhaps the simplest method.

However, with time, the default implementation provided by it is not enough for us. We must rewrite the equals method.

When writing your own data, follow the following four rules:

1. The equals method must be self-inverse. That is to say, X. Equals (x) must return true.

2. The equals method must be symmetric. That is to say, X. Equals (Y) and Y. Equals (x) must return the same value.

3. the equals method must be passed. that is to say, if X. equals (Y) and Y. equals (z) returns true, then X. equals (z) must also return true.

4. The equals method must be consistent. That is to say, if the values of the two objects do not change, the return values of multiple calls to the equals method should be the same.

(1) The base class does not overwrite the reference type of the object. Equals method to implement equals

 

Code
C lo * r * Y/n1u (E. M ~ 0n,} '7u5 [2f1t0
F % F/Re 'e \ 0 code highlighting produced by actipro codehighlighter (freeware) itpub personal space
^ Z + J: H |, g4r0jtv
Http://www.codehighlighter.com/itpubpersonal space d6m-v4ik5?&f

Juxz! Za + R & A. W0 --> class myreftype: basetype
X! FH "{+ G0 {
| 1gn (H! | C, @ 0 reftype refobj; // reference type
U {me7a0 valuetype valobj; // Value Type itpub personal space # V % dk fp p "T']
Itpub personal space (St ~ 7 ^ & J Ci
Public override bool equals (Object OBJ) itpub personal space ya [. e DD, rp6r
{Itpub personal space D; U; t j @ "n | I
// Because 'foo' is not null, if obj is null
| I0r * E "K3 [3z9 | 'm1c0 // then the two objects cannot be equal.
EDI * x 'iaz0itpub personal space 4t4dvp/[2 \ G
If (OBJ = NULL) itpub personal space KL '; l3muu
Return false;
Q2s + d} Z' | 0
K'n !? [/Di0 // if the two objects have different types, they cannot be equal.
1 {me yoqj] 0 if (this. GetType ()! = Obj. GetType () itpub personal space 3by $? CD + J
Return false;
(C % n'ep1z0 // convert OBJ to the defined type to access the field. Here, the itpub personal space f'm4v/CIE {2z8k! R
// Transformation will not fail, because we already know that two objects are of the same type.
0 \ v]. u I & X [B0
Bb) hgy1y 'S0 myreftype Other = (myreftype) OBJ; itpub personal space ~ 9o LZ; F % P & VL! C

] Nb @ H @ 0 // compare the reference type fields
'Tfkw M0 if (! Object. Equals (refobj, other. refobj) itpub personal space) BME Penz + W
Return false; itpub personal space 4 h WC; | 5c0 '$ 'G; EU _
Itpub personal space "_ # u6b ^ @ H ,?
// Compare the value type fields
: JC f9yce: xxb0 if (! Valobj. Equals (other. valobj ))
G5i HN, w0 return false; itpub personal space N & S ^ vgxw6 | T

He & K $ t "ve} q0 return true; // The two objects here are equal to the itpub personal space I + vs5x3q0j
} Itpub personal space S. \ F $ C * k | 0 ~ O
}

 

Note the following:

1. To compare fields of the reference type, we should call the static equals method of the object.

The static method equals of objext is an auxiliary method for comparing two referenced objects. This method is used for comparison because the code still runs normally even if the two fields have a null value.

2. to compare fields of the value type, you must call the equals method of this field type. the object's static method equals method should not be called. because the value of a value type object can never be null, and calling the static method equals of the object will perform the packing operation on the Value Type object.

(2) rewrite the reference type Implementation of object. Equals for the base class

 

Codeitpub personal space t XH (dx $ c0z ^ P
Diex! SG ${0
\ Q: PJ, u0s0code highlighting produced by actipro codehighlighter (freeware)

Cacv3h $ n "? 0 http://www.codehighlighter.com/itpubpersonal space 0u "\ 6xge o
Itpub personal space
HX # AC * p0m {[! He; F; w3t "\
--> Class myreftype: basetype
Z2nw2 _; q0 {itpub personal space U @ 3u, PV \
Reftype refobj; // reference type
AQ + _ 4x % E "hy0 valuetype valobj; // Value Type
M # | gz8m5q. W] 9 ^ kg0
UA; x3ihjn0 public override bool equals (Object OBJ) itpub personal space 2 kenrm, CJ {, Z "V
{Itpub personal space C * z) poio & DM
// First, let the base type compare the fields.
BM 'f5g0 if (! Base. Equals (OBJ ))
Ywa RKzk-I0 return false;
2scsi] S] 0
E \ B) hvz & I "O. IW # I0 // because 'I' is not null, if obj is nullitpub's personal space uf4lbe J
// The two objects cannot be equal.
N5s T-W '\ ms0
A "qhz3oi! RS & l0w/N0 if (OBJ = NULL) itpub personal space q zc! ':
Return false;
H! W4 \ G [0
& \ 3t_s_0p6ep0 // if the two objects have different types, they cannot be equal.
8 \ $ z4u [tc0 if (this. GetType ()! = Obj. GetType ())
2u0 | Yi & K ~ '# @ + Z_0 return false; itpub personal space ~ "YD % ^ & ve 'x7s
// Convert OBJ to the defined type to access the field. Here, itpub personal space 9 ratx # PK! \ L _: X % h9f! I
// Transformation will not fail, because we already know that two objects are of the same type.
P & G; \ as # P/w0itpub personal space N, ze; q X-I ^ %}
Myreftype Other = (myreftype) OBJ; itpub personal space n.f [, r0nb % ^ N $ t2tk
Itpub personal space &] rin0b2p3g8 [
// Compare the reference type field itpub's personal space/J, A8? /O9cqrkf
If (! Object. Equals (refobj, other. refobj ))
VDD] S6 [q0 return false; itpub personal space ha * ng WEDC! ^ Y

^ W4q _ & m2ph R0 // compare the value type field itpub personal space, {4C EQ o) A. fi
If (! Valobj. Equals (other. valobj ))
B. N, yh9 @ zlf2f0 return false; itpub personal space; R7 | # T + He, lt] x g
Itpub personal space) mxn & | 7d * \ % N l3j
Return true; // at this point, the two objects are equal to itpub's personal space H % _! S] G, V! X |
} Itpub personal space k0v: v m ^ (e8p
}

 

If base. equals will cause the object to be called. equals method, so it should not be called again. this is important. because only when two references point to the same object, the object. the equals method returns true. if the two references do not point to the same object, false is returned. in this way, the equals method we implement will always return false.

Of course, if the defined type is directly inherited from the object, we should implement equals like the first code. if the defined type is not directly inherited from the object, we must first determine whether the base type of the type has overwritten the equals method. if any of the base types overwrites the equals method, we should base it first like this code. equals method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.