static void Main() { int[] array1 = { 1, 2, 3 }; int[] array2 = { 1, 2, 3 }; IStructuralEquatable structuralEquator = array1; Console.WriteLine(array1.Equals(array2)); // False Console.WriteLine(structuralEquator.Equals(array2, EqualityComparer <int> .Default)); // True // string arrays string[] a1 = "a b c d e f g".Split(); string[] a2 = "A B C D E F G".Split(); IStructuralEquatable structuralEquator1 = a1; bool areEqual = structuralEquator1.Equals(a2, StringComparer.InvariantCultureIgnoreCase); Console.WriteLine("Arrays of strings are equal:" + areEqual); //tuples var firstTuple = Tuple.Create(1, "aaaaa"); var secondTuple = Tuple.Create(1, "AAAAA"); IStructuralEquatable structuralEquator2 = firstTuple; bool areTuplesEqual = structuralEquator2.Equals(secondTuple, StringComparer.InvariantCultureIgnoreCase); Console.WriteLine("Are tuples equal:" + areTuplesEqual); IStructuralComparable sc1 = firstTuple; int comparisonResult = sc1.CompareTo(secondTuple, StringComparer.InvariantCultureIgnoreCase); Console.WriteLine("Tuples comarison result:" + comparisonResult); //0 }
/// <summary> /// Determines whether the current collection object precedes, occurs in the /// same position as, or follows another object in the sort order. /// </summary> /// <param name="other">The object to compare with the current instance.</param> /// <param name="comparer"> /// An object that compares members of the current collection object with the /// corresponding members of other. /// </param> /// <returns> /// An integer that indicates the relationship of the current collection object /// to other. /// </returns> int IStructuralComparable.CompareTo(object other, IComparer comparer) { var self = this; Array otherArray = other as Array; if (otherArray == null) { var theirs = other as IImmutableArray; if (theirs != null) { otherArray = theirs.Array; if (self.array == null && otherArray == null) { return(0); } else if (self.array == null ^ otherArray == null) { throw new ArgumentException(SR.ArrayInitializedStateNotEqual, nameof(other)); } } } if (otherArray != null) { IStructuralComparable ours = self.array; return(ours.CompareTo(otherArray, comparer)); } throw new ArgumentException(SR.ArrayLengthsNotEqual, nameof(other)); }
public static void Show() { { int[] a1 = { 1, 2, 3 }; int[] a2 = { 1, 2, 3 }; IStructuralEquatable se1 = a1; Console.WriteLine(a1.Equals(a2)); // False Console.WriteLine(se1.Equals(a2, EqualityComparer <int> .Default)); // True } { string[] a1 = "the quick brown fox".Split(); string[] a2 = "THE QUICK BROWN FOX".Split(); IStructuralEquatable se1 = a1; bool isTrue = se1.Equals(a2, StringComparer.InvariantCultureIgnoreCase); } { var t1 = Tuple.Create(1, "foo"); var t2 = Tuple.Create(1, "FOO"); IStructuralEquatable se1 = t1; Console.WriteLine(se1.Equals(t2, StringComparer.InvariantCultureIgnoreCase)); // True IStructuralComparable sc1 = t1; Console.WriteLine(sc1.CompareTo(t2, StringComparer.InvariantCultureIgnoreCase)); // 0 } { var t1 = Tuple.Create(1, "FOO"); var t2 = Tuple.Create(1, "FOO"); Console.WriteLine(t1.Equals(t2)); // True } }
public void IStructuralComparable_NullComparer_ThrowsNullReferenceException() { // This was not fixed in order to be compatible with the full .NET framework and Xamarin. See #13410 IStructuralComparable comparable = (IStructuralComparable)Tuple; Assert.Throws <NullReferenceException>(() => comparable.CompareTo(Tuple, null)); }
public void IStructuralComparable_NullComparer_ThrowsNullReferenceException() { // This was not fixed in order to be compatible with the .NET Framework and Xamarin. // See https://github.com/dotnet/runtime/issues/19265 IStructuralComparable comparable = (IStructuralComparable)Tuple; Assert.Throws <NullReferenceException>(() => comparable.CompareTo(Tuple, null)); }
public static T[] CastAsArray <T>(this IStructuralComparable comparable) { #if NET40PLUS return((T[])comparable); #else return((T[])((ArrayWrapper)comparable).Array); #endif }
public static void Comparable_Some_WithSome_AndNotEqual_ForNotComparable_Throws() { // Arrange var cmp = Comparer <AnyT> .Default; IStructuralComparable some = AnyT.Some; // Act & Assert Assert.ThrowsArgexn(() => some.CompareTo(AnyT.Some, cmp)); }
public static void Comparable_Some_WithSome_AndEqual() { // Arrange var cmp = Comparer <int> .Default; IStructuralComparable one = One; // Act & Assert Assert.Equal(0, one.CompareTo(One, cmp)); }
public static void Comparable_Some_WithNone_ForNotComparable() { // Arrange var cmp = new AnyComparer(); IStructuralComparable some = AnyT.Some; // Act & Assert Assert.Equal(1, some.CompareTo(AnyT.None, cmp)); }
public static void Comparable_None_WithNone() { // Arrange var cmp = new AnyComparer(); IStructuralComparable none = Ø; // Act & Assert Assert.Equal(0, none.CompareTo(Ø, cmp)); }
public static void Comparable_Some_WithNull() { // Arrange var cmp = new AnyComparer(); IStructuralComparable one = One; // Act & Assert Assert.Equal(1, one.CompareTo(null, cmp)); }
public static void Comparable_None_WithSome() { // Arrange var cmp = new AnyComparer(); IStructuralComparable none = Ø; // Act & Assert Assert.Equal(-1, none.CompareTo(One, cmp)); Assert.Equal(-1, none.CompareTo(Two, cmp)); }
public static void Comparable_Some_WithSome_AndEqual_ForNotComparable() { // Arrange var cmp = Comparer <AnyT> .Default; var anyT = AnyT.Value; IStructuralComparable x = Maybe.SomeOrNone(anyT); var y = Maybe.SomeOrNone(anyT); // Act & Assert Assert.Equal(0, x.CompareTo(y, cmp)); }
public static void Comparable_Some_WithNone() { // Arrange var cmp = new AnyComparer(); IStructuralComparable one = One; IStructuralComparable two = Two; // Act & Assert Assert.Equal(1, one.CompareTo(Ø, cmp)); Assert.Equal(1, two.CompareTo(Ø, cmp)); }
public static void Comparable_Some_WithSome_AndNotEqual() { // Arrange var cmp = Comparer <int> .Default; IStructuralComparable one = One; IStructuralComparable two = Two; // Act & Assert Assert.Equal(-1, one.CompareTo(Two, cmp)); Assert.Equal(1, two.CompareTo(One, cmp)); }
public static void Comparable_None_Throws_WithInvalidType() { // Arrange var cmp = new AnyComparer(); IStructuralComparable none = Ø; // Act & Assert // NONGENERIC_MAYBE Assert.ThrowsArgexn("other", () => none.CompareTo(new object(), cmp)); Assert.Equal(0, none.CompareTo(NoText, cmp)); Assert.Equal(-1, none.CompareTo(SomeText, cmp)); }
public static void Comparable_Some_Throws_WithInvalidType() { // Arrange var cmp = new AnyComparer(); IStructuralComparable one = One; // Act & Assert // NONGENERIC_MAYBE Assert.ThrowsArgexn("other", () => one.CompareTo(new object(), cmp)); Assert.Equal(1, one.CompareTo(NoText, cmp)); Assert.Throws <UnexpectedCallException>(() => one.CompareTo(SomeText, cmp)); }
public static Array AsArray(this IStructuralComparable comparable) { #if NET40PLUS return(comparable as Array); #else ArrayWrapper wrapper = comparable as ArrayWrapper; if (wrapper == null) { return(null); } return(wrapper.Array); #endif }
public int Compare(object x, object y) { if (x == null) { return(y != null ? -1 : 0); } if (y == null) { return(1); } IStructuralComparable structuralComparable = x as IStructuralComparable; if (structuralComparable != null) { return(structuralComparable.CompareTo(y, (IComparer)this)); } return(Comparer.Default.Compare(x, y)); }
public int Compare(Object x, Object y) { if (x == null) { return(y == null ? 0 : -1); } if (y == null) { return(1); } IStructuralComparable scX = x as IStructuralComparable; if (scX != null) { return(scX.CompareTo(y, this)); } return(Comparer.Default.Compare(x, y)); }
public int Compare(object x, object y) { if (x == null) { if (y != null) { return(-1); } return(0); } if (y == null) { return(1); } IStructuralComparable comparable = x as IStructuralComparable; if (comparable != null) { return(comparable.CompareTo(y, this)); } return(Comparer.Default.Compare(x, y)); }
// Structural Comparison Helpers private static int StructuralCompare(CodeContext /*!*/ context, IStructuralComparable x, object y) { return(x.CompareTo(y, context.LanguageContext.GetComparer(null, null))); }
public static T[] CastAsArray <T>(this IStructuralComparable comparable) { return((T[])comparable); }