Re: String Init
From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 02/09/04
- Next message: Mingle: "Strange Offset Between the Coordinations of Two Points in Dual Display"
- Previous message: Magnus Lidbom: "Re: Anders Hejlsberg comment on immutable objects"
- In reply to: Alvin Bruney [MVP]: "String Init"
- Next in thread: Alvin Bruney [MVP]: "Re: String Init"
- Reply: Alvin Bruney [MVP]: "Re: String Init"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 9 Feb 2004 21:30:55 -0000
<"Alvin Bruney [MVP]" <vapor at steaming post office>> wrote:
> Sometime ago, i posted a concern about initializing string variables to null
> v string.empty. I can't find the thread to reply to it so excuse this one.
>
> I just now thought of this. Isn't it dangerous to initialize variables to
> null in a garbage collected environment since garbage collection is
> non-dert...yada yada yada.
>
> Consider this, i have a routine spanning 200 lines of code. at the top of
> the routine i intialize an object to null. I intend to use this variable at
> line 160 for example. At line 10, a garbage collection occurs. Is my code at
> line 160 safe, the object is eligible for garbage collection because it has
> no roots?
What object? There *is* no object, because you've set the variable to
null. Don't forget: the value of a reference-type variable isn't an
object, it's a reference. A reference is either a way of identifying an
object, or null.
> This is theoretical and not apt to occur frequently but, right
> now, i'm thinking this is terrible if it can actually occur. (haven't given
> it much thought just yet)
>
> wouldn't it be best to intialize an object to a root like empty which would
> prevent this situation from occuring?
No - because then you're probably creating an object for no reason.
(Not in the case of String.Empty, but in other cases.)
-- Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
- Next message: Mingle: "Strange Offset Between the Coordinations of Two Points in Dual Display"
- Previous message: Magnus Lidbom: "Re: Anders Hejlsberg comment on immutable objects"
- In reply to: Alvin Bruney [MVP]: "String Init"
- Next in thread: Alvin Bruney [MVP]: "Re: String Init"
- Reply: Alvin Bruney [MVP]: "Re: String Init"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|