Presenter:Mike Taulty
Generics in .NET Framework 2.0
The following text is a software generated transcript of the video. Click on a minute link to jump to a location within the video
Minute 0
Hello and welcome to the stock at its session 1 in a series of short session will look at key and interesting technologies in her net framework and visual studio wife Mike Tolkien I work in the developer and platform group in Microsoft in the UK and in this particular session and is less than five to 10 minutes talking about new feature in version 2 of net cultured ex so generics is actually a single archive and runtime feature for net version 2 and ultimately spans languages of this and see sharp and deeply and so on so that switch radio with visual studio and transit walk tragedy with generics okay so here I am working with visual studio 2005 beta one refresh which you can get from MSDN subscriber downloads a solution had try and illustrate and the key points around generics so through the solution are inside the solution I ve got
Minute 1
Of implementations art of a snack thrust back into a fairly standard computer science economic data structure and I m sure it was familiar with the Stack ingenuity that this is the real implementation but now we do have the bare bones of a stack it took the colours of the things that we would have on a stack would be a push operation and it would have a pop operation and then clearly we need to have some kind of story to the items on the stack in here and is using an array of the interesting things about this that because of the way net 1 1 works we d end up having to pick a type to push onto the Stack Oleander had attacked apart from the stack and in this particular case wife undone years I ve chosen to use object because in net Everything can be treated as objects like a go ahead and what I could have done without builder a m and in
Minute 2
It is stack a day and that would have had a proportionate looking into German and so on and then if I wanted to builder a m float stack I would have to go ahead and then have smokestack and so on and without footwork Porsche that took her afloat and I m labouring up and ready but clearly I wouldn t want to go and did as I don t have many versions of the Stack class only one to have won as in net 1 1 the way that I achieve this ears by falling back to the lowest common denominator that every type in the net type system and that its object and we can treat afloat is logically completed into is an object with intrigue customer as an object and so on and so full as a number one is exactly the kind of class I would ride it if I was writing a Stack class however falling back on to this lower the lowest common denominator of object causes of
Minute 3
A few minor issues to look at and got intimate use of this stack is my programme you can see that what I m doing here is an creating a new instance of that stack and I m pushing the value towns that an integer value 10 on their now what if I wanted to only have a stack that time contained integers and this factor I ve built Fat really couldn t facilitate this because it accepts objects to one of the problems of accepting object is somebody can take my stack and they can push floatingpoint value on their and they can also put a strain on their and whether I intended them to all wanted them to does nothing I can do about this because it accepts object you can put any old rubbish if you like on their and because you can put any old rubbish on their way new poll something back off with a pop operation which again returned object you have to cast so we have no idea really what s coming back from this pop operation in the schedule at abuse
Minute 4
Ringers at this cast will fail as a then other so side effect because you have the capacity was out to cope with the possibility of the cast family and you getting in an exception as you do that invalid cast that means is essentially we don t have a tight safe stack or if you want to have a type safe stack we have to build stack of into stack of float stack of decimal stack of Spring etc etc etc no wants to do that yet the round this is bad in the net type system and the value 10 here undervalued 22 1 2 floatingpoint are actually not real object types they are more equal value types and there s a small problem in that when we pass this 10 across that this method which except objects are the net runtime that he has to go through process called boxing has to go through cradling her a copy of the object on the managed heap and there s a small performance penalty of having to do that we get more memory allocations and we really would like similarly
Minute 5
And we come back from this top operation and we returned and object to turn it back into an integer we go through a procedure called on boxing which it has a small performance penalty associated with so that s about the best we can do with net 1 1 lets now take a look at what we can do with our generics in net 2 0 and see how it solves all of those issues for us so if we switch over to look at our generics stack a look at this implementation so you can see on the screen here that have built the generics stack again is not fully completed its own just sketched out really but the important thing to say is that this type is actually being parameterised sunny a so called object_type his little people anything I would am saying is that I want to build a class parameterised around this tie but I won t know what this type is until usage time she is now the push operation
Minute 6
Pizza parameter oh of type object type which will be supplied at a later point and pop returns object type and the stories that were using is going to be an array of object type similar using generic stack of integer effectively all these objects I become replaced with integer and I m using generic static customer effectively all the subject has become customer now it s important to realise this is not done at compile time at compile time what we get it compiled into I L is one single type generic stack parameterised around object I and only at run time will be acted on specialising senses of the spring to the first time that you come along and use generic stack of integer we will go and build a class in everything I feel I will build a new class for generic stack of integer if you reuse demerits that eventually will get built again and if you ve ever make generic stack afloat then again
Minute 7
Eat run time representation that specialised afloat is worth saying that if you have generic stack of reference tariffs like customer and address our bank balance and Somerset force only one implementation of generic stack will be used to serve anything that is restoring a reference type because essentially all referenced out at the end of the day object in life types to visit a river that the use of this custody in the programme so here I am using a generic stack of integer and clearly creating instantiated in assigning a new instance objects that integer I needed it were pushing at an edited 10 onto their antibody to realise is that the generic stack of integers will only accept integers we can t put a strain or afloat on here is an integer stack and similarly where we get to the return value from pop here you can see that return value is integer and so there s no
Minute 8
Cast any more of this note that no type safety problems involved there are no adults only got around boxing and on boxing potential performance hit from before because the parameters this method is an integer as we don t have the convert to object and the return value from this method is an integer is enough to return back from object we can do because build a generic surface drains and a sign that a new instance of that type and then we can push a string of loving off a string of again without casting now it s important to realise it was surely latest a very simple generic type that this type can t just be parameterised around one single parameter we can also have second or third parameters would have some other type here and so on and so forth you don t just have to have one typewriter and also it s not just classes that can be generous size so we can have am a generic class like this that has generic methods generic properties generic delegates and so on and also we do
Minute 9
Non generic classes that include generic methods so it s perfectly possible that we can have a class at college deaths here and we have some effort on here public some time and that s cause massive convert to and as his generic size around some type and then we can return something or other from their solicitor generic methods so there are many instances potentially of this method depending on how you actually am parameterised it and it is that service is dutifully to know what to return here I could act as she looked out of to just return I ll not like because of the default case problem with that would be that some type might be an integer afloat or it might be a customer and address you could be of value type or a reference type to have a special syntax with dealing with the difference that it was not done because value tax can t cast on olds so
Minute 10
Carries we can use the default value of some type which for a reference that is going to be null and the something like an integer is going to be zero so that gets is around the difficulty of not knowing at a construction time what some type is whether it s valuable reference type but we can indeed Andy Merritt sighs classes interface is method properties and so on and so forth and engages like this glacier once the classes and generic the method itself is generic interesting to say about this technology is that turn what if we wanted to in postmaster before we allowed an object gone to stack what if we wanted the validator in some way and am I the only thing the compiler knows about this parameter oh is that it is of some type which is not yet specified several its use O Hear were what can we do with her we can see that the only things we can invoke on oh the things that come through from system the object because clearly
Minute 11
Regardless of whatever we provide as object out and specialise always always going to be on object type because everything is object I so if really it is something I can validate the same then clearly win that we can t call a validate methadone on this oh up object high talking to it that cases we can actually constrain the parameters so we can as to give the compilers are more information about object_tight and we given it so far so for instance peer I have and interface called I validate will and as a simple method article validate to work and it is I think here s the compilers are more information here I can say we re object type is I validate a bull validator will gag and with that there that they means that in this in this man Method article validate such nature we can do that
Minute 12
Because the compiler is now going to make sure that any time this type is specialised it will not allow it to be specialised with any type that is not my validator will surface of the fruit to go about our programme at this point we would find that turned generic stack of end is not really any longer valid because in those and implement I validate a bull is approved that you build this code we should see the turn in this particular area here the advertised it is not I validator will therefore is not allowed to be used as a parameter to that generic type so by adding this certain constraints here we have limited the types that are out of use the specialisation and we can also constrain the Aaron of the object I didn t include things like out whether the the type has a default constructor and so on and so forth because otherwise it is very difficult for us to a scene stand
Minute 13
Late instances of this thing within methods here so I justify nothing to say we haven t covered every topic of With generics that is that there are a whole bunch of generic masses already already there in the net framework version 2 and specifically the most interesting was it a spot must immediately will be the ones that live in the system collections generic amare some things you get in atclassified list services of generic analysts that you can use down their passes like a stack for bigger example so again S gases such as dictionary to find dictionary that he and its generic size around is that he and values if I wanted to dictionary the maps into just a string is that I can use and so on and so forth so it can standard collection classes that you might expect already there are mounting a
Minute 14
Size them in that namespace is also a classical loot list which was often there as well so you can come across these uncommon idiocy modernises across the net framework and think as we go forward so what have we seen in this shorts well as an introductory scene at generics allow us to build parameterised types and rebuild one type at compile time and a runtime if she specialised a type around some kind of parameters that we can use this kit as benefits of the health of their buildings like collections with strong typing so you have to fall back on or always relied on object as a lowest common denominator that gives us performance benefits because we are figures to things like boxing boxing is beside s benefits because in the past that he wanted to build stronger tax collection you had to keep rebuilding collections again and again and again generics allows us to build the wants and has parameterised them as we set the seal feature certain
Minute 15
Hit one particular languages there is the sharpest only be experimented C so I won t not be mixed with templates which is a C compile time feature as important to stress that this is a runtime technology is not a compile time technology so it s different from her how templates working C in there was a compile time and indeed if you are interested in the differences between the two as searching a favourite search engine for generics and templates and the differences classified a nice article on so if you do want more information we have MSDN developer centre devoted to visual studio 2005 and that if you go and search your favourite search engine for MSDN generics overview don t you find a whole bunch of articles that will help you understand generics better if you want to follow other session than feel free to e mail the address is there on the screen and other than that it is listening and eye catching excellent
Click here to go to original video page
Thursday, July 24, 2008
Video: Generics in .NET Framework 2.0
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment