引用摘自:implicit(C# 参考)

仍以Student求和举例

    class Student
{
/// <summary>
/// 语文成绩
/// </summary>
public double Chinese { get; set; } /// <summary>
/// 数学成绩
/// </summary>
public double Math { get; set; }
}

不使用implicit 求和

    class Program
{
static void Main(string[] args)
{
var a = new Student
{
Chinese = 90.5d,
Math = 88.5d
}; //a的总成绩 语文和数据的总分数
Console.WriteLine(a.Chinese + a.Math);
}
}

使用implicit

    class Student
{
/// <summary>
/// 语文成绩
/// </summary>
public double Chinese { get; set; } /// <summary>
/// 数学成绩
/// </summary>
public double Math { get; set; } /// <summary>
/// 隐式求和
/// </summary>
/// <param name="a"></param>
public static implicit operator double(Student a)
{
return a.Chinese + a.Math;
}
}

求和:

    class Program
{
static void Main(string[] args)
{
var a = new Student
{
Chinese = 90.5d,
Math = 88.5d
}; double total = a; //a的总成绩 语文和数据的总分数
Console.WriteLine(total);
}
}
05-03 20:39