Getting to grips with Generics.
Posted: Wed Mar 22, 2017 5:14 pm
Hi guys,
Well, its been a long time coming (years and years) but I am now in a position to start researching Generics as it is used in the .NET world.
Yes, some of us have used it with collections like 'List<int>' but for technical reasons could not make definitions ourselves, using the full Generics capabilities.
Now we have Roslyn then xBase guys can start from scratch and do Generics properly.
I have made a start and feel as if I am doing well, and I will document my coding successes in part of my "Appendix 'X' " which I am currently compiling.
I am trying to make Generics simple and straight forward, which it is, by finding the right documentation and suitable examples. I am winning I feel ;-0)
If you can manage parameters in Classes and Methods (which we do all the time) then we can also manage 'Types' in them as well. 'Types' is a word used when some of us may prefer to use 'Classes'. So when in documentation we read "Types and Methods" we can think of it as "Classes and Methods".
Okay then, lets make a start, parameters are provided in round brackets ' ( ) ' and now with Generics the Types are provided in chevrons, or pointed brackets, ' < > '.
We can have one or more Type provided, and these can be named other than 'T', or 'T1' or 'TVal' etc. Its just convention. Any text item in a comma separated list in the '< >' is taken as a Type.
Lets see an example I have had working earlier this afternoon :-
Notice that in the three lines to define the 'class' or 'Type' we have different signatures. Like our regular 'stuff' we can't have the same signature twice over. And, the names are convention, possibly quite arbitrary, and our own choice. I would stick to 'T' in many cases, as this is what we read in Wen samples.
Lets now see a Method, both in use and its definition :-
So for Generic classes (Types) we usually have both lists of Types in ' < > ' and also lists of parameters in ' ( ) '.
And yes we can have overloads, as with Class constructors and Method definitions.
Finally for the moment, I include an image of Robert's sample from the X# CHM file, with some modifications by myself, to suit what I am trying to do.
Notice on line 186 I have followed Wolfgang's advice on property definition syntax. Thanks 'WR'.
Before I go, it would seem that the point of Generics, is the same as Procedures, to reuse code and stop code 'bloat' by repeating similar code. As yet I have not a great or good example to demonstrate this, but you may get my point if you compare Generics with Type provision, to Methods and Procedures using data parameters.
Hope this may start an interesting thread, I do have more nice samples, but enough for now.
Cheers, and Best regards,
Phil.
Wales, UK.
Well, its been a long time coming (years and years) but I am now in a position to start researching Generics as it is used in the .NET world.
Yes, some of us have used it with collections like 'List<int>' but for technical reasons could not make definitions ourselves, using the full Generics capabilities.
Now we have Roslyn then xBase guys can start from scratch and do Generics properly.
I have made a start and feel as if I am doing well, and I will document my coding successes in part of my "Appendix 'X' " which I am currently compiling.
I am trying to make Generics simple and straight forward, which it is, by finding the right documentation and suitable examples. I am winning I feel ;-0)
If you can manage parameters in Classes and Methods (which we do all the time) then we can also manage 'Types' in them as well. 'Types' is a word used when some of us may prefer to use 'Classes'. So when in documentation we read "Types and Methods" we can think of it as "Classes and Methods".
Okay then, lets make a start, parameters are provided in round brackets ' ( ) ' and now with Generics the Types are provided in chevrons, or pointed brackets, ' < > '.
We can have one or more Type provided, and these can be named other than 'T', or 'T1' or 'TVal' etc. Its just convention. Any text item in a comma separated list in the '< >' is taken as a Type.
Lets see an example I have had working earlier this afternoon :-
Notice that in the three lines to define the 'class' or 'Type' we have different signatures. Like our regular 'stuff' we can't have the same signature twice over. And, the names are convention, possibly quite arbitrary, and our own choice. I would stick to 'T' in many cases, as this is what we read in Wen samples.
Lets now see a Method, both in use and its definition :-
So for Generic classes (Types) we usually have both lists of Types in ' < > ' and also lists of parameters in ' ( ) '.
And yes we can have overloads, as with Class constructors and Method definitions.
Finally for the moment, I include an image of Robert's sample from the X# CHM file, with some modifications by myself, to suit what I am trying to do.
Notice on line 186 I have followed Wolfgang's advice on property definition syntax. Thanks 'WR'.
Before I go, it would seem that the point of Generics, is the same as Procedures, to reuse code and stop code 'bloat' by repeating similar code. As yet I have not a great or good example to demonstrate this, but you may get my point if you compare Generics with Type provision, to Methods and Procedures using data parameters.
Hope this may start an interesting thread, I do have more nice samples, but enough for now.
Cheers, and Best regards,
Phil.
Wales, UK.