Does FirstOrDefault return a reference to the item in the collection or the value of the item?
var obj = myCollection.FirstOrDefault(x => x.Param == "match condition");
if (obj != null)
{
obj = newObjectOfCollectionType; //if found, replace with the changed record
}
Will this code replace the object reference in the myCollection with the new object, or will it do nothing to myCollection?
var obj = myCollection.FirstOrDefault(x => x.Param == "match condition");
if (obj != null)
{
obj = newObjectOfCollectionType; --> this will not reflect in the collection
}
var obj = myCollection.FirstOrDefault(x => x.Param == "match condition");
if (obj != null)
{
obj.Property = newValue; --> this will reflect in your object in the original collection
}
It does nothing to the collection. You can change the collection like this:
int index = myCollection.FindIndex(x => x.Param == "match condition");
if (index != -1)
{
myCollection[index] = newObjectOfCollectionType;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With