public struct MyInt { public int csInt; public MyInt(int i) { csInt = i; } public MyInt(double d) { csInt = (int)d; } public static implicit operator MyInt(int value) { return new MyInt(value); } public static implicit operator MyInt(double value) { return new MyInt(value); } } int MyFunc(MyInt i = 0) { return -1; }
我想实现一个包装器structMyInt(在这里简化)接受int作为默认值(我知道这很奇怪和不自然,我只需要它遵守其他语言格式),但我遇到错误,当我像上面的代码,错误是at int MyFunc(MyInt i = 0)其中VS2012说:
a value of type ‘int’ cannot be used as a default parameter because there are no standard conversions to Type MyInt
据我所知,int和double也在C#中定义为struct.所以我试过以下:
int MyFunc(double i = (int)0) { return -1; }
它过去了!因此我认为默认参数允许类型转换.
所以我的问题是:
>为什么不能将MyInt的隐式类型转换用作默认para?
>标准转换在VS错误消息中的含义是什么,它是否来自MyInt中定义的隐式转换?
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。