+ 9
Why C# is not taking 'T' as a generic?
I thought I understood C# generics well enough.... until I made my own code. Why is C# taking T as a typename rather than a generic in the mentioned code(I hope I stated that correctly)? I tried a lot, but I can't seem to find what the problem is? https://code.sololearn.com/c4qcwty78Vb0/?ref=app
11 Réponses
+ 14
XXX I explain the issue here in a recorded video.
https://www.sololearn.com/post/223795/
+ 3
David Carroll thanks a lot.
And i fixed the bug.
+ 2
I am not really sure but shouldn't you use <T>
+ 2
Here's a workaround but just for the comparing. As I see the sorting is not quite well.
Anyhow I'm no expert, just a fellow sololearner trying to help, and learn while helping.
https://code.sololearn.com/cj959AN1U57g/?ref=app
+ 2
But you ultimately converted the parameters to Integer. What if I give a float as argument?
Still thanks for your time. I still can't understand what's wrong with my code.
+ 2
XXX Let me know if you need additional clarification.
Reference to Generic Constraints in C#:
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/constraints-on-type-parameters
Reference to IComparable.CompareTo(T):
https://docs.microsoft.com/en-us/dotnet/api/system.icomparable?view=netframework-4.8#remarks
+ 2
It was a very silly bug after all. Thanks.
+ 1
Mihai Apostol just tried that. Still showing operator '>' is not allowed for operands of type 'typ'(I renamed T to typ)
+ 1
XXX Nice... I had an earlier recording attempt where I mentioned how to fix your sorting issue. However, the recording was cut off before I could finish explaining the issue regarding constraints. So, I left the final recording focused on just generic constraints.
Plus, I figured you would want to fix the bug with sorting on your own anyway.
+ 1
wow. That's really cumbersome. I'd stop defending C# after this, David Carroll 😃
I'm joking, btw ☺
+ 1
Selin Genkur It's actually quite brilliant when viewed in more practical scenarios. Not that this is invalid. It's just not possible to see the elegance of static code in a clearer context with code in single file, narrow width format on Playground.