c# - Overriding Equals and comparing to string -


I defined a C # class with a string member. For all purposes, a purpose, think of this class as a sub-class of the string (except that it is not allowed). I am using it to represent a certain type string field that matches the specific format (I have simplified it).

  Public square field {Private redone string string m_field; Public area (string init_value) {// Check for syntax for errors if (checkintex (init_value)) {m_field = init_value; } And {leave the new logic exception (); }} Public override string ToString () {return m_field; }}  

Now, I want to compare this class directly to any other string (object or literal). Therefore, I implemented the following in the following:

  Public override baul par (object obj) {if (obj == null) {return false; } Return.m_field == obj.ToString (); } Public override int Hoshcode () {return.m_field.GetHashCode (); } Public Stable Bull Operator == (field x, object y) {if (object) x == null & y == null} {back true; } And if ((object) x == faucet} y == faucet) {return false; } And {return (x.m_field == y.ToString ()); }} Public Stable Bull Operator! = (Field x, object y) {return! (X == y); }  

Now that I am writing a unit test, then I depend on the order that I argue for assurance. IreEqual, I find different results:

  string valid = "some string"; Field target = new field (valid); Decide. First (target, valid); // Esker Ashwell Erequal (valid, goal); // FAILS  

I'm assuming this because in the first emphasis, this area is calling. Ecklace () and the other is making this string call. AQUALS () Clearly I'm coming from the wrong angle, can someone give me some insight?

One other thing, I can not use the structure here (value type) because in my actual case I am defining all this in a base class and inheriting it from it.

It is described as given in the description given in 8: when equals override the general agreement.

equals the method implements an equivalence relationship.

This is a reflexive, symmetrical, recurring, consistent, and any non-blank reference for x, x.equals (null) to false < / Code> You have to back the paradox breakdown is like you.

field class string class is familiar, but aware of the underlying string class field It does not have a difference in one way or another, and it should be removed.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -