- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我不明白为什么下面的代码会产生错误。通常我可以从语言规范中弄明白,但在这种情况下我不理解语言规范。
顺便说一下,这不会导致我的代码出现问题,我只是想了解这门语言。
例子:
bool success;
try
{
success = true;
}
catch
{
success = false;
}
finally
{
Console.WriteLine(success); // ERROR: Local variable 'success' might not be initialized before accessing
}
此行为似乎适用于所有 C# 版本,但下面的引用来自 C# Language Specification 5.0 .
第 5.3.3.14 节 Try-finally 语句
The definite assignment state of v at the beginning of finally-block is the same as the definite assignment state of v at the beginning of stmt.
这里“stmt 的开始”是指整个 try-finally 语句的开始,即在 try
之前。
5.3.3.15 节 Try-catch-finally 语句
The following example demonstrates how the different blocks of a try statement (§8.10) affect definite assignment.
static void F() {
int i, j;
try {
goto LABEL;
// neither i nor j definitely assigned
i = 1;
// i definitely assigned
}
catch {
// neither i nor j definitely assigned
i = 3;
// i definitely assigned
}
finally {
// neither i nor j definitely assigned
j = 5;
// j definitely assigned
}
// i and j definitely assigned
LABEL:;
// j definitely assigned
}
谁能解释为什么 success
(在我的示例中)或 i
(在语言规范示例中)没有在 finally block 的开头明确分配?
最佳答案
原因很简单 - 无法保证 try
或 catch
block 中的代码会在 finally
block 之前执行。
ThreadAbort
异常可能发生在 try
block 内,但在赋值执行之前。
运行时代码在抛出异常之后但在 catch
block 中的代码执行之前执行(搜索异常处理在 .Net 中的工作方式或“结构化异常处理”)。
因此,在执行 finally
block 之前,try 和 catch block 中的代码可能永远不会执行。
关于c# - 在 C# 中,为什么在 finally block 的开头没有明确分配变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28755949/
2种参数:尺寸和价格。目前,我只能单击选择/突出显示尺寸列中的一个,也只能单击选择/突出显示价格列中的一个,而不会影响另一个列中的一个。 当我点击尺寸时,会添加一个 URL 参数“#size=4”。单
在css命名约定中,有什么原因,一些object最好以前缀o-和component开头> 以 c- 开头? 我知道 o- 代表 object 而 c- 代表 component,但为什么不呢?难道我们
这就很迷惑了,一下子,下面的代码就不行了。尝试让我的 Android 很好地显示网页已经显示的内容: HttpClient httpclient = new DefaultHttpClient();
我正在将我的网站发布到我无法控制的 IIS 服务器,我想从代码隐藏中了解它的 URL 是否以“http”或“https”开头。 首先,我在本地尝试了这两种解决方案,都返回了正确的值(“http”):
如果我运行: sbin/start-dfs.sh 然后它实际上并没有启动一个名称节点尽管打印: Starting namenodes on [0.0.0.0] 0.0.0.0: starting na
我正在开发一个包含一些数组的模块。现在我的数组包含: $omearray = array ( '#title' = 'title', 0 = array ( 'another array',
对于 PMD,我希望有一个规则来警告我那些以 my 开头的丑陋变量。 这意味着我必须接受所有不以my开头的变量。 所以,我需要一个正则表达式(re),其行为如下: re.match('myVar')
出于某种奇怪的原因,当我尝试使用 URLConnection 获取网页源时,我在输出中得到“null”。有人可以解释一下吗? 我的方法: public String getPageSource()
如何批量检查某个字符串(记录文本文件中的行)是否以特定单词开头? 我知道如何检查句子/行(字符串)中是否存在单词(子字符串),但我如何检查天气是否以这个词开头? 谢谢:) 最佳答案 这可以通过 FIN
我有一个列表,其中包含多个网址和一些字符串,例如#skipsideNav、#content。我正在从这些字符串中分离出 url if link.startswith('/'): local_u
我有以下 html 标记: 我想选择类 bubble bubble_white 和 bubble bubble_black。我正在考虑下面的代码,但它不起作用: $(".bubbl
我有一个用于文件名验证的正则表达式。在这里: /^[0-9a-zA-Z\^\&\'\@\{\}\[\]\,\$\=\!\-\#\(\)\.\%\+\~\_; ]+$/ 如何更改它以检查文件名不是以
我正在构建一个自动填充函数,它接受一个字符串输入并返回一个字符串建议列表。 Sequelize 的 iLike:query返回出现查询字符串的每个字符串。我想支持查询是前缀的字符串。例如当query=
我首先知道这可能是有史以来看起来最糟糕的正则表达式,但这里是。 我有这个正则表达式 (?:http://)?(?:www.)?youtu(?:be)?.(?:[a-z]){2,3}(?:[a-z/?=
尝试读取文件并根据行创建字典,跳过以#符号开头的行 文件示例: param1=val1 # here is comment 我的功能: def readFromFile(name): conf
我的程序正在读取文本文件并根据文本执行操作。但是文本的第一行是有问题的。显然它以“”开头。这弄乱了我的 startsWith() 检查。 为了理解这个问题,我使用了这段代码: System.ou
我的印象是变量名只能以字母和 _ 开头,但是在测试时,我还发现变量名可以以 $ 开头,如下所示: 代码 #include int main() { int myvar=13; int
我试过这个... Dim myMatches As String() = System.Text.RegularExpressions.Regex.Split(postRow.Item("Post")
开头
我正在使用CKEditor,默认情况下在内容的开头添加了。 即使将enterMode设置为,它也只会影响Enter键的作用,并保留开始的。 我遇到的问题是,如果文本以标记开头,它将围绕它包装,并且图像
我有一个List ,其中有五个字符串: abc def ghi jkl mno 我还有另一个字符串“pq”,我需要知道列表中的每个字符串是否都不以“pq”开头-我将如何使用LINQ(.NET 4.0)
我是一名优秀的程序员,十分优秀!