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.

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

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.Internis 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:
When you populate the grid again, you just create a new
LocalInternobject, and the strings held in the previous one can be garbage collected.