Re: C# Fundamentals Part 3: ReferenceEquals question
- From: Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
- Date: Sat, 1 Apr 2006 16:28:09 +0100
Willy Denoyette [MVP] <willy.denoyette@xxxxxxxxxx> wrote:
| In this case, no new instance is being created :(
Yes, but all String(Char[]) or String(Char*) constructors are special cases,
take a look at the remark clause(s) in the Framework reference guide.
...
If value is a null reference (Nothing in Visual Basic) or contains no
element, an Empty instance is initialized.
...
I don't think that documentation even makes sense. What is an "Empty
instance" when Empty is a property? It should probably be worded as "a
new object is not created; instead, the value of String.Empty is
returned". Even that's not perfect as constructors initialise an
instance rather than returning it, but it makes more sense than what's
there, IMO...
On the current version of the CLR, it means that:
string x = String.Empty;
string y = new string (new char[]{});
x and y are references to the same string 'object', but:
- no new instances of String.Empty are ever created, and it's references are
not tracked (like x and y in above)
Again, "instances of Sring.Empty" doesn't make sense, as String.Empty
is a property, not a type.
Besides, you can certainly get different instances of System.String
which *are* empty in other ways:
using System;
using System.Text;
class Test
{
static void Main()
{
string x = new StringBuilder().ToString();
string y = new StringBuilder().ToString();
Console.WriteLine (object.ReferenceEquals(x, y));
}
}
Either way, this constructor means that the C# spec is violated - the
"new" operator doesn't return a new instance.
--
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
.
- Follow-Ups:
- Re: C# Fundamentals Part 3: ReferenceEquals question
- From: Willy Denoyette [MVP]
- Re: C# Fundamentals Part 3: ReferenceEquals question
- References:
- Re: C# Fundamentals Part 3: ReferenceEquals question
- From: Jon Skeet [C# MVP]
- Re: C# Fundamentals Part 3: ReferenceEquals question
- From: Willy Denoyette [MVP]
- Re: C# Fundamentals Part 3: ReferenceEquals question
- From: Jon Skeet [C# MVP]
- Re: C# Fundamentals Part 3: ReferenceEquals question
- From: Willy Denoyette [MVP]
- Re: C# Fundamentals Part 3: ReferenceEquals question
- Prev by Date: Re: adding two or more exes in one??
- Next by Date: Re: implicit cast operator funny-ness
- Previous by thread: Re: C# Fundamentals Part 3: ReferenceEquals question
- Next by thread: Re: C# Fundamentals Part 3: ReferenceEquals question
- Index(es):
Relevant Pages
|