我在 C# 中对字符串插值做错了什么?
What am I doing wrong with string interpolation in c#?
我在做什么:我正在尝试在一个函数中构建一个字典 (DictionaryBuilder
),从所述字典中提取一个字符串并将变量应用于它另一个函数(QuestionGenerator
)。因此每次调用 QuestionBuilder
时,将返回具有不同内容的相同字符串,而无需重复重新制作相同的字典。
int a;
int b;
string theQuestion;
string theAnswer;
Dictionary<string, string> questionDict = new Dictionary<string, string>();
void DictionaryBuilder()
{
questionDict.Add("0", $"What is {a} x {b} ?");
questionDict.Add("1", $"The sum of {a} x {b} = {a*b}");
}
void QuestionGenerator()
{
Random rnd = new Random();
a = rnd.Next(1, 10);
b = rnd.Next(1, 10);
theQuestion = questionDict["0"];
theAnswer = questionDict["1"];
Console.WriteLine(theQuestion);
Console.WriteLine(theAnswer);
}
当前结果: "What is 0 x 0?" 和 "The sum of 0 and 0 is 0"。我无法获取要应用的新号码。
问题:我如何进行这项工作,以便我可以将字典的创建和变量分开,以便每次 QuestionGenerator
都被称为新的提供相同格式的问题而不需要重复重建字典(我认为这是非常低效的)?
QuestionGenerator
点击按钮调用,生成相同格式的新题
(请注意:实际的字典和计算会更大、更复杂,问题和答案不会在同一个字典中——这只是为了简单起见。)
请注意,字符串是在 DictionaryBuilder()
调用时插入的。字符串不是 "dynamically" 内插的。
当您创建字典时,字符串会使用当时 a 和 b 的初始值计算一次,它们均为 0。
您需要为此创建一个小的单行方法,例如,您可以使用存储 Func<string>
而不是字符串的字典。
您应该将 questionDict 转换为以函数作为值而不是字符串的字典:
Dictionary<string, Func<string>> questionDict = new Dictionary<string, Func<string>>();
void DictionaryBuilder()
{
questionDict.Add("0", () => $"What is {a} x {b} ?");
questionDict.Add("1", () => $"The sum of {a} x {b} = {a*b}");
}
然后通过调用这些函数来设置变量:
theQuestion = questionDict["0"]();
theAnswer = questionDict["1"]();
这允许您在调用函数时捕获事态(a
和 b
的值)。
创建包含字符串模板的字典
Dictionary<string, string> questionDict = new Dictionary<string, string>();
void DictionaryBuilder()
{
questionDict.Add("0", "What is {a} x {b} ?");
questionDict.Add("1", "The sum of {a} x {b} = {a*b}");
}
然后在检索到的字符串上使用 String.Format()
来撰写您的问题。
theQuestion = string.Format(questionDict["0"], a, b);
我在做什么:我正在尝试在一个函数中构建一个字典 (DictionaryBuilder
),从所述字典中提取一个字符串并将变量应用于它另一个函数(QuestionGenerator
)。因此每次调用 QuestionBuilder
时,将返回具有不同内容的相同字符串,而无需重复重新制作相同的字典。
int a;
int b;
string theQuestion;
string theAnswer;
Dictionary<string, string> questionDict = new Dictionary<string, string>();
void DictionaryBuilder()
{
questionDict.Add("0", $"What is {a} x {b} ?");
questionDict.Add("1", $"The sum of {a} x {b} = {a*b}");
}
void QuestionGenerator()
{
Random rnd = new Random();
a = rnd.Next(1, 10);
b = rnd.Next(1, 10);
theQuestion = questionDict["0"];
theAnswer = questionDict["1"];
Console.WriteLine(theQuestion);
Console.WriteLine(theAnswer);
}
当前结果: "What is 0 x 0?" 和 "The sum of 0 and 0 is 0"。我无法获取要应用的新号码。
问题:我如何进行这项工作,以便我可以将字典的创建和变量分开,以便每次 QuestionGenerator
都被称为新的提供相同格式的问题而不需要重复重建字典(我认为这是非常低效的)?
QuestionGenerator
点击按钮调用,生成相同格式的新题
(请注意:实际的字典和计算会更大、更复杂,问题和答案不会在同一个字典中——这只是为了简单起见。)
请注意,字符串是在 DictionaryBuilder()
调用时插入的。字符串不是 "dynamically" 内插的。
当您创建字典时,字符串会使用当时 a 和 b 的初始值计算一次,它们均为 0。
您需要为此创建一个小的单行方法,例如,您可以使用存储 Func<string>
而不是字符串的字典。
您应该将 questionDict 转换为以函数作为值而不是字符串的字典:
Dictionary<string, Func<string>> questionDict = new Dictionary<string, Func<string>>();
void DictionaryBuilder()
{
questionDict.Add("0", () => $"What is {a} x {b} ?");
questionDict.Add("1", () => $"The sum of {a} x {b} = {a*b}");
}
然后通过调用这些函数来设置变量:
theQuestion = questionDict["0"]();
theAnswer = questionDict["1"]();
这允许您在调用函数时捕获事态(a
和 b
的值)。
创建包含字符串模板的字典
Dictionary<string, string> questionDict = new Dictionary<string, string>();
void DictionaryBuilder()
{
questionDict.Add("0", "What is {a} x {b} ?");
questionDict.Add("1", "The sum of {a} x {b} = {a*b}");
}
然后在检索到的字符串上使用 String.Format()
来撰写您的问题。
theQuestion = string.Format(questionDict["0"], a, b);