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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:03:20+00:00 2026-05-23T06:03:20+00:00

In a method I yield a custom object which I reuse. later for yielding

  • 0

In a method I yield a custom object which I reuse. later for yielding again.

private IEnumerable<DistributorMonthlyComparisonData> ExtractMonthlyAggregate(String CSVFilepath)
{
    DistributorMonthlyComparisonData unitData = new DistributorMonthlyComparisonData();
    while (!reader.EndOfStream)
        {
            if(something){
                // fill unitData
                }
            else{
                 yield return unitData;
                }
        }
}

In another object in my program I call said method in this way:

List<DistributorMonthlyComparisonData> distribList = new List<DistributorMonthlyComparisonData>();
foreach (DistributorMonthlyComparisonData result in ExtractMonthlyAggregate(percorso))
  {
    distribList.Add(result);
  }

If I don’t create in the yielding method, after each yield, a new object

unitData = new DistributorMonthlyComparisonData();

All I get is a List of identical objects.

I am wondering: is this because unitData is being passed as a reference and thus the List is merely a List of references to the same identical object? Creating a new unitData() every time changes the hash of the unitData object being passed?

Please enlighten me. 🙂

EDIT I am interested in why this is happening.
I used to think that the framework is supposed to do copy-on-write on objects, so basically yielding one object then modifying its properties and then yielding it again should result in two different objects being added to the list.

  • 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-23T06:03:21+00:00Added an answer on May 23, 2026 at 6:03 am

    C# has reference semantics for class types, so you are right about references being returned.

    Sidenote about the rest:

    yield return/yield break will continue the loop you place them in. Therefore, the code before your loop is only executed once before the loop is entered. You would have to new your returned objects inside the loop.

    I.e., you would have to replace

    DistributorMonthlyComparisonData unitData = new DistributorMonthlyComparisonData();
    while (!reader.EndOfStream) {
        ...
    

    with

    while (!reader.EndOfStream) {
        DistributorMonthlyComparisonData unitData = new DistributorMonthlyComparisonData();
        ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a fairly complex method that yield-returns IEnumerable<string> , but when I inspected
I have the following method: public IEnumerable<object> GetParameters(Context context) { yield return new[] {
I'm having a little trouble with a method in which I use yield return
Following to this post , I want parallelize this method : public IEnumerable<string> GetAllLogs(IEnumerable<IComputer>
I have a class where I want 'each' to yield another custom object, so
The method Thread.yield : Causes the currently executing thread object to temporarily pause and
In javadoc there is said that yield method Causes the currently executing thread object
The Thread.yield() static method (in Java) causes the currently executing thread object to temporarily
i'm using ECLiPSe programming logic system. i want to implement the yield method for
Method chaining is the practice of object methods returning the object itself in order

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.