奇怪的警告:“已分配但从未使用过它的值”。 [英] Weird warning: "is assigned but its value is never used"
问题描述
为什么在我的C#代码中会收到以下警告?
已分配专用字段xxxx,但从未使用其值
有问题的属性仅用作用户的标志。实际上,用户可以访问(获取)值 false
或 true
来了解事物的状态。
以下是创建此警告的代码示例:
class Class1 {
//属性
private b_myboolean = false;
//访问者
public bool B_myboolean
{
get {return b_myboolean;}
}
//方法
//方法中的某处
b_myboolean = true;
}
类似的操作将返回警告。为什么?我是否必须以其他方式对此 flag 进行编码?
您可以禁用此特定警告。 / p>
这里有两个候选对象,它们在未使用字段或未使用其值的情况下发出警告:
警告169:从未使用字段'xxx'
警告414 :分配了字段 xxx,但从未使用其值
#pragma warning disable 414
//属性
private b_myboolean = false;
#pragma警告恢复414
我也遇到警告消息。通常,当然不应禁用警告。但是围绕这个领域并不太吸引人。
Why do I get the following warning in my C# code?
the private field xxxx is assigned but its value is never used
The attribute in question is only used as a flag for the user. Indeed the user can access (get) the value false
or true
to know a state of something.
Here is an example of code which creates this warning:
class Class1 {
// Attributes
private b_myboolean = false;
// Accessors
public bool B_myboolean
{
get{ return b_myboolean;}
}
// Methods
// Somewhere in a method
b_myboolean =true;
}
Something like that will return the warning. Why? Do I have to code this flag another way?
You can disable this specific warning.
Here are two candidates causing warnings with fields not used or its value never used:
Warning 169: The field 'xxx' is never used Warning 414: The field 'xxx' is assigned but its value is never used
#pragma warning disable 414
// Attributes
private b_myboolean = false;
#pragma warning restore 414
I also ran into warning messages. In general one should of course not disable warnings. But surrounding just this field is not too dramatic.
这篇关于奇怪的警告:“已分配但从未使用过它的值”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!