Re: C# Fundamentals Part 3: ReferenceEquals question




"Jon Skeet [C# MVP]" <skeet@xxxxxxxxx> wrote in message
news:MPG.1e986b7a7f4b4ec998d01c@xxxxxxxxxxxxxxxxxxxxxxx
| Willy Denoyette [MVP] <willy.denoyette@xxxxxxxxxx> wrote:
| > | It prints "true" - meaning that x and y are references to the same
| > | object, despite them being the results of "new" expressions.
| >
| > Both x and y are initialzed to an empty string (== String.Empty), which
by
| > itself is an interned string. The result is that x and y are references
to
| > the same string object.
|
| Yes - but this is the only time it ever happens. You can't create a
| reference to any other interned string using "new" - and it actually
| violates the spec, which says:
|
| <quote>
| The new operator implies creation of an instance of a type
| </quote>
|
| 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.
....

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)
- the object is not GC heap allocated, it's initialized by the CLR and
shared by all AD's in the process,


Willy.


.



Relevant Pages

  • Re: Getting file name from the file path error
    ... I checked one users tools - References and it appears that it ... It is part of a code module in an Excel ... end of statement with the As highlighted after the(stFullName As String). ... Sub GetFileNameAs String ...
    (microsoft.public.excel.programming)
  • Re: accessors
    ... mentioned that returning references from functions makes using ... boundaries, but if i did, "string const& nameconst" is functionally ... the same as "string nameconst", so i don't see why i'd have to change ... anything (but if i did, the change does not really affect client code, ...
    (comp.lang.cpp)
  • Re: Importing Excel
    ... Function LastInStr(strSearched As String, strSought As String) As Integer ... Didn't realize you haven't set references yet. ... > named Microsoft Excel and select the box. ...
    (comp.databases.ms-access)
  • Re: Is garbage collection here yet?
    ... I don't consider tcl to be a fully higher-level language. ... Tcl has so many other cool features and such a clean ... As others have answered, Tcl does do ref-counting GC of its values, which works fine for strings as they can't contain circular references and are stateless/immutable. ... Basically, it's hard to distinguish a reference from any other string, which makes it difficult to know when it is safe to delete something. ...
    (comp.lang.tcl)
  • Suggestions for refactoring unusual tables
    ... id (int, primary key) ... systext (string) ... catalog_id ... type_id (int, references catalog_entry) ...
    (comp.databases)