在处理F#空值 [英] Handling Null Values in F#
问题描述
我需要用F#一些C#代码互操作。空的是,它被赋予,所以我需要检查,如果值为空的可能值。该文档建议使用模式匹配这样:
I need to interop with some C# code with F#. Null is a possible value that it is given so I need to check if the value was null. The docs suggest using pattern matching as such:
match value with
| null -> ...
| _ -> ...
我遇到的问题是原来的代码是在C#中的结构:
The problem I'm having is the original code is structured in C# as:
if ( value != null ) {
...
}
我该怎么做F#中等价?是否有一个空操作模式匹配?有没有办法if语句与检查null?
How do I do the equivalent in F#? Is there a no-op for pattern matching? Is there a way to check for null with an if statement?
推荐答案
如果您不想在空做任何事情的情况下,那么你可以使用的单位值()
:
If you don't want to do anything in the null case, then you can use the unit value ()
:
match value with
| null -> ()
| _ -> // your code here
当然,你也可以做的空检查,就像在C#中,这可能是在这种情况下,更清晰的:
Of course, you could also do the null check just like in C#, which is probably clearer in this case:
if value <> null then
// your code here
这篇关于在处理F#空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!