The Developer Space

Developer's Cafe

  • Cloud
  • Database
  • Open Source
  • Programming
  • Web Dev
  • Mobile
  • Security
  • QuickRef
  • Home
  • Programming
  • Back to Basics: String type in .NET

Back to Basics: String type in .NET

Shameel Ahmed - C#, CodeProject, Programming
June 14, 2009September 21, 2019 No Comments
0 0
Read Time3 Minute, 28 Second

System.String is one of the few classes in the .NET Framework Base Class Library that is given special treatment by the CLR.  Although it is a reference type (it derives from System.Object and not System.ValueType), it behaves much like a value type at runtime.

String copy and compare operations result in value semantics rather than reference semantics.

Have a look at this code

string str1 = "I am str1";
string str2 = str1;
str1 = "I have changed now!";
System.Console.WriteLine(str2);

This code prints “I am str1”.  It shows that the second line of code resulted in assignment of value and not reference. Does it mean that a new string was created during assignment? No. To make things more confusing, the same string was assigned to both variables.

The CLR optimizes string management by maintaining an internal pool of string values known as an intern pool for each .NET application. If the value being assigned to a string variable is the same as one of the strings already in the intern pool, no new string is created and the variable points to the address of the string in the pool. The compiler is capable of using the intern pool to optimize string initialization and have two string variables pointing to the same String object in memory. This optimization step is done only at compile time and isn’t performed at run time, though, because the search in the pool takes time and can even fail, adding overhead to the application.

//No optimization is done at run time.
string str1 = "ABC";
str1 += "XYZ";
string str2 = "ABCXYZ";
//These two variables point to different String objects.
Console.WriteLine(string.ReferenceEquals(str1, str2)); //False

String management can be optimized by using the String.Intern static method. This method searches a string value in the intern pool and returns a reference to the pool element that contains the value if the value is already in the pool. If the search fails, the string is added to the pool and a reference to it is returned. Notice how you can “manually” optimize the preceding code snippet by using the String.Intern method:

string str1 = "ABC";
str1 += "XYZ";
//Move S1 to the intern pool.
str1 = String.Intern(str1);
//Assign S2 a string that is already in the pool).
string str2 = "ABCXYZ";
//These two variables point to the same String object.
Console.WriteLine(string.ReferenceEquals(str1, str2)); //True

This optimization technique makes sense only if you’re working with long strings that appear in multiple portions of the applications. Using the Intern method, you can help your application produce a smaller memory footprint. You can also use the IsInterned static method to check whether a string is in the intern pool (in which case the string itself is returned) or not (in which case the method returns Nothing):

if (string.IsInterned(str1)) {
    //str1 is already in the intern pool.
} else {
    //str1 is not in the intern pool.
}

Share

Facebook
Twitter
LinkedIn
Email

Post navigation

MultiDropDown v2: A Multiple Selection Dropdown Control for ASP.NET

Related Articles

Cross Region Lambda invocation in AWS

Shameel Ahmed
February 27, 2022February 27, 2022 No Comments

Re:Link for Mac is here!!!

Shameel Ahmed
November 14, 2021November 14, 2021 No Comments
Re:Link Architecture

Re:Link, the Browser Bootstrapper

Shameel Ahmed
July 12, 2021July 19, 2021 No Comments

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%
(Add your review)

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

.NET Architecture ASP.NET AWS Azure Books C# Cloud CodeProject Database Data Security Facade Java Mobile MongoDB MySQL Open Source Patterns PostgreSQL Programming Python Security SQL Server Tools Uncategorized Web Development Windows Phone

Recent Posts

  • Cross Region Lambda invocation in AWS February 27, 2022
  • Re:Link for Mac is here!!! November 14, 2021
  • Re:Link, the Browser Bootstrapper July 12, 2021
  • Migrate MySQL table data to MongoDB collections using Python June 18, 2021
  • Book Review: Dealing with Difficult People (HBR Emotional Intelligence Series) September 14, 2020

Archives

  • February 2022 (1)
  • November 2021 (1)
  • July 2021 (1)
  • June 2021 (1)
  • September 2020 (1)
  • May 2020 (2)
  • April 2020 (1)
  • October 2019 (1)
  • September 2019 (4)
  • July 2019 (2)
  • May 2018 (1)
  • September 2017 (1)
  • April 2017 (1)
  • April 2014 (1)
  • August 2011 (1)
  • June 2009 (1)
Copyright 2020. The Developer Space | Theme: OMag by LilyTurf Themes
  • DbStudio
  • Re:Link
  • shameel.net
  • Privacy Policy
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are as essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
SAVE & ACCEPT