为什么这个C#代码会抛出错误:使用未分配的局部变量’n’
在MSDN上,此代码发布于
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch我无法理解为什么它会抛出错误:
static void Main() { int n; try { // Do not initialize this variable here. n = 123; } catch { } // Error: Use of unassigned local variable 'n'. Console.Write(n); } 解决方法
Compiler Error CS0165
更多的是,想象一下这种情况 int n; try { throw new Exception(); n = 123; // this code is never reached } catch { } // oh noez!!! bam! // The compiler is trying to be nice to you if(n == 234); 简而言之,计算机说没有 注意:当您在visual studio中遇到编译器错误时,您可以单击错误代码,有时(如果您很幸运)可以为您提供有关错误含义的更简明信息 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |