Re: C# Fundamentals Part 3: ReferenceEquals question



James Park <fakename@xxxxxxxxxxxxxxx> wrote:
"Jon Skeet [C# MVP]" <skeet@xxxxxxxxx> wrote in message
news:MPG.1e979f13f72548a998d010@xxxxxxxxxxxxxxxxxxxxxxx
Admittedly there's one situation (involving strings) where even a
"new" call doesn't actually end up creating a new object, but I think
it's fair to view that as an anomaly :)

Whaaa? Explain please!

Try this:

using System;

class Test
{
static void Main()
{
string x = new string (new char[]{});
string y = new string (new char[]{});

Console.WriteLine (object.ReferenceEquals (x, y));
}
}

It prints "true" - meaning that x and y are references to the same
object, despite them being the results of "new" expressions.

I believe this is something odd in the string constructor in the .NET
framework, and not a C# thing in itself. It may well give different
results on Mono. Still, I like it as an odd sort of corner case :)

(And no, I have no idea how I discovered it...)

--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.


Loading