[英]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. 由于某些原因
_historyFolder
始终为null
,尽管GetHistroyFolder(exchangeService)
确实返回了一个文件夹。 Why is that? 这是为什么?
UPDATE I 更新我
I have updated proof for the unbelievers ! 我已经为不信者更新了证明! If I separate it to 2 lines (without ??) it's OK!
如果我将其分成2行(没有??),就可以了! 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...
Resharper建议我将其恢复为以前的样子...
Yea , this is a workaround and not real answer for WHY and WTF ... ( .net 4.5 ) 是的,对于WHY和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 如果GetHistroyFolder(..)返回null(当foreach找不到...时),CreateHistoryFolder确实返回Folder对象,并且_historyFolder正在获取该值
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 我们没有理由对
_historyFolder
为NULL,如果GetHistroyFolder()
将返回一个对象如
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. 我只能想象,
_historyFolder
被设置为NULL后GetHistroyFolder()
被调用。 Your code looks incomplete, are you running in an ASP.NET or something? 您的代码看起来不完整,是否正在ASP.NET中运行?
EDIT: 编辑:
In your code, where you call FindFolders(new FolderView())
do this: 在您的代码中,调用
FindFolders(new FolderView())
是:
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. 因为FindFolders返回一个
IEnumerable
,所以我认为您应该调用ToList()
以确保一次性返回所有文件夹,而不仅仅是屈服。
可能是“ CreateHistoryFolder”,返回null
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.