Re: Strings.. Objects or not???

From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 07/09/04


Date: Fri, 9 Jul 2004 14:44:05 +0100

Rigga <s@v.c> wrote:
> Thanks all for your replies but I'm still confused.
>
> So coding "veg" anywhere will only ever create one object?
>
> and assigning that object to the reference variable as
>
> x1 as string = "veg"
>
> points to the same object as
>
> x2 as string = "veg"
>
> "veg" being the object?
>
> actually, it has always been kind of strange hat you do not have to code
>
> x1 as New String to instantiate the object.
>
> So we are saying that string does not behave in the same way as other
> objects?
>
> Anyway, now that I know this I'll have to code around it.. thanks all..

String behaves like other objects for the most part, but string
literals are interned automatically, so that within a single AppDomain,
all references to the same string literal are references to the same
actual string.

One *really* weird bit about string is that if you create a new string
using the char[] constructor, but give it an empty array, it doesn't
actually create a new string at all - it just returns String.Empty.
Very odd indeed.

-- 
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Relevant Pages

  • Re: Ada Interfaces and the Liskov Substitution Principle
    ... Polymorphism and references come hand in hand if you need the ability ... But it would be in vain, because assigning class-wide ... How would you pass an UTF-8 string to GTK+ ...
    (comp.lang.ada)
  • 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)

Quantcast