Это - ожидаемое поведение C# 4.0 равенства Кортежа?

С TortoiseSVN можно сделать правильное-clic перетаскивание & отбросьте свою папку и затем выберите команду "SVN Export All to here".

15
задан Mike Two 11 October 2009 в 19:23
поделиться

4 ответа

The results you see come from a design compromise, Tuples are now shared between F# and C#. The main point is that all Tuples are indeed implemented as reference types, that was not so obvious.

The decision whether Tuples should do deep or shallow equality checks was moved to two interfaces: IStructuralComparable, IStructuralEquatable. Note that those 2 are now also implemented by the Array class.

14
ответ дан 1 December 2019 в 03:24
поделиться

For Reference Type: == performs an identity comparison, i.e. it will only return true if both references point to the same object. While Equals() method is expected to perform a value comparison, i.e. it will return true if the references point to objects that are equivalent.

For reference types where == has NOT been overloaded, it compares whether two references refer to the same object

6
ответ дан 1 December 2019 в 03:24
поделиться

By default, the operator == tests for reference equality, so Yes the result you are seeing is expected.

See Guidelines for Overriding Equals() and Operator == (C# Programming Guide):

In C#, there are two different kinds of equality: reference equality (also known as identity) and value equality. Value equality is the generally understood meaning of equality: it means that two objects contain the same values. For example, two integers with the value of 2 have value equality. Reference equality means that there are not two objects to compare.

1
ответ дан 1 December 2019 в 03:24
поделиться

By default, == (on a class) means reference equality; i.e. are they the same instance; what object.ReferenceEquals(x,y) would return.

You can provide your own == / != operators to get the expected behaviour - and when you override Equals it is important to override GetHashCode too (otherwise you break usage as a key - Why is it important to override GetHashCode when Equals method is overriden in C#?):

public static bool operator == (NameAndNumber x, NameAndNumber y) {
    if (x == null && y == null) return true;
    if (x == null || y == null) return false;
    return x.Number == y.Number && x.Name == y.Name;
    // or if polymorphism is important: return x.Equals(y);
}
public static bool operator !=(NameAndNumber x, NameAndNumber y) {
    return !(x == y); // lazy but works
}
public override int GetHashCode() {
    return (Name == null ? 0 : Name.GetHashCode()) +
        17 * Number.GetHashCode();
}
1
ответ дан 1 December 2019 в 03:24
поделиться
Другие вопросы по тегам:

Похожие вопросы: