显式具有数字类型& amp;的隐式运算符出乎意料的结果 [英] Explicit & Implicit Operator with Numeric Types & unexpected results
问题描述
我从未对重载运算符进行过任何广泛的工作,尤其是隐式和显式转换.
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);
}
}
当我尝试测试实现来了解显式和隐式运算符时,我尝试将 Int64
强制转换为我的 Parameter
类型和类型令人惊讶的是它没有引发异常,甚至更令人惊讶的是,它只是截断了数字并继续前进.我尝试排除自定义显式运算符,但其行为仍然相同.
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
}
}
因此,我用普通的 Byte
代替了我的struct并重复了我的实验,并且具有相同的确切行为,因此显然这是预期的行为,但是我认为显式强制转换会导致丢失数据将引发异常.
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.
推荐答案
当编译器正在分析明确的用户定义转换时,允许放置明确的内置在转换的任意一方"(或双方)上进行"strong>转换".因此,例如,如果您有一个从int到Fred的用户定义转换,并且您具有:
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:
int? x = whatever;
Fred f = (Fred)x;
然后编译器说明从int到Fred进行了显式转换,因此我可以从int?到int进行显式转换,然后再将int转换为Fred.
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.
在您的示例中,内置了从long到short的显式转换,并且有用户定义的从short到Parameter的显式转换,因此将long转换为Parameter是合法的.
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.
在C#中,正确地建立自己的显式转换是一项艰巨的任务,我鼓励您停止尝试这样做,直到您对涵盖转换的规范的整个章节有了透彻和深入的了解.
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:
http://blogs.msdn.com/b/ericlippert/archive/2007/04/18/chained-user-defined-explicit-conversions-in-c-part-two.aspx
这篇关于显式具有数字类型& amp;的隐式运算符出乎意料的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!