TryFind的语法是否更紧凑?

Tim*_*ley 2 c# f# coding-style

我正在使用Microsoft.FSharp.Core.Collections.FSharpMap,并且经常要编写:

var oo = world.Entity.TryFind(t);
var entity = oo == null ? null : oo.Value;
Run Code Online (Sandbox Code Playgroud)

和类似的.有什么建议更好的风格?

dtb*_*dtb 6

你可以写一个扩展方法:

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

用法:

var entity = world.Entity.TryFind(t).ValueOrDefault();
Run Code Online (Sandbox Code Playgroud)