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 6031227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:09:08+00:00 2026-05-23T05:09:08+00:00

I am a .NET developer and recently started learning ruby with ruby_koans. Some of

  • 0

I am a .NET developer and recently started learning ruby with ruby_koans. Some of Ruby’s syntaxes are amazing and one of them is the way it handles “Sandwich” code.

The following is ruby sandwich code.

  def file_sandwich(file_name)
    file = open(file_name)
    yield(file)
  ensure
    file.close if file
  end

  def count_lines2(file_name)
    file_sandwich(file_name) do |file|
      count = 0
      while line = file.gets
        count += 1
      end
      count
    end
  end

  def test_counting_lines2
    assert_equal 4, count_lines2("example_file.txt")
  end

I am fascinated that I can get rid of the cumbersome “file open and close code” each time I access a file but cannot think of any C# equivalent code. Maybe, I can use IoC’s dynamic proxy to do the same thing, but is there any way I can do it purely with C#?

Many thanks in advance.

  • 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-05-23T05:09:09+00:00Added an answer on May 23, 2026 at 5:09 am

    You certainly don’t need anything IoC-related here. How about:

    public T ActOnFile<T>(string filename, Func<Stream, T> func)
    {
        using (Stream stream = File.OpenRead(stream))
        {
            return func(stream);
        }
    }
    
    public int CountLines(string filename)
    {
        return ActOnFile(filename, stream =>
        {
            using (StreamReader reader = new StreamReader(stream))
            {
                int count = 0;
                while (reader.ReadLine() != null)
                {
                    count++;
                }
                return count;
            }
        });
    }
    

    In this case it doesn’t help very much, as the using statement already does most of what you want… but the general principle holds. Indeed, that’s how LINQ is so flexible. If you haven’t looked at LINQ yet, I strongly recommend that you do.

    Here’s the act CountLines method I’d use:

    public int CountLines(string filename)
    {
        return File.ReadLines(filename).Count();
    }
    

    Note that this will still only read a line at a time… but the Count extension method acts on the returned sequence.

    In .NET 3.5 it would be:

    public int CountLines(string filename)
    {
        using (var reader = File.OpenText(filename))
        {
            int count = 0;
            while (reader.ReadLine() != null)
            {
                count++;
            }
            return count;
        }
    }
    

    … still pretty simple.

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

Sidebar

Related Questions

I've been a web developer for some time now, and have recently started learning
I'm being a ASP.NET developer over years but recently I started to work with
A developer buddy of mine just recently started at a new company and one
When I started to develop with .NET under VS2008 recently, I was very happy
I'm a pretty new C# and .NET developer. I recently created an MMC snapin
I have recently started applying for .Net jobs. I currently work in a sales
I am an experienced C# / .NET developer and recently been offered an opportunity
I am an asp.net web developer. Recently I have recieved a job offer to
I had some very wrong sounding advice recently from a senior developer/coworker regarding the
I recently started working on a small CMS. I usually develop .NET applications in

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.