Can generic types provide methods? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 4

Can generic types provide methods?

I may be too quick to ask as I have only started the section on generics. What I wonder is if generic types can provide methods šŸ¤” I do not have a concrete idea, yet. But let's say a method only know what to do but not how. But the type knows how: void DoSomething<T, A>(T knows, A withWhat) { // do what DoSomething can do knows.howToDoIt(withWhat); // continue ... } The idea is to be able to change some details of the method based on the type šŸ™‚ Thank you šŸ¤—šŸ’•

13th Sep 2021, 3:29 AM
Agnes Ahlberg
Agnes Ahlberg - avatar
2 Respostas
+ 1
Sounds good .... However unlike C++ you cannot use Specialization in C#. Itā€™s down to generics, simply put a generic version of class or method for <T> must be the same for all instances. You can work around by embedding checks within the method but this kind of goes against the grain of generics.
13th Sep 2021, 9:17 AM
DavX
DavX - avatar
0
Thank you šŸ¤—šŸ’• Yes, interesting šŸ¤”šŸ™‚
13th Sep 2021, 1:10 PM
Agnes Ahlberg
Agnes Ahlberg - avatar