Using SPListItemCollections indexer to get SPListItem returns different/reset item every time appearing to not save changes
While in a hurry to test some code I discovered that calling the following will not actually save your change:
spListItemCollection[0][“Title”] = “Hello World”;
spListItemCollection[0].Update();
Instead you need to store the SPListItem returned by the first indexer call and then use it to set and update.
var spListItem = spListItemCollection[0];
spListItem[“Title”] = “Hello World”;
spListItem.Update();
This indicates that even though its a property, the indexer is giving you a new or reset instance of the SPListItem everytime you ask for the same index.
Normally I would have refactored this and used a variable from the get go and so I would have never seen this. This just goes to show even the ones that build the framework don’t even follow their own best practices so always assume the worst and store objects in variables first before using them.