Re: C# Fundamentals Part 3: ReferenceEquals question



Willy Denoyette [MVP] <willy.denoyette@xxxxxxxxxx> wrote:
| 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...

Actually, String.Empty is a public static field of type String initialized
to "" by the String .cctor. That means that the first string in an AD
initializes this field to point (a reference) to the interned (process wide)
empty string object.

Apologies, yes. It's still not a type though, so I don't think that it
makes sense to talk about an instance of it :)

| Again, "instances of Sring.Empty" doesn't make sense, as String.Empty
| is a property, not a type.

Sorry I didn't make myself more clear, My point was to show the remark did
not make much sense, that's why I said there is never an 'instance' of an
'Empty' string created by this constructor.

Right.

| Besides, you can certainly get different instances of System.String
| which *are* empty in other ways:
|

Did I say otherwise?

I think I misinterpreted this line:

<quote>
no new instances of String.Empty are ever created,
</quote>

I thought you meant "in general" (given the word "ever") rather than
"with the code above".

| Either way, this constructor means that the C# spec is violated - the
| "new" operator doesn't return a new instance.

Let's say, it violates the specs for this particular case (IMO an
optimization with no added value anyway), IMO it's the C# the spec which is
too strict, string objects are so special it should mention this as an
exceptions.

I'm not sure - as you say, the optimization doesn't provide much value,
and I think it's entirely reasonable that "new" should mean *new*.

My guess is that the CLR is violating its spec as well - from partition
III of the ECMA spec (2nd edition, Dec 2002):

<quote>
The newobj instruction creates a new object or a new instance of a
value type.
</quote>

Now, the C# code generates a newobj instruction, but the CLR is *not*
creating a new object.

I would make more of a fuss about this, but I suspect any code that it
actually breaks is pretty poorly written to start with!

--
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
    ... 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: binary output
    ... int i=i1; ... You generally don't need to initialise a 'string' with "". ... It's default-initialised to "an empty string". ...
    (microsoft.public.vc.language)
  • Re: Exporting excel to text file
    ... filenumber is just a new local variable which is going to hold the system ... >> Thank you so much for your help, the While statement and empty is ... The selection may very large as compared to what is ... >> 'Walk down each row and include it in the string if the cell is not ...
    (microsoft.public.excel.misc)
  • Re: A Logical Model for Lists as Relations
    ... I am aware of the difference between the number zero and an empty list. ... contents of a set - yields a bunch, which also has a comma union ... is a string of length 4. ... Empty strings and empty lists? ...
    (comp.databases.theory)
  • Re: Exporting excel to text file
    ... >> Thank you so much for your help, the While statement and empty is ... The selection may very large as compared to what is ... >> Open Filename For Output As #filenumber ... >> 'Walk down each row and include it in the string if the cell is not ...
    (microsoft.public.excel.misc)