How to determine the type of the returned value within the generic method?

Sure, just add in some code like this: if(typeof(TReturnType) == typeof(int)) { var number = (int)returnValue; //Validate your output }.

Oh my God! This was so simple is absolutely embarrassing. I definitely need to go home now.

Thank you. – Sergio Romero Jul 10 '09 at 20:46.

Sure, but you should consider whether or not this is a good idea. You can always say if (typeof(T) == typeof(int)) whatever But doing so is a bit of a bad code smell. The whole point of generics is to be generic.

If you have special-purpose code for when T is an integer, then why not simply add another method that handles exactly the integer case?

I've done this on a generic collection that supports 3 kinds of ids: strings, ints, and guids. Anything else would throw a not implemented exception. – Jake Pearson Jul 10 '09 at 20:36 Better to just have three methods then.

Make your generics generic, that's why we called them generics. – Eric Lippert Jul 10 '09 at 21:52 You're right, I was probably being lazy. – Jake Pearson Jul 10 '09 at 22:18.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions