名称“ sr”在当前上下文中不存在 [英] The name 'sr' doesn't exist in current context
问题描述
我正在从Microsoft 站点读取示例文件。他们说要这样做:
class Test
{
public static void Main()
{
尝试
{
使用(StreamReader sr = new StreamReader( TestFile.txt)));
{
字符串行= sr.ReadToEnd();
Console.WriteLine(line);
}
}
catch(异常e)
{
Console.WriteLine(无法读取文件:);
Console.WriteLine(e.Message);
}
}
}
但是当我这样做时在Visual C#2010中会给我带来错误:
可能错误的空语句
名称 sr在当前上下文中不存在
我使用$ c $删除了 c>部分,现在代码看起来像这样并且可以正常工作:
try
{
StreamReader sr = new StreamReader( TestFile.txt);
字符串行= sr.ReadToEnd();
Console.WriteLine(line);
}
为什么?
更新: using(....);
这样做;
使用(StreamReader sr = new StreamReader( TestFile.txt ));
{
字符串行= sr.ReadToEnd();
Console.WriteLine(line);
}
实际上(在语义上)与此相同:
使用(StreamReader sr = new StreamReader( TestFile.txt))
{
//注意
}
{
字符串行= sr.ReadToEnd();
Console.WriteLine(line);
}
第二个块(由第二组花括号创建)与 using
块有关。由于在 using
块中定义的变量仅在该块的作用域内,因此一旦代码到达第二个变量,该变量就不存在(就作用域而言和可访问性而言)
您应该使用 using
语句,因为 StreamReader
实现 IDisposable
。 using
块提供了一种简单,干净的方法,即使在例外的情况下,也可以确保正确清理您的资源。有关 using
块(以及具体是 IDisposable
接口是什么)的更多信息,请参见 IDisposable
标签上的元描述。
I was following example from microsoft site for reading from text file. They say to do it like this:
class Test
{
public static void Main()
{
try
{
using (StreamReader sr = new StreamReader("TestFile.txt"));
{
String line = sr.ReadToEnd();
Console.WriteLine(line);
}
}
catch (Exception e)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}
but when I do it like that in Visual C# 2010 it brings me errors:
Possible mistaken empty statement
The name 'sr' does not exist in current context
I removed the using
part and now the code looks like this and is working:
try
{
StreamReader sr = new StreamReader("TestFile.txt");
string line = sr.ReadToEnd();
Console.WriteLine(line);
}
Why is that?
Update: there was semicolon at the end of using(....);
I'm only adding this answer because the existing ones (while properly upvoted) just tell you what the error is, not WHY it's an error.
Doing this;
using (StreamReader sr = new StreamReader("TestFile.txt"));
{
String line = sr.ReadToEnd();
Console.WriteLine(line);
}
is actually the same (semantically) as doing this:
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
// Note that we're not doing anything in here
}
{
String line = sr.ReadToEnd();
Console.WriteLine(line);
}
The second block (created by the second set of curly braces) doesn't have anything to do with the using
block. Since a variable defined within a using
block is only in scope within that block, it doesn't exist (in terms of being in scope and accessible) once your code reaches the second block.
You should use the using
statement because StreamReader
implements IDisposable
. The using
block provides a simple, clean way to ensure that--even in the case of an exception--your resources are properly cleaned up. For more information on the using
block (and, specifically, what the IDisposable
interface is), see the meta description on the IDisposable
tag.
这篇关于名称“ sr”在当前上下文中不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!