Trick for young players.
-
Guirec Le Bars wrote:
Wtf? Goddamn Sh*t!
That's almost exactly what I said when I realised what was going on!!!! This was found in the middle of an MVVM framework that was causing me an issue. Good one, eh?
MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')
-
Look at the prog below - a collection has one(object)bool in it. And I want to find out if one of them exists in the collection. Using collection.Contains() would seem like a good idea. But isn't!
class Program
{
static void Main(string[] args)
{
object arg1 = true;
object arg2 = true;List<object> collection = new List<object>() { arg1 }; bool first = (collection.Contains(arg1)); bool second = (collection.Contains(arg2)); Console.WriteLine(string.Format("{0} {1}", first, second)); first = false; second = false; for (int i = 0; i < collection.Count; i++) { if (collection\[i\] == arg1) { first = true; } if (collection\[i\] == arg2) { second = true; } } Console.WriteLine(string.Format("{0} {1}", first, second)); Console.ReadKey(); } }
MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')
Not sure what is confusing you, the implementation of Contains obviously uses the Equals object override, so sucessfully finds arg1 and arg2. However, your list is a list of object (rather than a list of bool) so when it comes to using the equality operator instead of Equals, then you're comparing object references (not values!) and clearly arg1 is not arg2.
-
Wtf? Goddamn Sh*t! I would not expect that either.... I am a young player!!
Seulement, dans certains cas, n'est-ce pas, on n'entend guère que ce qu'on désire entendre et ce qui vous arrange le mieux... [^]
== resolves to System.Object.ReferenceEquals while contains determines equality by using the default equality comparer, as defined by the object's implementation.
People say nothing is impossible, but I do nothing every day.
-
Not sure what is confusing you, the implementation of Contains obviously uses the Equals object override, so sucessfully finds arg1 and arg2. However, your list is a list of object (rather than a list of bool) so when it comes to using the equality operator instead of Equals, then you're comparing object references (not values!) and clearly arg1 is not arg2.
I understand the difference and still I make that mistake occasionally. I guess my head isn't screwed on tightly enough.
People say nothing is impossible, but I do nothing every day.
-
Not sure what is confusing you, the implementation of Contains obviously uses the Equals object override, so sucessfully finds arg1 and arg2. However, your list is a list of object (rather than a list of bool) so when it comes to using the equality operator instead of Equals, then you're comparing object references (not values!) and clearly arg1 is not arg2.
J4amieC wrote:
Not sure what is confusing you,
Seriously? Honestly? you can't see the confusion? Object1.equals(object2) == true; When object1 and object2 are different objects that have the same value? Assuming you didn't know what the objects were, are you telling me you honestly can't see a source of confusion?
MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')
-
J4amieC wrote:
Not sure what is confusing you,
Seriously? Honestly? you can't see the confusion? Object1.equals(object2) == true; When object1 and object2 are different objects that have the same value? Assuming you didn't know what the objects were, are you telling me you honestly can't see a source of confusion?
MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')
_Maxxx_ wrote:
Object1.equals(object2) == true;
When object1 and object2 are different objects that have the same value?Exactly, the point is I understand valuetype equality semantics from referencetype equality semantics. I agree they're somewhat confusing to new programmers.
-
_Maxxx_ wrote:
Object1.equals(object2) == true;
When object1 and object2 are different objects that have the same value?Exactly, the point is I understand valuetype equality semantics from referencetype equality semantics. I agree they're somewhat confusing to new programmers.
Except object isn't a valuetype
MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')
-
For sure! your collection is a list of objects and the arg2 object is simply not in that collection...
Seulement, dans certains cas, n'est-ce pas, on n'entend guère que ce qu'on désire entendre et ce qui vous arrange le mieux... [^]
-
Except object isn't a valuetype
MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')
-
== resolves to System.Object.ReferenceEquals while contains determines equality by using the default equality comparer, as defined by the object's implementation.
People say nothing is impossible, but I do nothing every day.
-
== will resolve to ReferenceEquals for reference types, and if there is no == operator override. For a value type (like bool) it will do a content equality.
Quite right. I referred to this specific case which of course wasn't obvious at all. :sigh:
People say nothing is impossible, but I do nothing every day.
-
Except object isn't a valuetype
MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')
you clearly are forgetting about polymorphism :laugh: this is just one of the confusions he can cause... (see the << and >> operators in c++)
I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)
-
:thumbsup:
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
-
If I may be clear, I understand what is happening and why - the fact remains it is an inconsistency. If each object had two booleans, would it act the same? As an'outside observer' the behavior is inconsistent. In the case where this came up, the framework was processing parameters passed to a constructor, and trying to find the best constructor to use based on the parameters. The routine in question failed if two booleans were passed because e second was deemed to be the same parameter as the first when their values were equal. Because this is a framework, and until now nobody had happened to write a constructor with multiple value types of the same type, and subsequently try to use it with those value types having the same value, nobody had noticed the issue. If lit had been integers rather than booleans it would have been even more interesting - as the chances of the values also being equal wold be that much smaller. So, I under stand what is happening, but I still regard this as an issue with the potential for causing larger problems I an application. The fact that the Contains method works inconsistently depending on the contents of objects is the problem. I ask 'does object a contain object b' And I expect the answer to be yes or no - and not 'well, if it's an object containing only a value type, then the collection contains at least one similar Object where the value type has the same value, but if it's a reference type then that instance exists I the collection' This means in principal that I need to know about the objects in any collection beforehand - which especially in a framework environment, I do not.
MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')
-
Look at the prog below - a collection has one(object)bool in it. And I want to find out if one of them exists in the collection. Using collection.Contains() would seem like a good idea. But isn't!
class Program
{
static void Main(string[] args)
{
object arg1 = true;
object arg2 = true;List<object> collection = new List<object>() { arg1 }; bool first = (collection.Contains(arg1)); bool second = (collection.Contains(arg2)); Console.WriteLine(string.Format("{0} {1}", first, second)); first = false; second = false; for (int i = 0; i < collection.Count; i++) { if (collection\[i\] == arg1) { first = true; } if (collection\[i\] == arg2) { second = true; } } Console.WriteLine(string.Format("{0} {1}", first, second)); Console.ReadKey(); } }
MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')
I see the problem... it's my inability to understand why people prefer object oriented languages. I must be too old to play. :(
-
If I may be clear, I understand what is happening and why - the fact remains it is an inconsistency. If each object had two booleans, would it act the same? As an'outside observer' the behavior is inconsistent. In the case where this came up, the framework was processing parameters passed to a constructor, and trying to find the best constructor to use based on the parameters. The routine in question failed if two booleans were passed because e second was deemed to be the same parameter as the first when their values were equal. Because this is a framework, and until now nobody had happened to write a constructor with multiple value types of the same type, and subsequently try to use it with those value types having the same value, nobody had noticed the issue. If lit had been integers rather than booleans it would have been even more interesting - as the chances of the values also being equal wold be that much smaller. So, I under stand what is happening, but I still regard this as an issue with the potential for causing larger problems I an application. The fact that the Contains method works inconsistently depending on the contents of objects is the problem. I ask 'does object a contain object b' And I expect the answer to be yes or no - and not 'well, if it's an object containing only a value type, then the collection contains at least one similar Object where the value type has the same value, but if it's a reference type then that instance exists I the collection' This means in principal that I need to know about the objects in any collection beforehand - which especially in a framework environment, I do not.
MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')
Try this:
Sub Main() Dim arg1 As Object = True Dim arg2 As Object = True Dim collection As New List(Of Object)() From { \_ arg1 \_ } Dim first As Boolean = (collection.Contains(arg1)) Dim second As Boolean = (collection.Contains(arg2)) Console.WriteLine(String.Format("{0} {1}", first, second)) first = False second = False For i As Integer = 0 To collection.Count - 1 If collection(i) = arg1 Then first = True End If If collection(i) = arg2 Then second = True End If Next Console.WriteLine(String.Format("{0} {1}", first, second)) Console.ReadKey() End Sub
At least it's consistent. :-D
People say nothing is impossible, but I do nothing every day.
-
I see the problem... it's my inability to understand why people prefer object oriented languages. I must be too old to play. :(
I used to think that some years ago - couldn't see the point; until I started using them - and fell in love!
MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')
-
Look at the prog below - a collection has one(object)bool in it. And I want to find out if one of them exists in the collection. Using collection.Contains() would seem like a good idea. But isn't!
class Program
{
static void Main(string[] args)
{
object arg1 = true;
object arg2 = true;List<object> collection = new List<object>() { arg1 }; bool first = (collection.Contains(arg1)); bool second = (collection.Contains(arg2)); Console.WriteLine(string.Format("{0} {1}", first, second)); first = false; second = false; for (int i = 0; i < collection.Count; i++) { if (collection\[i\] == arg1) { first = true; } if (collection\[i\] == arg2) { second = true; } } Console.WriteLine(string.Format("{0} {1}", first, second)); Console.ReadKey(); } }
MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')
Contains
usesEquals
, and it has to. Firstly, because it can't see an overloaded operator==, secondly because if it somehow could, you could put a double.NaN in a list and never find it again - you'd have a list with one element, butContains
is false for any argument, making it look as though the one element isn't any one value, and thirdly because object.ReferenceEquals would always give false for lists of value types because you'd create new boxed objects all the time. So there's really no good alternative, it has to be this way. -
If I may be clear, I understand what is happening and why - the fact remains it is an inconsistency. If each object had two booleans, would it act the same? As an'outside observer' the behavior is inconsistent. In the case where this came up, the framework was processing parameters passed to a constructor, and trying to find the best constructor to use based on the parameters. The routine in question failed if two booleans were passed because e second was deemed to be the same parameter as the first when their values were equal. Because this is a framework, and until now nobody had happened to write a constructor with multiple value types of the same type, and subsequently try to use it with those value types having the same value, nobody had noticed the issue. If lit had been integers rather than booleans it would have been even more interesting - as the chances of the values also being equal wold be that much smaller. So, I under stand what is happening, but I still regard this as an issue with the potential for causing larger problems I an application. The fact that the Contains method works inconsistently depending on the contents of objects is the problem. I ask 'does object a contain object b' And I expect the answer to be yes or no - and not 'well, if it's an object containing only a value type, then the collection contains at least one similar Object where the value type has the same value, but if it's a reference type then that instance exists I the collection' This means in principal that I need to know about the objects in any collection beforehand - which especially in a framework environment, I do not.
MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')
_Maxxx_ wrote:
If each object had two booleans, would it act the same?
Depends if it's a value type, and whether Equals is overridden.
_Maxxx_ wrote:
The fact that the Contains method works inconsistently depending on the contents of objects is the problem.
It does not behave differently depending on the contents. It depends differently depending on the type. You can think of it like string interning: there is only one false so when you have two of them, they are always equal, both in value terms and in reference terms. I think this is true for all value types; it's certainly true for all basic types.
_Maxxx_ wrote:
And I expect the answer to be yes or no - and not 'well, if it's an object containing only a value type, then the collection contains at least one similar Object where the value type has the same value, but if it's a reference type then that instance exists I the collection'
The answer is yes if there is an object in the collection for which Equals with the one you've passed returns true. That's what equality means, and it's usually much more useful than checking for reference equality. You can override == on custom types to make that check whatever you want. By default the behaviour is to check references for reference types and to check values for value types (or at least base types). Choosing which constructor to use based on the values of parameters, instead of the types of parameters, is a WTF all to itself. Edit: Equals, not ==. I always override both of these, and also !=, if I do either, so I get them mixed up sometimes.
-
I used to think that some years ago - couldn't see the point; until I started using them - and fell in love!
MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')