[英]How to fix “variable assigned but its value is never used” error when calling public function?
I'm following a beginner C# guide that is teaching how to use public methods. 我正在遵循一个初学者的C#指南,该指南正在教如何使用公共方法。 I understand very little about how to properly use methods/functions, so sorry in advance if this question is obvious.
我对如何正确使用方法/功能了解得很少,如果这个问题很明显,请提前抱歉。 I researched a bunch of questions asking the same thing, but wasn't able to find an answer for this situation.
我研究了很多问题,问了同样的事情,但找不到这种情况的答案。
The program is supposed to take the string text, send it to the CheckDuplicate function, and determine if it contains more than one of the same number. 该程序应该采用字符串文本,将其发送到CheckDuplicate函数,并确定它是否包含多个相同的数字。 If so it should return string result with the value "Duplicate" or "No Duplicate", and then display it on the console.
如果是这样,它将返回字符串结果“ Duplicate”或“ No Duplicate”,然后在控制台上显示它。
Right now references to string return under the CheckDuplicate function have the error "variable assigned but its value is never used" and the program doesn't return "Duplicate" or "No Duplicate" when inputting a string. 现在,在CheckDuplicate函数下引用字符串返回的错误为“已分配变量,但从未使用过其值”,并且在输入字符串时程序不会返回“ Duplicate”或“ No Duplicate”。
class Program
{
public static string result;
static void Main(string[] args)
{
Console.WriteLine("Enter several numbers separated by -");
string text = Console.ReadLine();
if (String.IsNullOrEmpty(text))
{
Console.WriteLine("Empty");
}
else
{
result = CheckDuplicate(text);
}
Console.WriteLine(result);
}
public static string CheckDuplicate(string text)
{
var textArray = text.Split('-');
int[] textArrayNum = textArray.Select(s => int.Parse(s)).ToArray();
if (textArrayNum.Length != textArrayNum.Distinct().Count())
{
string result = "Duplicate";
}
else
{
string result = "No Duplicate";
}
return result;
}
}
You´re cnfusing yourself because you have multiple variables with the same name that overlap in their scope. 您之所以陷入困境,是因为您有多个具有相同名称的变量,它们在它们的范围内重叠。
The most outer-scope is the class-level, where you have a static
field result
. 最外部的作用域是类级别的,在那里您得到一个
static
场result
。
Then you have another result
defined in CheckDuplicate
that don´t have anything to do with the field from above. 然后,您在
CheckDuplicate
中定义了另一个result
,该result
与上面的字段没有任何关系。 More precisely you have three different result
s in that method, two in the different if/else
-statements and one in the outer-scope. 更准确地说,您在该方法中有三个不同的
result
,两个在不同的if/else
语句中,而另一个在外部作用域中。
public static string CheckDuplicate(string text) { var textArray = text.Split('-'); int[] textArrayNum = textArray.Select(s => int.Parse(s)).ToArray(); if (textArrayNum.Length != textArrayNum.Distinct().Count()) { string result = "Duplicate"; // most inner scope, hides the static field } else { // same level as before but completely unrelated to that one, also hides the static field string result = "No Duplicate"; } return result; // references the static field }
Anyway you could easily avoid such confusion by using meaningful names for your variables. 无论如何,您可以通过对变量使用有意义的名称来轻松避免此类混淆。 In particular a field name
result
seems odd as it indicates that your entire class has some kind of a result which is very unlikeley and thus should be replaced by something like IsDuplicate
. 特别是,字段名称
result
似乎很奇怪,因为它表明您的整个类都有某种非常不像结果的结果,因此应使用IsDuplicate
类的东西IsDuplicate
。 A method on the other hand may have a result
. 另一方面,一种方法 可能会产生
result
。 Having said this its usually a good idea to limit the scope of variables as much as possibles. 话虽如此,通常最好是尽可能地限制变量的范围。
In your example however you even can completely omit the static field, as you´re only using the methods return-value in Main
. 但是,在您的示例中,您甚至可以完全省略静态字段,因为您仅使用
Main
return-value方法。 Just use a local variable and print it to the console: 只需使用局部变量并将其打印到控制台即可:
static void Main(string[] args)
{
...
var result = string.IsNullOrEmpty(text) ? "Empty" : CheckDuplicate(text);
Console.WriteLine(result);
}
Making your method returning directly in the if/else-blocks also reduces that kind of error: 使您的方法直接在if / else-blocks中返回也可以减少这种错误:
public static string CheckDuplicate(string text)
{
var textArray = text.Split('-');
int[] textArrayNum = textArray.Select(s => int.Parse(s)).ToArray();
if (textArrayNum.Length != textArrayNum.Distinct().Count())
{
return "Duplicate";
}
else
{
return "No Duplicate";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.