C# implicit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Digit
{
public double val;

public Digit(double d)
{
val = d;
}

// 将 Digit 对象转换成 double 数字
public static implicit operator double(Digit d)
{
return d.val;
}

// 将 double 数字转换成 Digit 对象
public static implicit operator Digit(double d)
{
return new Digit(d);
}
}

class Program
{
static void Main(string[] args)
{
// test
Digit dig = 2.1d;
// test
double d = dig;
}
}