Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does the IS operator enters an IF statement when false?

I most be missing something here,
see in the image i made of my debug session.

(items[i] is MailItem) is FALSE according the debugger, but still it enters the if statement.
What am I missing here ?
.

enter image description here

For reference, here is the full code of this method

private MailItem GetMailBySubject(DateTime dateReceived, string subject)
{
    MailItem Result = null;

    Microsoft.Office.Interop.Outlook.Application OutlookIns = new Microsoft.Office.Interop.Outlook.Application();
    Microsoft.Office.Interop.Outlook.NameSpace olNamespace = OutlookIns.GetNamespace("MAPI");
    MAPIFolder myInbox = olNamespace.GetDefaultFolder(OlDefaultFolders.olFolderInbox);


    Items items = myInbox.Items;
    int count = items.Count;
    MailItem mail = null;
    int i = 1; //DO NOT START ON 0

    while ((i < count) && (Result == null))
    {
        if (items[i] is MailItem)
        {
            mail = (MailItem)items[i];
            if ((mail.ReceivedTime.ToString("yyyyMMdd hh:mm:ss") == dateReceived.ToString("yyyyMMdd hh:mm:ss")) && (mail.Subject == subject))
            {
                Result = mail;
            }
        }
        i++;
    }

    return Result;
}
like image 896
GuidoG Avatar asked Nov 06 '22 17:11

GuidoG


1 Answers

This SO answer explains a bit about why you are seeing the IF condition getting passed even when the expression inside it is false. Apparently it's an issue with the debugger and multiple threads. Also, it suggests a workaround to prevent this issue by using lock. Hope it helps.

like image 128
akg179 Avatar answered Nov 15 '22 01:11

akg179