开发者

Is there a more compact syntax for TryFind?

I am using a using Microsoft.FSha开发者_开发问答rp.Core.Collections.FSharpMap and very often have to write:

var oo = world.Entity.TryFind(t);
var entity = oo == null ? null : oo.Value;

And similar. Any suggestions for a better style?


You could write an Extension Method:

public static T ValueOrDefault<T>(this FSharpOption<T> option)
{
    return option == null ? default(T) : option.Value;
}

Usage:

var entity = world.Entity.TryFind(t).ValueOrDefault();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜