开发者

string interning - should this code only create 10 strings in memory?

Seems to create a lot more. Please advice why or how to intern correctly开发者_高级运维 here.

Thanks

        IList<string> list = new List<string>(10000);

        for (int i = 0; i < 10000; i++)
        {
            for (int k = 0; k < 10; k++)
            {
                list.Add(string.Intern(k.ToString()));
            }
        }

        Console.WriteLine("intern Done");
        Console.ReadLine();


It will create a lot more during the looping process (each iteration will generate a new string, then replace it with the interned value), but your resulting list should only have 10 unique references to the interned string values.

When you're completed, the interning table should contain the 10 strings (for values of k), as well as "intern Done", and any other literal strings in your app.


According to intellisense, the (1000) is just the initial size of the list - probably increases performance (assuming you need all 1000!) to give it a size so that it doesn't have to create a new, single allocation in memory every time an item is added to the collection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜