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

1、可空类型介绍及使用

正常情况下,值类型是不为null,必须有值,而可空类型可以理解成一种特殊的值类型,可以为null,比如,int? a=1;a=null;a变量是int类型并且可以为null。常用的值类型:byte,short,int,long,float,double,decimal,char,bool,enumstruct

可空类型的使用示例代码如下:

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

public int Foo(int a)
{
    // ...
}
// in some other method
int? x = 0;
x = Foo(x);
public static class 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 class 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?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值