简体   繁体   English

在当前上下文中不存在名称'sr'

[英]The name 'sr' doesn't exist in current context

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: 但是当我在Visual C#2010中这样做时,它会给我带来错误:

Possible mistaken empty statement 可能是错误的空话

The name 'sr' does not exist in current context 在当前上下文中不存在名称'sr'

I removed the using part and now the code looks like this and is working: 我删除了using部分,现在代码看起来像这样,并且正在工作:

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(....); 更新: using(....);结束时有分号using(....);

What you described is achieved by putting ; 你所描述的是通过推杆实现的; after using statement 使用声明后

using (StreamReader sr = new StreamReader("TestFile.txt"));
{
     String line = sr.ReadToEnd();
     Console.WriteLine(line);
}

Possibly you even didn't notice that and deleted later. 可能你甚至没有注意到并在以后删除。

What's the difference between using(StreamReader) and just StreamReader? 使用(StreamReader)和StreamReader有什么区别?

When you put disposable variable (StreamReader) in using statement it's the same as: 当您将一次性变量(StreamReader)放入using语句时,它与以下内容相同:

StreamReader sr = new StreamReader("TestFile.txt");
try
{
    String line = sr.ReadToEnd();
    Console.WriteLine(line);
}
finally
{
    // this block will be called even if exception occurs
    if (sr != null)
        sr.Dispose(); // same as sr.Close();
}

Also if you declare variable in using block, it will be visible only in using block. 此外,如果您在使用块中声明变量,它将仅在使用块中可见。 Thats why ; 那就是为什么; made your StreamReader non-existing in latter context. 使您的StreamReader在后一种情况下不存在。 If you declare sr before using block, it will be visible later, but stream will be closed. 如果在使用块之前声明sr ,它将在稍后显示,但将关闭流。

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. 第二个块(由第二组花括号创建)与using块没有任何关系。 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. 由于在using块中定义的变量仅在该块的范围内,因此一旦您的代码到达第二个块,它就不存在(就在范围和可访问性方面)。

You should use the using statement because StreamReader implements IDisposable . 您应该使用using语句,因为StreamReader实现了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. using块提供了一种简单,干净的方式,以确保 - 即使在例外情况下 - 您的资源也得到了适当的清理。 For more information on the using block (and, specifically, what the IDisposable interface is), see the meta description on the IDisposable tag . 有关using块的更多信息(具体而言, IDisposable接口是什么),请参阅IDisposable标记上的元描述

change this: 改变这个:

  using (StreamReader sr = new StreamReader("TestFile.txt"));

to this: 对此:

  using (StreamReader sr = new StreamReader("TestFile.txt"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM