Re: C# Fundamentals Part 3: ReferenceEquals question
- From: Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
- Date: Sat, 1 Apr 2006 19:09:33 +0100
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
.
- 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
- 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: Any free way to create unreadable exe file yet?
- 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
|