为什么我的{get; private set} 变量返回一个空字典?
Why is my {get; private set} variable returning an empty Dictionary?
我正在尝试使用{get; private set;} 用于我正在构建的 MorseCode 翻译器。 MorseToText
词典应该是根据我已经定义的 TextToMorse
词典构建的。出于某种原因,我的词典在使用时是空的。
private static Dictionary<char, string> TextToMorse = new Dictionary<char, string>()
{
{'A', ".-" }, {'B', "-..."}, {'C', "-.-."}, {'D', "-.."}, {'E', "."}, {'F', "..-."},
{'G', "--."}, {'H', "...."}, {'I', ".."}, {'J', ".---"}, {'K', "-.-"}, {'L', ".-.."}, {'M', "--"},
{'N', "-."}, {'O', "---"}, {'P', ".--."}, {'Q', "--.-"}, {'R', ".-."}, {'S', "..."}, {'T', "-"},
{'U', "..-"}, {'V', "...-"}, {'W', ".--"}, {'X', "-..-"}, {'Y', "-.--"}, {'Z', "--.." }
};
private static Dictionary<string, char> _MorseToText = new Dictionary<string, char>();
public static Dictionary<string, char> MorseToText
{
get { return _MorseToText; }
private set
{
foreach (KeyValuePair<char, string> pair in TextToMorse)
{
_MorseToText.Add(pair.Value, pair.Key);
}
}
}
...
for (int i = 0; i < splitInput.Length; i++)
{
MorseToText.TryGetValue(splitInput[i], out char value);
output += $"{value} ";
}
你永远不会调用你的 setter,因此你的字典永远不会充满值。为了调用 setter,您必须编写例如MorseToText = new Dictionary<string,char>()
或例如MorseToText = null
在您的某处代码处。请注意,您输入 setter 的值将被丢弃。如果您调用 setter 两次,您的代码将抛出异常,因为您的字典已经包含键。这非常令人困惑,因此我建议使用静态构造函数。也许您还想使用只读字典来公开您的字典:
private static Dictionary<char, string> TextToMorse;
private static Dictionary<string, char> _MorseToText;
public static ReadOnlyDictionary<string,char> MorseToText {get; private set; }
static YourClassName()
{
TextToMorse = new Dictionary<char, string>()
{
{'A', ".-" }, {'B', "-..."}, {'C', "-.-."}, {'D', "-.."}, {'E', "."}, {'F', "..-."},
{'G', "--."}, {'H', "...."}, {'I', ".."}, {'J', ".---"}, {'K', "-.-"}, {'L', ".-.."}, {'M', "--"},
{'N', "-."}, {'O', "---"}, {'P', ".--."}, {'Q', "--.-"}, {'R', ".-."}, {'S', "..."}, {'T', "-"},
{'U', "..-"}, {'V', "...-"}, {'W', ".--"}, {'X', "-..-"}, {'Y', "-.--"}, {'Z', "--.." }
};
_MorseToText = new Dictionary<string,char>();
foreach (KeyValuePair<char, string> pair in TextToMorse)
{
_MorseToText.Add(pair.Value, pair.Key);
}
MorseToText = new ReadOnlyDictionary(_MorseToText);
}
反转 Dictionary
的最简单方法之一是使用 ToDictionary
linq 函数,然后交换 Key
和 Value
:
static Dictionary<char, string> TextToMorse = new Dictionary<char, string>()
{
{'A', ".-" }, {'B', "-..."}, {'C', "-.-."}, {'D', "-.."}, {'E', "."}, {'F', "..-."},
{'G', "--."}, {'H', "...."}, {'I', ".."}, {'J', ".---"}, {'K', "-.-"}, {'L', ".-.."}, {'M', "--"},
{'N', "-."}, {'O', "---"}, {'P', ".--."}, {'Q', "--.-"}, {'R', ".-."}, {'S', "..."}, {'T', "-"},
{'U', "..-"}, {'V', "...-"}, {'W', ".--"}, {'X', "-..-"}, {'Y', "-.--"}, {'Z', "--.." }
};
static Dictionary<string, char> MorseToText = TextToMorse.ToDictionary(x => x.Value, x => x.Key);
目前您使用的setter有误。当您为 MorseToText
赋值时,将调用 set 函数。例如,当您尝试将 MorseToText
设置为 MorseToText = null
等任何实例时,它将 运行 覆盖您使用 setter.[=18 中的代码期望的默认行为=]
我正在尝试使用{get; private set;} 用于我正在构建的 MorseCode 翻译器。 MorseToText
词典应该是根据我已经定义的 TextToMorse
词典构建的。出于某种原因,我的词典在使用时是空的。
private static Dictionary<char, string> TextToMorse = new Dictionary<char, string>()
{
{'A', ".-" }, {'B', "-..."}, {'C', "-.-."}, {'D', "-.."}, {'E', "."}, {'F', "..-."},
{'G', "--."}, {'H', "...."}, {'I', ".."}, {'J', ".---"}, {'K', "-.-"}, {'L', ".-.."}, {'M', "--"},
{'N', "-."}, {'O', "---"}, {'P', ".--."}, {'Q', "--.-"}, {'R', ".-."}, {'S', "..."}, {'T', "-"},
{'U', "..-"}, {'V', "...-"}, {'W', ".--"}, {'X', "-..-"}, {'Y', "-.--"}, {'Z', "--.." }
};
private static Dictionary<string, char> _MorseToText = new Dictionary<string, char>();
public static Dictionary<string, char> MorseToText
{
get { return _MorseToText; }
private set
{
foreach (KeyValuePair<char, string> pair in TextToMorse)
{
_MorseToText.Add(pair.Value, pair.Key);
}
}
}
...
for (int i = 0; i < splitInput.Length; i++)
{
MorseToText.TryGetValue(splitInput[i], out char value);
output += $"{value} ";
}
你永远不会调用你的 setter,因此你的字典永远不会充满值。为了调用 setter,您必须编写例如MorseToText = new Dictionary<string,char>()
或例如MorseToText = null
在您的某处代码处。请注意,您输入 setter 的值将被丢弃。如果您调用 setter 两次,您的代码将抛出异常,因为您的字典已经包含键。这非常令人困惑,因此我建议使用静态构造函数。也许您还想使用只读字典来公开您的字典:
private static Dictionary<char, string> TextToMorse;
private static Dictionary<string, char> _MorseToText;
public static ReadOnlyDictionary<string,char> MorseToText {get; private set; }
static YourClassName()
{
TextToMorse = new Dictionary<char, string>()
{
{'A', ".-" }, {'B', "-..."}, {'C', "-.-."}, {'D', "-.."}, {'E', "."}, {'F', "..-."},
{'G', "--."}, {'H', "...."}, {'I', ".."}, {'J', ".---"}, {'K', "-.-"}, {'L', ".-.."}, {'M', "--"},
{'N', "-."}, {'O', "---"}, {'P', ".--."}, {'Q', "--.-"}, {'R', ".-."}, {'S', "..."}, {'T', "-"},
{'U', "..-"}, {'V', "...-"}, {'W', ".--"}, {'X', "-..-"}, {'Y', "-.--"}, {'Z', "--.." }
};
_MorseToText = new Dictionary<string,char>();
foreach (KeyValuePair<char, string> pair in TextToMorse)
{
_MorseToText.Add(pair.Value, pair.Key);
}
MorseToText = new ReadOnlyDictionary(_MorseToText);
}
反转 Dictionary
的最简单方法之一是使用 ToDictionary
linq 函数,然后交换 Key
和 Value
:
static Dictionary<char, string> TextToMorse = new Dictionary<char, string>()
{
{'A', ".-" }, {'B', "-..."}, {'C', "-.-."}, {'D', "-.."}, {'E', "."}, {'F', "..-."},
{'G', "--."}, {'H', "...."}, {'I', ".."}, {'J', ".---"}, {'K', "-.-"}, {'L', ".-.."}, {'M', "--"},
{'N', "-."}, {'O', "---"}, {'P', ".--."}, {'Q', "--.-"}, {'R', ".-."}, {'S', "..."}, {'T', "-"},
{'U', "..-"}, {'V', "...-"}, {'W', ".--"}, {'X', "-..-"}, {'Y', "-.--"}, {'Z', "--.." }
};
static Dictionary<string, char> MorseToText = TextToMorse.ToDictionary(x => x.Value, x => x.Key);
目前您使用的setter有误。当您为 MorseToText
赋值时,将调用 set 函数。例如,当您尝试将 MorseToText
设置为 MorseToText = null
等任何实例时,它将 运行 覆盖您使用 setter.[=18 中的代码期望的默认行为=]