Do all the things like ++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatarSign Up
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple APILearn More
== 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
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.
SomeNone840296dIf 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.
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, idType)));
throw new Exception("Uknowon id type");
jasonsalas166296dI 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.
LotsOfCaffeine2654295dI've never even seen Convert.ChangeType what even??