Re: C# Fundamentals Part 3: ReferenceEquals question



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
.



Relevant Pages

  • Re: C# Fundamentals Part 3: ReferenceEquals question
    ... |> take a look at the remark clausein the Framework reference guide. ... What is an "Empty ... String.Empty is a public static field of type String initialized ... empty string object. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: C# Fundamentals Part 3: ReferenceEquals question
    ... String.Empty is a public static field of type String ... |> 'Empty' string created by this constructor. ... sorry I meant using the String constructors taking the charor char* ... | My guess is that the CLR is violating its spec as well - from partition ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: newbie trouble making array of instances
    ... an Empty instance is initialized." ... string x = string.Empty; ... "" is not reference equal to string.Empty. ... The old instance of str1 was replaced with a whole new instance. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: empty vs null
    ... By definition, any String, including the empty string, compares ... greater than a null reference; ... > So say you have a variable MyString. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Deserialization of Null Elements
    ... absence means it's a null reference and empty means it's an empty ... string during deseiralization. ...
    (microsoft.public.dotnet.xml)