我有点困惑.为什么从一方面我变成CS0103(变量不存在)和另一方面CS0136(用自己的话 - 变量已经存在)和交换机中具有相同名称的变量声明?
这有效:
var obj = new object();
switch (obj)
{
case string str:
break;
case object str:
break;
}
Run Code Online (Sandbox Code Playgroud)
在这里,我成为编译错误CS0103 "名称''在当前上下文中不存在":
var obj = new object();
switch (obj)
{
case string str:
break;
}
if (str == null) { } //CS0103
Run Code Online (Sandbox Code Playgroud)
在这里我成为编译错误CS0136:
var obj = new object();
switch (obj)
{
case string str: //<--- CS0136
break;
}
string str = "";
Run Code Online (Sandbox Code Playgroud)
CS0136:命名为"无功"的局部变量不能在此范围内声明,因为它会给予不同的意义"变种",这已经是在"父母或电流/子"范围用来表示别的东西
这里有三条规则:
object str和string str-但你并不需要一个证明这些特定的错误.您不需要模式匹配来演示这一点.这是一个给出CS0103错误的简单方法:
void Method()
{
{
// Scope of str is the nested block.
string str = "";
}
// str isn't in scope here, so you can't refer to it;
// second bullet point
if (str == "") {}
}
Run Code Online (Sandbox Code Playgroud)
这是CS0136的一个例子:
void Method()
{
{
// Can't declare this variable, because the str variable
// declared later is already in scope
string str = "";
}
// Scope of this variable is the whole method
string str = "";
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
193 次 |
| 最近记录: |