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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:19:57+00:00 2026-05-15T07:19:57+00:00

Besides just using yield for iterators in Ruby, I also use it to pass

  • 0

Besides just using yield for iterators in Ruby, I also use it to pass control briefly back to the caller before resuming control in the called method. What I want to do in C# is similar. In a test class, I want to get a connection instance, create another variable instance that uses that connection, then pass the variable to the calling method so it can be fiddled with. I then want control to return to the called method so that the connection can be disposed. I guess I’m wanting a block/closure like in Ruby. Here’s the general idea:

private static MyThing getThing()
{
    using (var connection = new Connection())
    {
        yield return new MyThing(connection);
    }
}

[TestMethod]
public void MyTest1()
{
    // call getThing(), use yielded MyThing, control returns to getThing()
    // for disposal
}

[TestMethod]
public void MyTest2()
{
    // call getThing(), use yielded MyThing, control returns to getThing()
    // for disposal
}

...

This doesn’t work in C#; ReSharper tells me that the body of getThing cannot be an iterator block because MyThing is not an iterator interface type. That’s definitely true, but I don’t want to iterate through some list. I’m guessing I shouldn’t use yield if I’m not working with iterators. Any idea how I can achieve this block/closure thing in C# so I don’t have to wrap my code in MyTest1, MyTest2, … with the code in getThing()‘s body?

  • 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-15T07:19:58+00:00Added an answer on May 15, 2026 at 7:19 am

    What you want are lambda expressions, something like:

    // not named GetThing because it doesn't return anything
    private static void Thing(Action<MyThing> thing)
    {
        using (var connection = new Connection())
        {
            thing(new MyThing(connection));
        }
    }
    
    // ...
    // you call it like this
    Thing(t=>{
      t.Read();
      t.Sing();
      t.Laugh();
    });
    

    This captures t the same way yield does in Ruby. The C# yield is different, it constructs generators that can be iterated over.

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

Sidebar

Related Questions

I understand that Exceptions are for exceptional cases [a], but besides just being repeated
Besides using script or an iframe, is there any way to get independently operating
I was just wondering what are the pros and cons of using embedded images
Besides open-sourcing your project and legislation, are there ways to prevent, or at least
Besides the dynamic nature of Python (and the syntax), what are some of the
Besides the CODEX what resources do you recommend to help a person new to
Besides the DateTime ctor (new DateTime(year, month, day)) , is there any other way
Besides the syntactic sugar and expressiveness power what are the differences in runtime efficiency.
Besides the sample applications featured in the books Beginning Groovy and Grails and The
1) Besides the negative frequencies, which is the minimum frequency provided by the FFT

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.