无法让我的函数查看我的字典

Cannot get my functions to see my dictionary

我已经在我的程序开始时声明了一个字典

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        Dictionary<string, int> dictionary = new Dictionary<string, int>();
    }

我有一个函数可以使用发送的字符串填充字典

    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);
    }

但是我的函数没有看到我在开始时创建的字典,而且我不知道如何从函数中 return 字典。我已经尝试将我的字典声明为静态 and/or public 等等,但我只会收到更多错误。

在 class 级别声明您的字典:

 public partial class Form1 : Form
 {
     public Dictionary<string, int> dictionary = new Dictionary<string, int>();

     public Form1()
     {
          InitializeComponent();        
     }
 }