首页 .Net .NET(C#) Nullable(可空类型)通过扩展方法传委托参数调用方法

.NET(C#) Nullable(可空类型)通过扩展方法传委托参数调用方法

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
{
	>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<R>。如果xnull,它将返回带nullNullable<R>

public int Foo(int a)
{
    // ...
}
// in some other method
int? x = 0;
x = Foo(x);
public static >FuncUtils {
public static Nullable<R> Fmap<T, R>(this Nullable<T> x, Func<T, R> 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<T, R>作为输入,返回一个Func<Nullable<T>, Nullable<R>>,这样我们就可以对特定的x使用它:

public static >FuncUtils {
public static Func<Nullable<T>, Nullable<R>> Fmap<T, R>(Func<T, R> f)
where T : struct
where R : struct {
return delegate (Nullable<T> x) {
if(x != null) {
return f(x.Value);
} else {
return null;
}
};
}
}

调用方法如下:

var fmapf = FuncUtils.Fmap<int, int>(Foo);
fmapf(null);  // -> null
fmapf(12);    // -> Foo(12) as int?
特别声明:本站部分内容收集于互联网是出于更直观传递信息的目的。该内容版权归原作者所有,并不代表本站赞同其观点和对其真实性负责。如该内容涉及任何第三方合法权利,请及时与824310991@qq.com联系,我们会及时反馈并处理完毕。