Specialization of generics

  • Thread starter Thread starter Adam Badura
  • Start date Start date
A

Adam Badura

Is there a way to specialize generic types or methods tha same way as it
is possible in C++ for example?
I know that instead of specializing a class

class MyCollection<TType>

for string type I could do something like

class MyStringCollection : MyCollection<string>

or even something like

class MyCollection<Type> : IMyCollection<Type>
class MyStringCollection : IMyCollection<string>

However selection of type MyStringCollection is not automatic. If
programer uses MyCollection<string> (perhaph unaware of the existance of
specialized class) then nothing will even let him know that he should use
MyStringCollection.

So to sum up is there a way in C# to do it like you can do it in C++?

Adam Badura
 
you could use the 'ObsoleteAttribute' but it could be percieved as
misleading

But if the generic class is marked with this attribute then warning will
be issued even if programer uses MyCollection<int> for which there is no
specialization and the generic schould be used. So indeed misleading. :)

Adam Badura
 
Adam Badura said:
Is there a way to specialize generic types or methods tha same way as
it is possible in C++ for example?
I know that instead of specializing a class

class MyCollection<TType>

for string type I could do something like

class MyStringCollection : MyCollection<string>

or even something like

class MyCollection<Type> : IMyCollection<Type>
class MyStringCollection : IMyCollection<string>

However selection of type MyStringCollection is not automatic. If
programer uses MyCollection<string> (perhaph unaware of the existance of
specialized class) then nothing will even let him know that he should use
MyStringCollection.

So to sum up is there a way in C# to do it like you can do it in C++?

It's not the prettiest solution but you could check if the type of the
generic parameter is a string and check that this.GetType() is
MyStringCollection or not MyCollection<string>, and throw an descriptive
error if it is. In the constructor.
Of corse you only get runtime checking, and it's not encapsuled very well.

Kind Regards,
Allan Ebdrup
 
It's not the prettiest solution but you could check if the type of the
generic parameter is a string and check that this.GetType() is
MyStringCollection or not MyCollection<string>, and throw an descriptive
error if it is. In the constructor.
Of corse you only get runtime checking, and it's not encapsuled very well.

Yes. I thought on that also. But to be honest I think it is better to
not check it at all if the check should look like this. However this could
be improved if this check was done as Debnug.Assert to not cause errors in
the Release version. After all the reason to do specialization is most often
to do some optimizations not to correct errors, co using unspecialzied
version in moste cases is not required - only causes worse perfomance.

Adam Badura
 
you could use the 'ObsoleteAttribute' but it could be percieved as
misleading

Giving it second thoughts I think that perhaps it is possible to make
own attribute which will do the nessecery checkings and issue a warning when
required.

But still this is kind of workaround. The best solution - in my
opinion - is to have the effect like in C++. If programmer writes
MyCollection<string> then compiler selectes specialized implementation
instead of the generic one.

Adam Badura
 
Adam Badura said:
Is there a way to specialize generic types or methods tha same way as
it is possible in C++ for example?

No, there is not.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top