1、可空类型介绍及使用
正常情况下,值类型是不为null
,必须有值,而可空类型可以理解成一种特殊的值类型,可以为null
,比如,int? a=1;a=null;
中a
变量是int类型并且可以为null
。常用的值类型:byte,short,int,long,float,double,decimal,char,bool,enum 和struct。
可空类型的使用示例代码如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{class Program{static void Main(string[] args){int? i = null;if (!i.HasValue) // 不为null,则 i.HasValue 为true{i = 99;}Console.WriteLine(i.Value); // i 的值}}}// i.HasValue 比 i != null 复杂一点,i.Value 也比 i 更麻烦// 但是当使用更加复杂的值类型(struct)来声明可空类型时, .HasValue 和 .Value 就有了优势。
2、可空类型扩展方法传委托参数调用方法
实现可空类型为null
的情况的判断,如果x不为空,它将调用函数Foo
。它将把结果包装回一个Nullable
。如果x
为null
,它将返回带null
的Nullable
。
public int Foo(int a){ // ...}// in some other methodint? x = 0;x = Foo(x);
public static class FuncUtils {
public static NullableFmap (this Nullable x, Func f)
where T : struct
where R : struct {
if(x != null) {
return f(x.Value);
} else {
return null;
}
}
}
调用方法如下:
int? x = 0;x = x.Fmap(Foo);
或者可以写一个更等价的函数(如Haskell中的fmap
),其中我们有一个函数fmap
,它接受Func
作为输入,返回一个Func
,这样我们就可以对特定的x
使用它:
public static class FuncUtils {
public static Func, Nullable > Fmap (Func f)
where T : struct
where R : struct {
return delegate (Nullablex) {
if(x != null) {
return f(x.Value);
} else {
return null;
}
};
}
}
调用方法如下:
var fmapf = FuncUtils.Fmap(Foo);fmapf(null); // -> nullfmapf(12); // -> Foo(12) as int?