泛型类型的运营商隐 [英] implicit operator on generic types
问题描述
这有什么错用一个隐含的运营商这样的:
Is there anything wrong with using an implicit operator like the following:
//linqpad c# program example
void Main()
{
var testObject = new MyClass<int>() { Value = 1 };
var add = 10 + testObject; //implicit conversion to int here
add.Dump(); // 11
}
class MyClass<T>
{
public T Value { get; set; }
public static implicit operator T (MyClass<T> myClassToConvert)
{
return myClassToConvert.Value;
}
}
我想我可以当作实例对象作为一个值类型这样,但看到我从来没有见过这样我想一个例子,也许有一个理由的不的做这样的事情,有人能指出?
I was thinking I could treat as instance of the object as a value type this way, but seeing as I've never seen an example of this I thought maybe there was a reason not to do something like this that someone could point out?
在我实际的代码,我想这样做,作为一个数据抽象层的一部分,这样我就可以用信息返回对象描述的基础数据,但允许逻辑代码把它作为当它需要知道所有的价值,并在同一时间保持它的所有漂亮和类型与仿制药的安全值类型。
In my actual code I was thinking of doing this as part of a data abstraction layer, so that I could return objects with information describing the underlying data, but allow the logic code to treat it as a value type when all it needs to know about is the value, and at the same time keep it all nice and type safe with the generics.
推荐答案
如果以下全部为真:
-
的所有的可能值的你
MyClass的< T>
键入(!包括空
如果它不是一个值类型)映射到<一个有效的价值code> T
all possible values of your
MyClass<T>
type (includingnull
if it’s not a value type!) map to a valid value ofT
隐式运算符从不抛出(甚至不是空
!)
the implicit operator never throws (not even for null
!)
隐式转换,使语义意义,而不是混淆客户端程序员
the implicit conversion makes semantic sense and is not confusing to the client programmer
再没有什么错。当然,你的可能的做任何这三样东西,但它会很糟糕的设计。特别是,抛出一个隐含的运算符可以是很难调试,因为在那里它被称为不说,它被调用的地方。
then there is nothing wrong with this. Of course you could do any of these three things, but it would be bad design. In particular, an implicit operator that throws can be very hard to debug because the place where it is called doesn’t say that it is being called.
例如,可以考虑该 T?
有没有隐式转换为 T
(其中 T
,当然,值类型)。如果有这样一个隐含的运营商,它将不得不放弃当 T?
为空,因为有转换空没有明显的价值code>来,将有意义的任何的值类型
T
。
For example, consider that T?
has no implicit conversion to T
(where T
is, of course, a value type). If there was such an implicit operator, it would have to throw when the T?
is null, as there is no obvious value to convert null
to that would make sense for any value type T
.
让我举一个例子,我有麻烦调试,其中隐含的运营商抛出了一个问题:
Let me give an example where I had trouble debugging an issue where the implicit operator threw:
public string Foo()
{
return some_condition ? GetSomething() : null;
}
在这里, GetSomething
返回一个类型,我写它有一个用户定义的隐式转换字符串
的东西。我做的绝对保证的是 GetSomething
永远无法返回空
,但我得到了的NullReferenceException
!为什么?由于上述代码的不可以等同于
Here, GetSomething
returned something of a type I wrote which has a user-defined implicit conversion to string
. I made absolutely sure that GetSomething
could never return null
, and yet I got a NullReferenceException
! Why? Because the above code is not equivalent to
return some_condition ? (string)GetSomething() : (string)null;
但
but to
return (string)(some_condition ? GetSomething() : (Something)null);
现在,你能看到的空
来了从!
Now you can see where the null
came from!
这篇关于泛型类型的运营商隐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!