2014-07-23 40 views
0

我是C#的新手(根据Visual Studio 2013命令行使用C#5)。微软网站上的以下一些教程后,我遇到一个问题:无法添加元素到Hashtable

using System; 
using System.Collections; 

public class Hashtable { 

    public static void Main() { 
     Hashtable employees = new Hashtable(); 

     employees.Add("111-222-333","Matt"); 
     employees.Add("222-333-444","Steve"); 
     employees.Add("123-432-123","Adam"); 

     if(employees.ContainsKey("111-222-333")) { 
      string empName = (string) employees["111-222-333"]; 
      Console.WriteLine("Employee 111-222-333's name is: " + empName); 
     } 
     else { 
      Console.WriteLine("Employee 111-222-333 is not in the hash table."); 
     } 
    } 
} 

当我尝试编译此,我得到它规定了一个错误:

“添加”,没有扩展方法“添加”接受型 的第一个参数‘哈希表’可以找到(是否缺少using指令或程序 集引用?)

我完全糊涂了。我找不到任何暗示我应该做出与众不同的事情。

+2

不要调用你自己的类'Hashtable'。 –

+1

这是一个简单的修复(愚蠢的举动)。谢谢。 – Matt

+3

考虑使用支持泛型的[Dictionary](http://msdn.microsoft.com/zh-cn/library/xfhwa508(v = vs.110).aspx)。 – user2864740

回答

4

这是因为您的类名称被命名为Hashtable,因此它使用的是C#的bcl Hashtable实现。指定System.Collection.Hashtable以显式使用bcl散列表。