I'm relatively new to C# and Office automation and recently I found myself trying to obtain a reference to someone's Outlook inbox and sorting the emails by Received time. It wasn't working until I found a solution elsewhere on the web where the Inbox is assigned to a local variable of type Microsoft.Office.Interop.Outlook.Items and then the sort is performed on the local variable and it works. The question, however, is why? I thought that in C# objects are references and when you declare a new Outlook.Inbox reference and then assign it the Items from the user's Inbox, it simply serves as an additional pointer to the actual emails, and DOES NOT actually copy each of the emails to a new collection. So it should be no different than calling Sort on the original reference, right? Obviously I'm wrong, so I'd appreciate an explanation. Thanx!!
using Outlook = Microsoft.Office.Interop.Outlook;
...
Outlook.Folder oInbox = (Outlook.Folder)oApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
oInbox.Items.Sort("[Received]", true); //this doesn't produce expected results
Outlook.Items inboxFolder = (Outlook.Items)oInbox.Items;
inboxFolder.Sort("[Received]", true); //this DOES sort the items!