Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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(....);

like image 833
cikatomo Avatar asked Nov 19 '25 01:11

cikatomo


2 Answers

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?

When you put disposable variable (StreamReader) in using statement it's the same as:

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. If you declare sr before using block, it will be visible later, but stream will be closed.

like image 190
Sergey Berezovskiy Avatar answered Nov 20 '25 15:11

Sergey Berezovskiy


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.

like image 25
Adam Robinson Avatar answered Nov 20 '25 17:11

Adam Robinson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!