+ 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