开发者

Generic overloading tells me this is the same function. Not agree

base class:

Class List(Of T)
    Function Contains(ByVal value As T) As Boolean

derived class:

Class Bar : List(Of Exception) ' Exception type as example '
    Function Contains(Of U)(ByVal value As U) As Boolean

compiler tells me that that two are the same, so I need to declare Overloads/new this开发者_JS百科 second function.

But I want use U to differentiate the type (one logic) like NullReferenceException, ArgumentNull Exception, etc. but want to leave the base function(no differentiation by type - other logic) as well.


Well the equivalent certainly compiles in C#. I'd say you should avoid doing it, however - give your method a different name if it's going to behave differently. Overloading by type parameter in a derived class is rarely a good idea - and indeed deriving from List<T> is rarely a good idea in itself.

In VB you have to declare that you're explicitly overloading - which you are. You should add Overloads to the function declaration. Basically this method is not the same as the base Contains method - it has a different number of type parameters (1 instead of 0) and the parameter is of a different type (U instead of T). Note that contrary to your title you're not overriding anything.

Can you give more indication about what you're actually trying to achieve with this class? We may be able to suggest a better approach.


If you expect to pass a different type to the class then you might want to use a where in your generic declaration. That might be enough info for the compiler to be happy.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜