using var 总是被执行吗?
Is the using var always executed?
在我维护的一些代码中,我遇到了这个:
int Flag;
using (StreamReader reader = new StreamReader(FileName, Encoding.GetEncoding("iso-8859-1"), true))
{
Flag = 1;
// Some computing code
}
if(Flag == 1)
{
// Some other code
}
据我了解,如果执行 using
部分,这是一种执行其他指令的方法。但是 using
是否有可能不被执行(除非引发异常)?或者这是完全无用的代码?
基于using Statement documentation,您可以将您的代码翻译成
int flag;
{
StreamReader reader = new StreamReader(FileName, Encoding.GetEncoding("iso-8859-1"), true);
try
{
flag = 1;
// Some computing code
}
finally
{
if (reader != null) ((IDisposable)reader).Dispose();
}
}
if (flag == 1)
{
// Some other code
}
如果您达到 flag == 1
测试,这意味着您的代码没有抛出,因此,标志设置为 1
。所以,是的,flag
东西在你的情况下是完全无用的代码。
那个代码没用...
如果添加 try
...catch
它可能有某种意义...您想知道 if/where 发生异常,例如:
int flag = 0;
try
{
using (StreamReader reader = new StreamReader(FileName, Encoding.GetEncoding("iso-8859-1"), true))
{
flag = 1;
reader.ReadToEnd();
flag = 2;
}
flag = int.MaxValue;
}
catch (Exception ex)
{
}
if (flag == 0)
{
// Exception on opening
}
else if (flag == 1)
{
// Exception on reading
}
else if (flag == 2)
{
// Exception on closing
}
else if (flag == int.MaxValue)
{
// Everything OK
}
代码总是在using语句中执行,除非创建实例抛出异常。
考虑到这一点。
int Flag;
using (StreamReader reader = new StreamReader(FileName, Encoding.GetEncoding("iso-8859-1"), true))
{
// This scope is executed if the StreamReader instance was created
// If ex. it can't open the file etc. then the scope is not executed
Flag = 1;
}
// Does not run any code past this comment
// if the using statement was not successfully executed
// or there was an exception thrown within the using scope
if(Flag == 1)
{
// Some other code
}
但是有一种方法可以确保代码的下一部分得到执行。
使用 try 语句可以确保设置了标志。
这可能不是您想要做的,但根据您的代码,它会确保设置了标志。也许你需要一些其他的逻辑。
int Flag;
try
{
using (StreamReader reader = new StreamReader(FileName, Encoding.GetEncoding("iso-8859-1"), true))
{
// This scope is executed if the StreamReader instance was created
// If ex. it can't open the file etc. then the scope is not executed
Flag = 1;
}
}
catch (Exception e)
{
// Do stuff with the exception
Flag = -1; // Error Flag perhaps ??
}
// Any code after this is still executed
if(Flag == 1)
{
// Some other code
}
在我维护的一些代码中,我遇到了这个:
int Flag;
using (StreamReader reader = new StreamReader(FileName, Encoding.GetEncoding("iso-8859-1"), true))
{
Flag = 1;
// Some computing code
}
if(Flag == 1)
{
// Some other code
}
据我了解,如果执行 using
部分,这是一种执行其他指令的方法。但是 using
是否有可能不被执行(除非引发异常)?或者这是完全无用的代码?
基于using Statement documentation,您可以将您的代码翻译成
int flag;
{
StreamReader reader = new StreamReader(FileName, Encoding.GetEncoding("iso-8859-1"), true);
try
{
flag = 1;
// Some computing code
}
finally
{
if (reader != null) ((IDisposable)reader).Dispose();
}
}
if (flag == 1)
{
// Some other code
}
如果您达到 flag == 1
测试,这意味着您的代码没有抛出,因此,标志设置为 1
。所以,是的,flag
东西在你的情况下是完全无用的代码。
那个代码没用...
如果添加 try
...catch
它可能有某种意义...您想知道 if/where 发生异常,例如:
int flag = 0;
try
{
using (StreamReader reader = new StreamReader(FileName, Encoding.GetEncoding("iso-8859-1"), true))
{
flag = 1;
reader.ReadToEnd();
flag = 2;
}
flag = int.MaxValue;
}
catch (Exception ex)
{
}
if (flag == 0)
{
// Exception on opening
}
else if (flag == 1)
{
// Exception on reading
}
else if (flag == 2)
{
// Exception on closing
}
else if (flag == int.MaxValue)
{
// Everything OK
}
代码总是在using语句中执行,除非创建实例抛出异常。
考虑到这一点。
int Flag;
using (StreamReader reader = new StreamReader(FileName, Encoding.GetEncoding("iso-8859-1"), true))
{
// This scope is executed if the StreamReader instance was created
// If ex. it can't open the file etc. then the scope is not executed
Flag = 1;
}
// Does not run any code past this comment
// if the using statement was not successfully executed
// or there was an exception thrown within the using scope
if(Flag == 1)
{
// Some other code
}
但是有一种方法可以确保代码的下一部分得到执行。 使用 try 语句可以确保设置了标志。 这可能不是您想要做的,但根据您的代码,它会确保设置了标志。也许你需要一些其他的逻辑。
int Flag;
try
{
using (StreamReader reader = new StreamReader(FileName, Encoding.GetEncoding("iso-8859-1"), true))
{
// This scope is executed if the StreamReader instance was created
// If ex. it can't open the file etc. then the scope is not executed
Flag = 1;
}
}
catch (Exception e)
{
// Do stuff with the exception
Flag = -1; // Error Flag perhaps ??
}
// Any code after this is still executed
if(Flag == 1)
{
// Some other code
}