How would I limit the general method in C # to allow T, where T has TryParse?

public int DoSomething<T>(string someString) 

I want to insist that I can call TryParse on T. Can I restrict T this way? TryParse not part of the interface, it seems to exist on several types of values, so how would I do it?

+5
source share
1 answer

You can not

Unfortunately. I do not know what else to say. It is simply not possible. You can enable any T , and then do a reflection somewhere and throw if it does not have a method called TryParse , but since it is not part of the interface or base class ... there is no way.

+7
source

Source: https://habr.com/ru/post/1273466/


All Articles