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 6893111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:37:07+00:00 2026-05-27T06:37:07+00:00

What situation that I need to call string.Intern or string.IsInterned on String value ?

  • 0

What situation that I need to call string.Intern or string.IsInterned on String value ?

I have this method used by grid to group row :

    // get grouping value for DataGrid
    string GroupItemAccessor(object value)
    {
        COA coa = (COA)value;
        return string.IsInterned(coa.GroupName);
    }

If I don’t call string.Intern the result is wrong, this grid should group by value of GroupName.

String.Intern not used

This if string.Intern used (like example code) it work as I expected.

String.Intern used

  • 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. Editorial Team
    Editorial Team
    2026-05-27T06:37:08+00:00Added an answer on May 27, 2026 at 6:37 am

    The reason that you get a different result is that you are not grouping on the string value, you are grouping on the string reference. The strings are not treated as strings when grouping, but as objects, so only the references are compared, not the content.

    When you read the values from the database, each string will be a separate instance, even if they have the same value. The first two strings “AAA” for example will be separate objects, not references to the same object.

    If you can’t make the grid group on the string values, using String.Intern is one way to make the strings the same instances, so that the grouping works anyway.

    However, you might want to use your own method of making the strings the same instances, as interning the strings means that they will never be garbage collected. You can use a class like this:

    public class LocalIntern {
    
      private Dictionary<string, string> _intern = new Dictionary<string, string>();
    
      public string Intern(string value) {
        if (_intern.ContainsKey(value)) {
          return _intern[value];
        } else {
          _intern.Add(value, value);
          return value;
        }
      }
    
    }
    

    When you populate the grid again, you just create a new LocalIntern object, and the strings held in the previous one can be garbage collected.

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

Sidebar

Related Questions

Situation: I have a simple XML document that contains image information. I need to
HI, I am in a situation that I need to display notification when call
I'm running into this situation where I need to parse a String into an
I have a method myMethod(int a, String b) that has been working fine. However,
I have a situation where I need to pass a string with an apostrophe
I am in kind of situation that I need to start with a tab
Situation: I need to make an imap client (using java mail api) that if,
I have a situation that has been partially covered by other answers at SO,
I have a situation that requires redirecting users who are already logged in away
I’m in the situation that I have to design and implement a rather big

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.