我与新Task Parallel Library,今天的工作去了这种情况:为什么任务没有返回null
此代码不能编译:
internal Task<Guid?> SavePages(string[] pages)
{
return Task.Run(() =>
{
if (pages == null || pages.Length == 0)
return null;
....
除非我明确地返回一个空可空Guid:
internal Task<Guid?> SavePages(string[] pages)
{
return Task.Run(() =>
{
if (pages == null || pages.Length == 0)
return (Guid?)null;
// Check documents path access
为什么这种行为,我是我做错了什么?我的意思是,我得到的代码与第二个选项一起工作,但不知道如果我滥用该库,我的意思是,null始终为空,不是吗?
编译错误:
Cannot convert lambda expression to delegate type 'System.Func' because some of the return types in the block are not implicitly convertible to the delegate return type
http://msdn.microsoft.com/en-us/library/dd460717.aspx
FFR,包括确切的编译器错误将是有益的,因为在这种情况下,它可能会提到一些关于不能推断类型的内容。 –
问题在于null始终为空,因此编译器无法知道您使用空表达式表示哪种类型 –
注意:表示“(Guid?)null”的其他两种方法是“new Guid?()'和'default(Guid?)'。 – svick