News for hackers. Stuff that matters.
Re: Strangest language feature by ufmace on Sunday March 24, 2013 @01:42PM (#5432787)
Found a few interesting bits there. A reference to a Quiz about C# and object equality: http://blogs.msdn.com/b/jmstall/archive/2005/03/06/386064.as...

The last one makes me wonder a bit. Given local int variable x, what does this evaluate to?

(object) x.ToString() == (object) x.ToString()

When I run it, I get false, and the author says that it's false, because we're doing reference comparison of two independently created objects. But I thought strings were interned in .NET, so if you independently create 2 strings with the same contents, then they are actually the same object, which would make this true?

Re: Strangest language feature by kvb on Sunday March 24, 2013 @02:27PM (#5432940)
String constants are interned at compile time. Dynamically created strings are typically not interned.
Re: Strangest language feature by LoneWolf on Sunday March 24, 2013 @05:00PM (#5433558)
I would say that it is because each time ToString() is called a new string is created, and at least in the Java I believe that is what will happen, comparison will be false because they are not the same instance.
Re: Strangest language feature by ufmace on Sunday March 24, 2013 @05:56PM (#5433895)
It's a bit trickier than that due to string interning. That's where there is only one instance framework-wide of a string of a particular value. Try to create a second string with the same value as one you have created already, and you get back a reference to the first string instance.

As an example:

(object)"XYZ" == (object)"XYZ"

returns true. So does:

(object) String.Intern(x.ToString()) == (object) String.Intern(x.ToString())

The trick is to know exactly which strings are interned, and when. I initially thought that all strings were, but it seems that string literals in code are always interned, and dynamically-created strings are mostly not interned automatically, but can be manually.

http://en.wikipedia.org/wiki/String_interning http://msdn.microsoft.com/en-us/library/system.string.intern...