在哪里存储两个值以进行进一步比较?

时间:2017-08-25 20:41:43

标签: c# c#-4.0

在DataGridView中单击上一行后,我在此行中获取两个单元格的值:

string id = Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells["Number"].Value);
    string type = Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells["dataGridViewTextBoxColumn46"].Value);

那么,在哪个存储这些值的结构中,要进一步取消它们呢?

结果我需要比较结构中是否存在id, type

我试过了Dictionary<int, int>。但很难检查字典中的值是否如下所示:Dictionary<'id', 'type'>

1 个答案:

答案 0 :(得分:1)

简单的HashSet<Tuple<string, string>>可能会这样做:

  1. HashSet<T>是一组值,为“包含”查询提供O(1)平均查询时间。

  2. Tuple<T1, T2>是一个表示一对值的类,它使用值类型相等的语义,即使用存储在类中的值来实现EqualsGetHashCode,这意味着具有相同成员的两个不同实例将被视为相等(如果您想将它们用作HashSet<T>键,这一点非常重要。

  3. 所以,您只需执行以下操作:

    // somewhere in your method or class
    HashSet<Tuple<string, string>> hashset = new HashSet<Tuple<string, string>>();
    
    // once you get the (id, type) pair:
    hashset.Add(Tuple.Create(id, key));
    
    // to check if the items are in the hashset:
    if (hashset.Contains(Tuple.Create("a", "b")) 
    {
         // do stuff
    }
    
    // to remove the item from the hashset
    hashset.Remove(Tuple.Create("a", "b"));