Jul*_*ell 0 c# dictionary function visual-studio-2012
我已经在我的程序开始时声明了一本词典
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Dictionary<string, int> dictionary = new Dictionary<string, int>();
}
Run Code Online (Sandbox Code Playgroud)
我有一个函数,使用它发送的字符串填充字典
public IDictionary<string, int> SortTextIntoDictionary(string text)
{
text = text.Replace(",", ""); //Just cleaning up a bit
text = text.Replace(".", ""); //Just cleaning up a bit
text = text.Replace(Environment.NewLine, " ");
string[] arr = text.Split(' '); //Create an array of words
foreach (string word in arr) //let's loop over the words
{
if (dictionary.ContainsKey(word)) //if it's in the dictionary
dictionary[word] = dictionary[word] + 1; //Increment the count
else
dictionary[word] = 1; //put it in the dictionary with a count 1
}
return(dictionary);
}
Run Code Online (Sandbox Code Playgroud)
但是我的函数没有看到我在开始时创建的字典,我不知道如何从函数返回字典.我试过声明我的字典静态和/或公共等等但我只是得到更多的错误.
在课程级别声明你的字典:
public partial class Form1 : Form
{
public Dictionary<string, int> dictionary = new Dictionary<string, int>();
public Form1()
{
InitializeComponent();
}
}
Run Code Online (Sandbox Code Playgroud)