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.
Just to keep you informed, the research work I am conducting on Generics in .NET and finding suitable X# syntax, is going well. The topic seems a lot clearer and simpler to me now I have put in the work and experimentation / testing.
I have added some sections to the Cologne 2017 eNotes - see image below :-
It would seem to me that many of us coming new to Generics probably are best advised to follow up and use the Types already included in the current recent version of .NET - I mean those in System.Collections. Generic and System.Collections.ObjectModel.
If we don't go and see what these Namespaces offer us then we may all be reinventing the wheel, badly ;-0)
Has any of you used 'Laz<T>', and is so have you a clear and straight forward example to share with us ?
Finally, has any of you got a good / reasonable example of your own Generics Class, Method or other member ? What I find in eBooks and web articles all seem to simple, trivial, and no real hep or use at all. yes, they work, but that is a very different matter.
Roslyn uses Lazy<> all over the place. For example when it loads the types from an external assembly it does not get all the info about its methods, fields and properties. If the type is never used, why bother reading all of that info. So this info is exposed with Lazy properties. Only when you are requesting the info then it will create the info for you and return it.
That is probably one (of the many) tricks that makes Roslyn, and therefore also the X# compiler, so fast.
XSharp Development Team
I tried generics recently and finally skipped/delete all I've done so far.
Using generics has two requirements
- totally understanding of interfaces
- similar actions on different types
That's why generics on lists are relativly easy to implement.
I think we can use generics after implementing different classes with not only the same interface but also the same code. The usual copy and paste situation. Then we can replace the always same code of the different classes with a generic one.
Just short thoughts about it. Hope I'm right in some way.
True. As a rule of thumb, if I find I have a fairly compact specific class, and I'm copying it more than a couple of times and just changing the classes that I'm using in it, I start to look for a generic reworking of it. This has saved me literally thousands of lines of coding.
If the only thing you're doing is say storing an object in a property of the class, then not much is involved. But if you need to start actually carrying out operations, then the problem is how to guarantee that the classes you're passing in as type parameters support the required operations - and this of course is where interfaces come in. As you know, if a class implements an interface it's guaranteed to implement the methods and properties specified in that interface.
So in your generic class or method definition, the where clauses can be used to specify which interfaces must be implemented, and thus you can start to put specific method calls into your generic class. And this is when generics really start to become useful.
I for myself like generics, as they make code stabler and permit the reuse of methods and classes. At least in the collection classes they are used over and over.
I have to admit that even if I'm slowly feeling "at home" in the .NET framework with all its possibilities, I'm an absolute beginner..... Remembering the famous Socrates word "I know that I know nothing" all the time.
This makes you half cured to over-using Interfaces then.
My simple point was - just because something is good, and useful, does not mean it has to be used all of the time, anywhere.
I distinctly remember you eulogising about Interfaces at DevShare (maybe Shrewton) when you said everything should be done by Interfaces. You had just discovered them.
See, you need to be careful what you say, when the elephants have their ears outstretched ;-0)
I will get into Interfaces with Generics sometime soon. I promise you. However, remember that passing Types into Interfaces is Generics, its not the function of the Interface which has anything to do with the Generic side of it. The same ' < > ' technology applies to Events and Delegates too.
Hope this makes some sense,
P.S. Clocks go forward tomorrow ..... Cologne gets closer.
Yes, LAZY<T> sounds very interesting and useful - BUT - it would seem that for standard guys like myself, we need to go easy before 'WE' start using it all over the place.
If we are to use Entity Framework 6, or later, then the Microsoft guys have already built-in a sensible amount of LAZY<> access for us.
In my recent 'Stock' sample for Cologne 2017, when I get a list of Customers back, the Orders list for each customer is not retrieved until required/asked for. Now I understand why there is a short time lapse when I click the combo / drop-down box for an individual Customer's orders.
It looks as it for many of us just using the latest version of all of the .NET frameworks, that much useful Generics technology is built-in for us.
My feeling is that we should use Generic types, and the latest EF stuff, then see what is lacking, before we rush off and re-invent the wheel etc., etc..
I will attempt to attach and insert a short video which clearly shows this happening at runtime. Fingers crossed :-
The only issue / problem with watching the video is that the delay happens after a mouse click BUT the mouse click is not visually obvious ;-0( I think you will get the idea by watching the quick opening of re-visited drop boxes.
Remember that this is all available to me without me having to do anything. EF6 does it all for me.