开发者

Is there a more compact syntax for TryFind?

开发者 https://www.devze.com 2023-03-10 13:40 出处:网络
I am using a using Microsoft.FSha开发者_开发问答rp.Core.Collections.FSharpMap and very often have to write:

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

精彩评论

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