使用c#

时间:2016-11-25 09:45:09

标签: c# hashtable

我有一个包含以下值的文本文件:

0000000000
0000111222
0000144785

我需要用c#将此文件插入HashTable,这是我到目前为止所做的:

        string[] FileLines = File.ReadAllLines(@"D:TestHash.txt");

        Hashtable hashtable = new Hashtable();

        foreach (string line in FileLines)
        {
            //  dont know what to do here
        }

之后我需要将文本框中的值与哈希表值匹配。我该怎么办?

3 个答案:

答案 0 :(得分:4)

Hashtable是键值对的容器。由于您只有值,而不是键值对,因此您不需要哈希表,需要HashSet

HashSet<string> fileLineSet = new HashSet<string>(FileLines);

检查MSDN如何使用哈希集(包括示例)。

答案 1 :(得分:1)

这会将所有行读入HashSet并检查TextBox的值

HashSet<string> items = new HashSet<string>(File.ReadLines(@"D:\TestHash.txt"));
bool hasValue = items.Contains(TextBox.Text);

答案 2 :(得分:0)

 static void Main(string[] args)
    {
        string[] FileLines = File.ReadAllLines("your text file path");

        Hashtable hashtable = new Hashtable();

        foreach (string line in FileLines)
        {
            if (!hashtable.ContainsKey(line))
            {
                hashtable[line] = line;
            }
        }
        foreach (var item in hashtable.Values)
        {
            //here you can match with your text box values...
            //why you need to insert text file data into hash table really i dont know.from above foreach loop inside only you can match the values.might be you have some requirement for hash table i hope
            string textboxVal = "text1";
            if (item == textboxVal)
            {
                //both are matched.do your logic
            }
            else{
                //not matched.
        }
        }
    }