简体   繁体   中英

C# “??” operator - what's wrong?

// EWS Microsoft.Exchange.WebServices.Data.Folder
private Folder _historyFolder;

_historyFolder = GetHistroyFolder(exchangeService) ?? CreateHistortFolder(exchangeService);

public Folder GetHistroyFolder(ExchangeService service)
{
    //if found the folder I want - return it , otherwise returns null
}

public Folder CreateHistortFolder(ExchangeService service)
{
    var folder = new Folder(service);
    folder.DisplayName = _historyFolderName;   // "who cares" :)
    folder.FolderClass = _historyFolderClass; // "IPF.Note"
    folder.Save(WellKnownFolderName.MsgFolderRoot);
    return folder;
}

For some reason _historyFolder is always null , although GetHistroyFolder(exchangeService) does return a folder. Why is that?

UPDATE I

I have updated proof for the unbelievers ! If I separate it to 2 lines (without ??) it's OK! but I still wanna understand why the first approach doesn't work... why down vote? mean ppl..

在此处输入图片说明

UPDATE II

Thanks for the all warm people who down vote the question / vote for "close" this thread.

And Thanks for the true warm people who are trying ...

I used the workaround approach, split it to 2 lines

    _historyFolder = GetHistroyFolder(exchangeService);
    if (_historyFolder == null) _historyFolder = CreateHistortFolder(exchangeService); 

you know what funny? Resharper suggests me to return it to how it was before...

Yea , this is a workaround and not real answer for WHY and WTF ... ( .net 4.5 )

UPDATE III

if GetHistroyFolder(..) is return null ( when foreach doesn't find ... ) , CreateHistoryFolder does return Folder object , and _historyFolder is getting the value

Instead of a field why dont you use a property with a backing field. This doesn't exactly solve the problem but at least this makes it easier to debug.

Folder historyFolder 
{
    get{
        if(_historyFolder != null)
            return _historyFolder;
        _historyFolder = GetHistroyFolder(exchangeService); 
        if(_historyFolder == null)
            _historyFolder = CreateHistortFolder(exchangeService) 
        if(_historyFolder == null)
            throw new NullReferenceException("history folder still null");
        return _historyFolder;
    }
}

There's no reason for _historyFolder to be NULL, if GetHistroyFolder() is returning an object eg

namespace ConsoleApplication1
{
    class Program
    {
        // EWS Microsoft.Exchange.WebServices.Data.Folder
        private static object _historyFolder;

        static void Main(string[] args)
        {
            _historyFolder = GetHistroyFolder(null) ?? CreateHistortFolder(null);

            Console.WriteLine(_historyFolder == null);
        }

        public static object GetHistroyFolder(object service)
        {
            return new object();
            //if found the folder I want - return it , otherwise returns null
        }

        public static object CreateHistortFolder(object service)
        {
            return null;
        }
    }
}

I can only imagine that _historyFolder is being set to NULL after GetHistroyFolder() has been called. Your code looks incomplete, are you running in an ASP.NET or something?

EDIT:

In your code, where you call FindFolders(new FolderView()) do this:

FindFolders(new FolderView()).ToList()

Because FindFolders returns an IEnumerable , I think you should call ToList() to ensure that all the folders are returned in one go, not just yielded.

可能是“ CreateHistoryFolder”,返回null

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM