请确认这是一个MSFT错误 [英] Please confirm this is a MSFT bug
问题描述
你好,
在我通知MSFT之前,有人可以确认这实际上是一个编译器错误(它好像b $ b似乎很明显,但我''对C#来说相当新。以下代码
(错误地)生成编译器错误CS0177(在控制离开当前方法之前,必须将out参数''Whatever''
分配给。)
public void SomeFunc(out string Whatever)
{
bool Continue = ShouldWeContinue();
if (继续)
{
Whatever =" Continue";
}
if(b) !继续)
{
无论什么=没有继续;
}
}
Jake,
编译错误是正确的 - 您必须为在离开方法之前''无论''
。您的代码可以很容易地更新来执行此操作
如此:
bool Continue = ShouldWeContinue();
if(继续)
Whatever =继续;
其他
无论什么=没有继续;
希望这会有所帮助。
Dan Manges
Jake Forson写道:你好,>
在我通知MSFT之前,有人可以确认这实际上是一个编译器错误(它看起来非常明显,但我对C#来说相当新)。以下代码
(错误地)生成编译器错误CS0177(在控制离开当前方法之前必须指定out参数''Whatever''
。
public void SomeFunc (out string Whatever)
{
bool Continue = ShouldWeContinue();
if(Continue)
{
Whatever =" Continue";
}
如果(!继续)
{
随便=没有继续;
}
}>
> Jake,
编译错误是正确的 - 在离开方法之前,必须为''Whatever''
分配一个值。您的代码可以很容易地更新来执行此操作
如此:
bool Continue = ShouldWeContinue();
if(继续)
Whatever =" Continue" ;;
Whatever =没有继续;
希望这会有所帮助。
谢谢你反馈。它确实在所有情况下分配它,但这就是为什么
我认为它是一个错误。
正如所写,是的 - 我们可以观察到它它总是会有一个值。
它退出的时间。
但是:如果变量是一个字段,或者变量是用于一个(2.0) )
匿名委托(作为/ capture /变量),那么
(在代码中没有显而易见)另一个线程可以改变值
两次通话之间继续,顺便说一下可能会或可能不会发现
,具体取决于波动性/记忆障碍。鉴于此,对于我来说,编译器遵循保持简单的意义对我来说很有意义。原则,并且
平等地考虑所有三种情况 - 否则你必须记住愚蠢的
规则,了解每种情况何时适用,并做出改变(比如在
anonymous delegate)会突然导致无关的代码无法编译。
这有意义吗?
Marc
Hi there,
Before I notify MSFT, can someone confirm this is in fact a compiler bug (it
seems pretty obvious but I''m fairly new to C#). The following code
(erroneously) generates compiler error CS0177 (The out parameter ''Whatever''
must be assigned to before control leaves the current method).
public void SomeFunc(out string Whatever)
{
bool Continue = ShouldWeContinue();
if (Continue)
{
Whatever = "Continue";
}
if (!Continue)
{
Whatever = "Didn''t continue";
}
}
Jake,
The compiler error is correct - you must assign a value to ''Whatever''
before leaving the method. Your code could easily be updated to do this
as so:
bool Continue = ShouldWeContinue();
if (Continue)
Whatever = "Continue";
else
Whatever = "Didn''t continue";
Hope this helps.
Dan Manges
Jake Forson wrote:Hi there,
Before I notify MSFT, can someone confirm this is in fact a compiler bug (it
seems pretty obvious but I''m fairly new to C#). The following code
(erroneously) generates compiler error CS0177 (The out parameter ''Whatever''
must be assigned to before control leaves the current method).
public void SomeFunc(out string Whatever)
{
bool Continue = ShouldWeContinue();
if (Continue)
{
Whatever = "Continue";
}
if (!Continue)
{
Whatever = "Didn''t continue";
}
}
> Jake,
The compiler error is correct - you must assign a value to ''Whatever''
before leaving the method. Your code could easily be updated to do this
as so:
bool Continue = ShouldWeContinue();
if (Continue)
Whatever = "Continue";
else
Whatever = "Didn''t continue";
Hope this helps.
Thanks for the feedback. It does assign it in all cases however which is why
I consider it a bug.
As written, yes - we can observe that it will always have a value by the
time it exits.
However: if the variable was a field, or if the variable was used in a (2.0)
anonymous delegate (as a /captured/ variable), then it would be possible for
(without it being obvious in the code) another thread to alter the value of
Continue between the two calls, which incidentally may or may-not be spotted
depending on volatility / memory-barriers. Given this, it makes a lot of
sense for me for the compiler to follow the "keep it simple" principle, and
consider all three cases equally - otherwise you would have to remember daft
rules about when each applies, and making a change (like using it in an
anonymous delegate) would suddenly cause unrelated code to not compile.
Does that make sense?
Marc
这篇关于请确认这是一个MSFT错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!