Click here to Skip to main content
Licence CPOL
First Posted 30 Sep 2007
Views 22,052
Bookmarked 8 times

String.Empty Internals

By | 30 Sep 2007 | Article
Technical Analysis of String.Empty and Comparision with "" (empty String)

Introduction

One of the best coding practices suggests using String.Empty rather using "" (empty string) as this will improve code readability and result in better managed execution. Using String.Empty over "" is still debatable for several reasons.

In this article, I tried getting more technical details on how String.Empty works under the hood.

Please note, this article is not trying to propose any recommendations.

The Real Difference

The first thing is, is there any real technical difference between "" and String.Empty? When I checked Shared Source Common Language Infrastructure code files release by Microsoft, here is what I found:

//The Empty constant holds the empty string value.
//We need to call the String constructor so that the compiler doesn't
//mark this as a literal.
//Marking this as a literal would mean that it doesn't show up as a field 
//which we can access from native.

public static readonly String Empty = ""; 

Now this clearly tells us that String.Empty is just an alternative for "". Since Empty public variable is static readonly, it will not create a new string object, instead CLR would use the reference for an existing one which is beneficial with respect to memory management.

Couple of things to notice here. All managed strings are subject to "interning" which manages having only one object in memory which will hold the value "" and for every usage of "" will refer to the same object. Interning happens in the core part of CLR. Thinking on these lines, we may conclude that using "" or String.Empty makes no technical difference. But there are few more things to have a look at.

String.Empty resides in managed assembly mscorlib.dll. If we don't see this DLL referred in our project, we can ensure this by checking the assembly manifest after successfully compiling the project.

Screenshot - image001.jpg

Does CLR load mscorelib.dll every time when an assembly is subject to run? In that case, using String.Empty introduces the burden of taking a value from another assembly. But CLR handles this assembly in a different way.

Before the CLR executes the first line of managed code, it creates three application domains (System Domain, Shared Domain and Default Application Domain). Two of these (System and Shared) are opaque from within the managed code and are not even visible to CLR hosts. They can only be created through the CLR bootstrapping process facilitated by the shim—mscoree.dll and mscorwks.dll. These two are unmanaged assemblies.

The SystemDomain is responsible for creating and initializing the SharedDomain and the default AppDomain. It loads the system library mscorlib.dll into SharedDomain which is accessible to multiple application domains.

The System domain and the Shared domain are singleton and multiple app domains make use of these. Though we see mscorlib in the manifest of every .NET component, it will not get loaded every time (since it is a single shared instance) like our custom (or application specific private) assemblies.

Whereas declaring and using const string or string with "" value will be a part of its own application domain. If we are running multiple .NET applications with their default application domain, we will see multiple (private) copies of string literals in each domain.

History

  • 30th September, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Pavan Gayakwad

Web Developer

India India

Member

Pavan Gayakwad - a Dot Net Programmer with 3+ years of industry experience.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
RantSSCLI only PinmemberUL-Tomten1:22 24 Aug '08  
GeneralString Interning PinmemberAndrew Phillips21:34 28 Apr '08  
The article is based on an incorrect assumption.
 
The CLR does not have to intern strings if the NoStringInterning flag is used. And in fact the C# compiler sets this flag by default. You may find that comparing using ReferenceEquals indicates that they are interned but you cannot rely on it. I believe CLR 2.0 did intern anyway but not if the code was NGEN'd.
 
Andrew Phillips
http://www.hexedit.com
andrew @ hexedit.com

GeneralInteresting topic PinmemberPatrick Sears5:30 30 Sep '07  
GeneralString freezing PinmemberDaniel Grunwald1:49 30 Sep '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120529.1 | Last Updated 30 Sep 2007
Article Copyright 2007 by Pavan Gayakwad
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid