C# :
Welcome to C#, as it is a very powerfull and typed language you will never have issues with Equals and "==".

Also C# : (image)

Edit : It's actually wrking as expected, but I would';ve guessed that "==" would wotk for basic types

  • 4
    == is defacto shorthand for Object.Equals. The rules for equals are:

    1. Is it the same type (can be invoked by calling .equals)
    2. Is there an implicit operator for the object that can indirectly satisfy the equals signature
  • 1
    Well yes but in the first example, while left and right part are both “int”, “==” invoke Object.Equals. So it does reference comparison (Which rightfully returns false)
    But .Equals (2d example) invoke the “right one” and compares values of int.
    That what surprised me a little.
  • 0

    yep, exactly the same !
  • 0
    It's like I said, == uses Object.Equals
    Your second example is Nullable<int>.Equals, which understands the Nullable abstraction.

  • 2
    If you do weird shit that boxes items, and/or rely on overloadable operators (such as `==`) or methods (such as Equals()), you better know exactly what you are doing.
  • 0
    @SomeNone True.
    A bit of context. I need it for making DataBase MOCK work with a .Find method and table keys may be ints or Guids. Code must handle both.
    Here is the final line of code
    dbSetMock.Setup(set => set.Find(It.IsAny<object[]>())).Returns((object[] input) =>
    if (!string.IsNullOrWhiteSpace(idProperty) && idType != null)
    return elements.SingleOrDefault(x => Convert.ChangeType(x.GetType().InvokeMember(idProperty, System.Reflection.BindingFlags.GetProperty, Type.DefaultBinder, x, null), idType).Equals(Convert.ChangeType(input[0], idType)));
    throw new Exception("Uknowon id type");
  • 0
  • 0
    I may get flamed for this, but I was always happy with C# 1.x. I was an early adopter of .NET, so I built some stuff on the early releases and it was fantastic. This is pre-generics, and some of the more modern enhancements. Fantastic language.

    I wonder how Go will evolve its simplistic structure, given that people have made certain feature requests.
  • 0
    The reflection seems like it's compensation for lack of a consistent Id naming convention. I would usually want to see something like this if I was having to deal with EF:
  • 0
  • 0
    I've never even seen Convert.ChangeType what even??
Add Comment