Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8822997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:14:42+00:00 2026-06-14T06:14:42+00:00

I was following example from microsoft site for reading from text file. They say

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:14:43+00:00Added an answer on June 14, 2026 at 6:14 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following code from a Microsoft example: public class EngineMeasurementCollection : Collection<EngineMeasurement> {
I am pretty much following the example on this MSDN page: http://code.msdn.microsoft.com/Imoprt-Data-from-Excel-to-705ecfcd with 1
following some example from Microsoft I created my custom MembershipProvider in my MVC application
I have the following example of reading from a buffered reader: while ((inputLine =
Can anybody help me with retrieving some elements from the following example text: sdfaasdflj
I am looking at the following tutorial from Microsoft. As per this tutorial, In
I've been following Mike Bostock's code from this example to learn how to draw
The Class Library is an Microsoft Studio extension that generates a file from saving
I've have the following code example from Microsoft (http://msdn.microsoft.com/en-us/library/bb534869.aspx): String[] fruits = {apple, banana,
Please consider the following example code (from the lm doc): ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) trt

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.