Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 125741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:12:26+00:00 2026-05-11T05:12:26+00:00

I have heard and read that a string can not be changed (immutable?). That

  • 0

I have heard and read that a string can not be changed (immutable?). That should be correct I guess. But I have also heard that two strings with the same contents share the same memory-space (or what you call it). Is this correct?

And if so, does that mean that if I create a List with thousands of strings, it wouldn’t really take up much space at all if most of those strings were equal to each other?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T05:12:26+00:00Added an answer on May 11, 2026 at 5:12 am

    EDIT: In the answer below I’ve referred to the intern pool as being AppDomain-specific; I’m pretty sure that’s what I’ve observed before, but the MSDN docs for String.Intern suggest that there’s a single intern pool for the whole process, making this even more important.

    Original answer

    (I was going to add this as a comment, but I think it’s an important enough point to need an extra answer…)

    As others have explained, string interning occurs for all string literals, but not on ‘dynamically created’ strings (e.g. those read from a database or file, or built using StringBuilder or String.Format.)

    However, I wouldn’t suggest calling String.Intern to get round the latter point: it will populate the intern pool for the lifetime of your AppDomain. Instead, use a pool which is local to just your usage. Here’s an example of such a pool:

    public class StringPool {     private readonly Dictionary<string,string> contents =         new Dictionary<string,string>();      public string Add(string item)     {         string ret;         if (!contents.TryGetValue(item, out ret))         {             contents[item] = item;             ret = item;         }         return ret;     } } 

    You’d then just use something like:

    string data = pool.Add(ReadItemFromDatabase()); 

    (Note that the pool isn’t thread-safe; normal usage wouldn’t need it to be.)

    This way you can throw away your pool as soon as you no longer need it, rather than having a potentially large number of strings in memory forever. You could also make it smarter, implementing an LRU cache or something if you really wanted to.

    EDIT: Just to clarify why this is better than using String.Intern… suppose you read a bunch of strings from a database or log file, process them, and then move onto another task. If you call String.Intern on those strings, they will never be garbage collected as long as your AppDomain is alive – and possibly not even then. If you load several different log files, you’ll gradually accumulate strings in your intern pool until you either finish or run out of memory. Instead, I’m suggesting a pattern like this:

    void ProcessLogFile(string file) {     StringPool pool = new StringPool();     // Process the log file using strings in the pool } // The pool can now be garbage collected 

    Here you get the benefit of multiple strings in the same file only existing once in memory (or at least, only getting past gen0 once) but you don’t pollute a ‘global’ resource (the intern pool).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is not pretty but it works: rm -R $(ls… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer Yes. Override the base1 and base2 methods in Derived to… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer No, you can't. Unfortunately, UIEvent doesn't expose any public way… May 16, 2026 at 12:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

So I have a problem that I can not figure out. I am writing
Currently i have an application that reads and writes several properties from one or
(Note: I have seen several questions regarding .NET logging frameworks, but haven't seen any
I have an array of a large structure that I am trying to output
From all the articles I've read so far about Mochiweb, I've heard this over
Suppose I develop an application with the logic as a CLI program that can
I am writing a method that's intended to return a dictionary filled with configuration
Right now I have been trying to use Launchpad's API to write a small
I have a localization issue. One of my industrious coworkers has replaced all the
Please some help on how to debug this! On my development machine I can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.