Re: newbie trouble making array of instances



G.Doten <gdoten@xxxxxxxxx> wrote:
static void Main(string[] args)
{
string x = new string(new char[]{});
string y = new string(new char[]{});
Console.WriteLine(object.ReferenceEquals(x,y));

I believe that these instances are the same because of this comment in
the documentation for the char[] constructor: "If value is a null
reference or contains no element, an Empty instance is initialized." So
your code is the same as doing this:

string x = string.Empty;
string y = string.Empty;

Well, that just says it's documented - it doesn't explain *how* it
happens. Calling a constructor should logically (and even according to
the C# spec) create a new object. You certainly can't create a C# class
which has this behaviour, for instance.

The references didn't wind up equal. Particularly surprising was that
"" is not reference equal to string.Empty. I wasn't really all that
surprised that str1 didn't wind up equal to anything else, but I was
surprised that str2 and str3 weren't equal references.

I believe that whenever you use '""' in a C# program, the compiler emits
a new instance for each of those quote pairs. Since string.Empty is
itself an instance of the string class then string.Empty does not refer
to the same instance as "".

C# guarantees that all uses of the same string constant within an
assembly use the same object.

Strings are immutable so when you did this:

str1 = str1.Remove(0, str1.Length);

The old instance of str1 was replaced with a whole new instance.

Well, the value of str1 was replaced with a reference to a different
instance. The string itself wasn't being replaced.

However String.Remove certainly *could* detect that the result was
going to be an empty string, and return string.Empty instead of
creating a new empty string.

--
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: newbie trouble making array of instances
    ... A new string instance was created by the Removemethod, and the reference to this new instance was assigned to str1. ... Notice there is no verbiage describing special-casing around a string that happens to come back as empty; it says it *always* returns a new string. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: newbie trouble making array of instances
    ... A new string instance was created by the Removemethod, and the reference to this new instance was assigned to str1. ... that's simply the documentation describing what it does. ...
    (microsoft.public.dotnet.languages.csharp)
  • 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: newbie trouble making array of instances
    ... the value of str1 was replaced with a reference to a different instance. ... The string itself wasn't being replaced. ... The reference contained by the variable str1 is being replaced by a new reference, ... The original string "test string" still exists. ...
    (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)

Loading