***关于自定义类型转换***

zabaglione 2013-08-13 05:05:39
我自己写了下面的类型转换,但是编译的时候一直报错,
有高手知道原因吗?我感觉代码没有问题啊

c#代码

using System;

struct Currency
{
public uint Dollars;
public ushort Cents;

public Currency(uint Dollars,ushort Cents)
{
this.Dollars = Dollars;
this.Cents = Cents;
}

public override string ToString()
{
return string.Format("${0}.{1,-2:00}",Dollars,Cents);
}

public static implicit operator float(Currency value)
{
return value.Dollars + (value.Cents/100.01);
}
}

public class Test
{
public static void Main()
{
Currency Balance = new Currency(10,50);
float D = Balance;
}
}


编译的时候,就报错
Currency.cs(21,10): error CS0266: Cannot implicitly convert type 'double' to
'float'. An explicit conversion exists (are you missing a cast?)
...全文
212 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
智商余额不足 2013-08-13
  • 打赏
  • 举报
回复

public static implicit operator float(Currency value)
{
   //return value.Dollars + (value.Cents/100.01);
   return value.Dollars + (value.Cents/100.01f);
}

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧