Learning F#, can't find the answer to this... I'd like to handle case where a Nullable parameter (DateTime? in the original c#) is null, but I get the error "Nullable does not have null as a proper value". What is the correct way to do this?
let addIfNotNull(ht:Hashtable, key:string, value:Nullable<DateTime>) =
match value with
| null -> ()
| _ -> ht.Add(key,value)
ht
Run Code Online (Sandbox Code Playgroud)
可以
System.Nullable<'T>通过使用Value属性从对象获得实际值,并且可以System.Nullable<'T>通过调用HasValue方法确定对象是否具有值。
所以代替match你
if value.HasValue then ht.Add(key, value.Value)
Run Code Online (Sandbox Code Playgroud)
你可以用
match Option.ofNullable value with ...
Run Code Online (Sandbox Code Playgroud)
或声明一些活动模式以提供帮助。