Object == Object is safe in C #

In Java, the following code may return false:

IntegerPlus o1 = new IntegerPlus(1000); IntegerPlus o2 = o1; boolean b1 = o1 == o2; boolean b2 = o1.Equals (o2); 

Is this also a problem in C #? Or does C # do == as it will always be true, even if the objects are moved? ( I describe the Java problem in more detail here. )

-2
equality c #
Mar 30 '14 at 18:08
source share
2 answers

No.

In C # /. The network hash is not involved in the default implementation == != Or Equals . If an object is a reference type and is moved by the GC, there is nothing outside this object that will affect the default comparisons.

Edit : unrelated comparison details below.

== and Equals not related to each other (except for the legend).

In C #, you have operator == that the class / structure can overload. The default behavior is to compare links for reference types, compare values ​​for types of system values, and there is no == operator automatically generated for the user "structure". Note that another Equals comparison is not involved in the default definition of operator == .

For predefined value types, the equality operator (==) returns true if the values ​​of its operands are equal, otherwise false. For reference types other than string, == returns true if its two operands refer to the same object.

You can also provide your own Object.Equals for the class. It’s good practice for classes to override == or Equals is to override all comparison methods for continuous operation ( == != , Equals(object other) and GetHashCode , possibly Equals(myType other) ).

The only use of GetHashCode in .Net is hash collections such as Dictionary , HashSet . Check out GetHashCode Rules in C #

An obvious example of user comparison is System.String ( string ) - it is a reference type, but behaves like a normal value type relative to a comparison.

So, back to the original sample:

  • if IntegerPlus struct without the custom "==" / Equals : it does not automatically create == ( new IntegerPlus(42) == new IntegerPluss(42) - syntax error). You get a comparison of the values ​​for all fields for Equals (automatically provided).
  • If IntegerPlus is a class that does not provide custom operator == or Equals , you get a comparative comparison and new IntegerPlus(42) != new IntegerPluss(42) and the same for Equals
  • If IntegerPlus provides only one of == IntegerPlus != , Equals , how the behavior will be determined by the user implementation (probably by an inexplicable external observer)
  • If IntegerPlus is either a value or a reference type and provides a consistent set of all 4 comparison methods, you can get value comparison behavior.
+2
Mar 30 '14 at 18:13
source share

In Java, the == operator was used to ensure security with all combinations of operands that would be compiled, and provided that none of the operands were float or double , it would implement an equivalence relation. With the advent of autoboxing, this is no longer true, because unfortunately, legal comparisons between primitives and their wrapped equivalents are unsafe and extend the == operator in a way that is incompatible with equivalence relations defined by individual types.

While the equivalence relation that Java will check when both operands are reference types (for example, reference equivalence) was not always one of those interested in programmers, the == behavior corresponded to reference types. If the variables x, y and z are any combination of reference types, and the expressions x == y, y == z and x == z are compiled, then in each case when two are true, the third will also be.

In C #, the token == used to represent two different operators: an overloaded equality test and a default equality test. If one of the operands in == defines an overload that is applicable to a specific combination of the provided operands, the token will be interpreted as an overloaded equality operator. Otherwise, if the operands are compatible reference types, this will be interpreted as the standard test operator for setting equality. Since the same token is used for both operators, and they have different semantics, it cannot generally implement a reliable equivalence relation among reference types if none of the considered types overload it to represent anything other than reference equality .

Given the definitions of String s1=123.ToString(); String s2=123.ToString(); Object o = s1; String s1=123.ToString(); String s2=123.ToString(); Object o = s1; comparing s1==s2 will return True (using the overload (string,string) == , which sees that they contain the same characters), and o==s1 will return true (using the default referential equivalence operator to see that they are repeatedly the same object), but o==s2 will return false (using the default referential equivalence operator to see that they are different objects).

If both operands before == are of type Object , it will behave as an equality control test; however, if they belong to other types, then the operator can sometimes behave as a control test of equality, and sometimes as something else.

+1
Apr 16 '14 at 17:47
source share



All Articles