use implicit, C# codes as below:
using System;
class Integer
{
private Integer(int d) { val = d; }
public int val;
public static implicit operator int(Integer d)
{
return d.val;
}
public static implicit operator Integer(int d)
{
return new Integer(d);
}
}
class Program
{
static void Main(string[] args)
{
Integer dig = 12;
Console.WriteLine(dig);
Console.ReadKey();
}
}
本文通过一个C#代码示例介绍了如何实现整数类的隐式类型转换。示例中定义了一个Integer类,并实现了从Integer到int及从int到Integer的隐式转换操作符。

被折叠的 条评论
为什么被折叠?



