----------------------------------------------------------------------Currency.cs

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceConsoleApplication4{//类和结构相似publicstructCurrency{privateuintdollars;//元privateushortcents;//分publicCurrency(uinti,ushorts)//初始化构造函数{this.dollars=i;this.cents=s;}publicoverridestringToString(){returnstring.Format("{0}.{1,2:00}",dollars,cents);}//看情况选择是显示装换还是隐式转换,(uint和ushort都可以隐式转换为float)//重载运算符必须使用publicstatic//implicit隐式转换//把Currency对象隐式转换为float类型publicstaticimplicitoperatorfloat(Currencyc){returnc.dollars+c.cents/100.0f;}//explicit为显式转换//把float对象显式转换为Currency类型publicstaticexplicitoperatorCurrency(floatf){checked//溢出则抛出异常{uinti=(uint)f;ushorts=Convert.ToUInt16((f-i)*100);returnnewCurrency(i,s);}}}}

----------------------------------------------------------------------主程序

Currencyc=newCurrency(50,35);floatf=(float)(c);c=(Currency)f;Console.WriteLine(c.ToString());Console.ReadKey();