1

If I have two IEnumerable A and B, where B is a "sub-collection of A":

IEnumerable<Price> A = List of Price;
IEnumerable<Price> B = A.Where(x => x.Status == "N");

Will updating some properties of the Prices in B automatically update those same Prices and their properties in A?

foreach(var price in B)
{
   price.Date = DateTime.Now;
   price.Status = "F";
}

Will those same Prices in A that are in B now have their Date and Status properties updated? If not, what's the best way to update them? I already have an IEqualityComparer overriding the Equals and GetHashCode methods for something else. Is there a quick way to update all Price properties of A with the Price properties from B for "Equal" Prices?

1 Answers1

3

If Price is a class, then the answer is: yes, when you modify an item in either collection it will be modified in the other. Any class derives from object which is a reference type.

However, if Price is actually a struct, then the answer is: No, a value modified in one collection will not affect the other. Structs are value types.

See also: What is the difference between a reference type and value type in c#

MikeH
  • 4,242
  • 1
  • 17
  • 32
  • Thank you! And yes, you are right, "Object" is a class type. I edited the original post and changed "Object" to "Price". – user3121062 Apr 25 '23 at 22:19
  • @user3121062 Great, I updated the answer to reflect your changes. If this answer was helpful, clicking the check mark would be appreciated! – MikeH Apr 26 '23 at 15:30