Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
== 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. -
@NoToJavaScript
It's like I said, == uses Object.Equals
Your second example is Nullable<int>.Equals, which understands the Nullable abstraction.
https://referencesource.microsoft.com/... -
SomeNone7114yIf 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.
-
@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
@highlight
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");
}); -
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. -
@NoToJavaScript
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:
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
rant