为什么在此代码中的out参数上获得代码分析CA1062? [英] Why do I get Code Analysis CA1062 on an out parameter in this code?
问题描述
我有一个非常简单的代码(从原始代码简化了-所以我知道这不是一个很聪明的代码),当我在Visual Studio 2010中使用代码分析进行编译时,会向我发出警告CA1062:验证公共方法的参数。
I have a very simple code (simplified from the original code - so I know it's not a very clever code) that when I compile in Visual Studio 2010 with Code Analysis gives me warning CA1062: Validate arguments of public methods.
public class Foo
{
protected static void Bar(out int[] x)
{
x = new int[1];
for (int i = 0; i != 1; ++i)
x[i] = 1;
}
}
我得到的警告:
CA1062:Microsoft.Design:在
外部可见方法'Foo.Bar(out
int [])'中,验证局部变量
'(* x)',在使用前从
参数'x'重新分配。
CA1062 : Microsoft.Design : In externally visible method 'Foo.Bar(out int[])', validate local variable '(*x)', which was reassigned from parameter 'x', before using it.
我不明白为什么会收到此警告,如何在不抑制的情况下解决该警告? new
可以返回 null
吗?这是Visual Studio 2010的错误吗?
I don't understand why do I get this warning and how can I resolve it without suppressing it? Can new
return null
? Is this a Visual Studio 2010 bug?
更新
推荐答案
我已在Visual Studio 2010 Premium中使用与给定的代码完全相同的方式复制了此代码,并使用在分析设置中启用了Microsoft All Rules 。
I've reproduced this in Visual Studio 2010 Premium with the code exactly as given and with Microsoft All Rules enabled in the analysis settings.
这似乎是一个错误(请参见此处底部: http://msdn.microsoft.com/en-us/library/ms182182.aspx )。有人抱怨您在使用它之前没有检查 x
是否不为空,但是它在 out
参数上,因此没有要检查的输入值!
It looks like this is a bug (see bottom of here: http://msdn.microsoft.com/en-us/library/ms182182.aspx). It is complainng that you are not checking that x
is not null before using it, but it's on out
parameter so there is no input value to check!
这篇关于为什么在此代码中的out参数上获得代码分析CA1062?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!