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

  • Home
  • SEARCH
  • 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 323155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:00:18+00:00 2026-05-12T09:00:18+00:00

I seem to be having an odd issue whereby every time I try to

  • 0

I seem to be having an odd issue whereby every time I try to change a value of an item in a collection, it affects all others that contain the same initial values.

An example is below:

public class Product : ICloneable
{
  public int Id { get; set; }
  public string Name { get; set; }
  public int Quantity { get; set; }

  public Product()
  {
    Id = 0;
    Quantity = 0;
  }

  public Clone()
  {
    return (Product)this.MemberwiseClone();
  }
}

...

private static IEnumerable<Product> GetProducts(Product product, int quantity)
{
  for (int i = 0; i < quantity; i++)
  {
    yield return product.Clone();
  }
}

...

IEnumerable<Product> myProducts = Enumerable.Empty<Product>();
Product product1 = new Product() { Id = 0, Name = "Buzz Cola" };
Product product2 = new Product() { Id = 1, Name = "Choco Bites" };

myProducts = myProducts.Concat(GetProducts(product1, 2));
myProducts = myProducts.Concat(GetProducts(product2, 1));

//Now set the quantity of the first product to be 1.
myProducts.ElementAt(0).Quantity = 1;

foreach(Product product in myProducts)
{
  Console.WriteLine(string.Format("Id: {0}  Quantity: {1}", product.Id, product.Quantity));
}

//Output:
//Id: 0  Quantity: 1
//Id: 0  Quantity: 1 //NO!
//Id: 1  Quantity: 0

Any ideas?

Many thanks!

Update I have updated the question to include the Clone() as suggested. The output is still the same however.

  • 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-12T09:00:18+00:00Added an answer on May 12, 2026 at 9:00 am

    You need something like a clone method or a copy constructor.

    public class Product
    {
      public int Id { get; set; }
      public string Name { get; set; }
      public int Quantity { get; set; }
    
      public Product()
      {
          this.Id = 0;
          this.Name = null;
          this.Quantity = 0;
      }
    
      public Product(Product product)
      {
          this.Id = product.id;
          this.Name = product.Name;
          this.Quantity = product.Quantity;
      }
    }
    
    IList<Product> myProducts = new List<Product>();
    
    Product product1 = new Product() { Id = 0, Name = "Buzz Cola" };
    Product product2 = new Product() { Id = 1, Name = "Choco Bites" };
    Product product3 = new Product(product1); // Use copy-constructor.
    
    myProducts.Add(product1);
    myProducts.Add(product2);
    myProducts.Add(product3);
    
    myProducts[0].Quantity = 1;
    

    And now everything should be fine. You can use this together with your cloniung method to produce a large number of clones at once.

    Just to note, this code has still a very bad taste – you are creating different product instances with equal ids. I can just guess, but do you want to build something like a shopping cart with cart items having a quantity and a product? If yes, you should really think about splitting the product class into two classes. And think about the accessibility of your properties aganin.

    public class Product
    {
      public Int32 Id { get; private set; }
      public String Name { get; private set; }
    }
    
    public class ShoppingCartItem
    {
      public Product Product { get; private set; }
      public Int32 Quantity { get; set; }
    }
    
    public class ShoppingCart
    {
      public IList<ShoppingCartItem> Items { get; private set; }
    }
    

    This solves your current problems because there is no longer a need for cloning products.

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

Sidebar

Related Questions

Seem to be having an issue with std::auto_ptr and assignment, such that the object
i'm having an issue with creating a query in oracle which doesnt seem to
I seem to make this mistake every time I set up a new development
I'm having an odd issue with CALayer drawing for the iPhone. I have a
I'm having an odd issue with an assembly that is targeting .NET 3.5. When
I seem to be having some extremely odd cache_money interactions. When I am on
Hey I seem to be having some kind of memory leak issue with my
I'm having a problem with my Seam code and I can't seem to figure
Now I know this issue is over-talked about but I cannot seem to find
I'm having trouble getting my head around algorithm analysis. I seem to be okay

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.