小编yam*_*ini的帖子

c#中的Hashset给出了一种奇怪的行为

我在C#中遇到HashSet问题....

这是我的代码:

  List<int> elements = new List<int>();
        for(int i = 0;i< 100000;i++)
        {
            elements.Add(i);
        }
        HashSet<int> c = new HashSet<int>();
        foreach(var ele in elements)
        {
        c.Add(ele);
        }

        Console.WriteLine("Working HashSet " + c.Count);

        var Numbers = new HashSet<int>();
        var mycount = 0;
        using (TextReader reader = File.OpenText(@"myfile.txt"))
        {
            while ((line = reader.ReadLine()) != null)
            {
                mycount++;
                int parsed = int.Parse(line);
                Numbers.Add(parsed);
            }
        }

        Console.WriteLine("my counter took" + mycount);

        Console.WriteLine("Bad HashSet" + Numbers.Count);
Run Code Online (Sandbox Code Playgroud)

工作HashSet 100 000

我的柜台拿了50万

Bad HashSet 9999

为什么第二个hashset没有添加50万个项目???? 这对我来说是个谜

c# memory-management hashset while-loop data-structures

0
推荐指数
1
解决办法
187
查看次数