我有代码来计算文件中重复的单词
这是
static void Main()
{
StreamReader f = new StreamReader(@"d:\C#\text.txt");
string s = f.ReadToEnd();
char[] separators = { };//here is symbols
List<string> words = new List<string>(s.Split(separators));
Dictionary<string, int> map = new Dictionary<string, int>();
foreach (string w in words)
if (map.ContainsKey(w)) map[w]++; else map[w] = 1;
foreach (string w in map.Keys)
Console.WriteLine("{0}\t{l}", w.map[w]);
}
但是在这一行Console.WriteLine("{0}\t{l}", w.map[w]);
中我有这个错误。
错误CS1061'string'不包含'map'的定义,并且没有可以找到接受类型'string'的第一个参数的扩展方法'map'
我如何解决此错误?
答案 0 :(得分:1)
如果我得到你想要的东西,这将有所帮助:
List<string> words = new List<string>() { "c#", "html", "web service", "c#", "c#" };
Dictionary<string, int> map = new Dictionary<string, int>();
foreach (string w in words)
{
if (map.ContainsKey(w))
{
map[w] = map[w]+1;
}
else
{
map[w] = 1;
}
}
//to iterate through Dictionary<string, int> use this instead
foreach (KeyValuePair<string, int> entry in map)
{
Console.WriteLine($"{entry.Key}\t{entry.Value}");
}