显式的&具有数字类型和隐式运算符意想不到的结果
我从未对重载运算符做过任何广泛的工作,尤其是隐式和显式转换。
但是,我有几个经常使用的数字参数,因此我创建一个结构体作为数字类型的包装器,以强类型化这些参数。这是一个示例实现:
public struct Parameter
{
private Byte _value;
public Byte Value { get { return _value; } }
public Parameter(Byte value)
{
_value = value;
}
// other methods (GetHashCode, Equals, ToString, etc)
public static implicit operator Byte(Parameter value)
{
return value._value;
}
public static implicit operator Parameter(Byte value)
{
return new Parameter(value);
}
public static explicit operator Int16(Parameter value)
{
return value._value;
}
public static explicit operator Parameter(Int16 value)
{
return new Parameter((Byte)value);
}
}
当我尝试测试实现以掌握显式和隐式运算符时,我尝试将 Int64
显式转换为我的 Parameter
类型,并且令我惊讶的是它没有抛出异常,更令人惊讶的是,它只是截断了数字并继续前进。我尝试排除自定义显式运算符,但它的行为仍然相同。
public void TestCast()
{
try
{
var i = 12000000146;
var p = (Parameter)i;
var d = (Double)p;
Console.WriteLine(i); //Writes 12000000146
Console.WriteLine(p); //Writes 146
Console.WriteLine(d); //Writes 146
}
catch (Exception ex)
{
Console.WriteLine(ex.Message); //Code not reached
}
}
因此,我用普通的 Byte 代替我的结构重复了我的实验,并且具有相同的行为,所以显然这是预期的行为,但我认为导致数据丢失的显式转换会抛出一个例外。
I have never done any extensive work with overloading operators, especially the implicit and explicit conversions.
However, I have several numeric parameters that are used frequently, so I am creating a struct as a wrapper around a numeric type to strongly type these parameters. Here's an example implementation:
public struct Parameter
{
private Byte _value;
public Byte Value { get { return _value; } }
public Parameter(Byte value)
{
_value = value;
}
// other methods (GetHashCode, Equals, ToString, etc)
public static implicit operator Byte(Parameter value)
{
return value._value;
}
public static implicit operator Parameter(Byte value)
{
return new Parameter(value);
}
public static explicit operator Int16(Parameter value)
{
return value._value;
}
public static explicit operator Parameter(Int16 value)
{
return new Parameter((Byte)value);
}
}
As i was experimenting with my test implementation to get a hang of the explicit and implicit operators, I tried to explicitly cast a Int64
to my Parameter
type and to my surprised it did not throw an exception, and even more surprising, it just truncated the number and moved on. I tried excluding the custom explicit operator and it still behaved the same.
public void TestCast()
{
try
{
var i = 12000000146;
var p = (Parameter)i;
var d = (Double)p;
Console.WriteLine(i); //Writes 12000000146
Console.WriteLine(p); //Writes 146
Console.WriteLine(d); //Writes 146
}
catch (Exception ex)
{
Console.WriteLine(ex.Message); //Code not reached
}
}
So I repeated my experiment with a plain Byte
in place of my struct and has the same exact behavior, so obviously this is expected behavior, but I thought an explicit cast that results in a lose of data would throw an exception.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当编译器分析显式用户定义转换时,允许在转换的“任一侧”(或两侧)放置显式内置转换。因此,举例来说,如果您有一个从 int 到 Fred 的用户定义转换,并且您有:
那么编译器会推理“存在从 int 到 Fred 的显式转换,因此我可以进行从 int? 到 int 的显式转换,然后将int转换为Fred。
在你的例子中,有一个从long到short的内置显式转换,并且有一个从short到Parameter的用户定义显式转换,所以将long转换为Parameter也是如此
。编译器可能会插入隐式转换;在用户定义的隐式转换两侧进行内置隐式转换
在 C# 中,编译器永远不会链接两个用户定义的
显式转换,这是一项艰巨的任务,我鼓励您这样做。停止尝试这样做,直到您对涵盖转换的规范的整个章节有了全面而深入的了解。
有关链式转换的一些有趣的方面,请参阅我关于该主题的文章:
When the compiler is analyzing an explicit user-defined conversion it is allowed to put an explicit built-in conversion on "either side" (or both) of the conversion. So, for example, if you have a user-defined conversion from int to Fred, and you have:
then the compiler reasons "there is an explicit conversion from int to Fred, so I can make an explicit conversion from int? to int, and then convert int to Fred.
In your example, there is a built-in explicit conversion from long to short, and there is a user-defined explicit conversion from short to Parameter, so converting long to Parameter is legal.
The same is true of implicit conversions; the compiler may insert built-in implicit conversions on either side of a user-defined implicit conversion.
The compiler never chains two user defined conversions.
Building your own explicit conversions correctly is a difficult task in C#, and I encourage you to stop attempting to do so until you have a thorough and deep understanding of the entire chapter of the specification that covers conversions.
For some interesting aspects of chained conversions, see my articles on the subject:
这个目标:
,并且此代码:
完全矛盾。通过添加 2 路隐式运算符,您可以取消包装器可能带来的任何类型安全性。
所以放弃隐式转换。您可以将它们更改为显式的。
This goal:
And this code:
Are in total contradiction. By adding 2-way implicit operators you annul any type-safety the wrapper might bring.
So drop the implicit conversions. You can change them to explicit ones.