添加静态方法(扩展方法)
using System;
namespace Test.AspNetCore.Reflecttion
{
class Program
{
static void Main(string[] args)
{
int a = 1;
int? b = 1;
int c = a + TestForInt.toInt(b); //可以用 b.toInt() 替换
int d = a + b.toInt();
Console.WriteLine($"c={c},d={d}");
}
}
///静态扩展
public static class TestForInt
{
public static int toInt(this int? t)
{
return t ?? 0;
}
}
}
结果是一样的: