Presenter:Mike Taulty
Iterators in C# V2.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 Nugget session 1 in a series of short sessions where we spent five effeminate looking at interesting and key technologies Inc net turnaround net minus m Tolkien I work on the development platform group in Microsoft in the UK and this particular set our to take a very brief look at a new feature in Ossetia language version to which we call iterators so that switch later with the visual studio 2005 I m using the beta one refresh version of visual studio 2005 which you can get from MSDN subscriber downloads so here we are in visual studio likely to spring up the window here and I ve got a couple of projects in here already but one particularly to tie that the iterators so the idea of iterators ears to make writing in innumerable types of simpler than it is today so a tightening innumerable is all around implementing the interface I innumerable
Minute 1
It only has one economical getting numerator Knapton i e numerator safeguards and a classical string list is completely useless class to be honest though it illustrates what I want to show here is existing list all that is a theatre wrapper around an array of strings so it just keeps in a reference to raise strings internally and really that s it but it makes that an array of strings which is already innumerable and innumerable to act nothing really to this picture but it is less innumerable and weight does that is by implementing the required getting numerator function which needs to return an instance of my numerator and you can see the way it does is booked by creating a new essence of this class and passenger at this point across into that class a weather eye in numerator looks unsure of your already aware ears that turn to begin his expanded format that can appear online the
Minute 2
Later you are lives to invent a property court current which does what you expect a function call move next which again does what you expect a return to Boolean say whether it s possible to move any further on as a single reset is sent back to the start of the year enumeration and leisure requirement for implementing our own numerator somewhere to hear cities are nested class inside of a disc dismaying ghastliness in order to do that enumeration form and that s quite a convincing people to do so really any time that I wanted to implement and innumerable class and innumerable collection or whatever it might be and always went after implement I innumerable and always will have to write a class that looks like this which maintains wherewith currently up to in the enumeration hand contains a reset method and the current property in its next dignity that the closer implementing
Minute 3
Enumerated actually more in this case of the code I have has to enlist in the first place so there s quite a bit access to implementing I innumerable and I numerator and really the code that the writing here is boilerplate so whether their son is an enumerator for this particular class stainless toilet was enumerated in some other kind class not much really needs to change as you could absolutely boilerplate the stuff Exocet in particular here we were dealing with stainless roses and other enumerator would presumably be doing some other type so you probably will have to write the code everytime it s always going to look and 99 so just in terms of how this would actually used in some programme then clearly we can go and make ourselves a history lesson here we are making a string vest and sticking an array of strings into that analysis using freekick around the innumerable collection and pulling at each strength so that it will expect at work
Minute 4
To our visit is in floor and iterators because what thereabouts he is is no longer having to type in this boilerplate codes the compiler can degenerate this for us to do and you am version of the stringless classless than to look at that we look at this new stringless then again you stringless and implement are innumerable and it s just a wraparound array strings are not change the functionality millionaire anyway but there are two new methods or rather properties on this class here you can see that after a property called forwards and woggle backwards and don t does was return an instance of my innumerable and it is weird thinking that they re using this new iterator syntax in order to enumerate forwards through my collection what I m saying is that we are every strength and the yield a return value of that string S industry and collection service is News and that s like saying is
Minute 5
At every time effectively yield return his son is executed that is one of the elements that I want a car park as a member of the collection that is being enumerate it around so this is a return value from my enumeration if you like and you see we re also implementing a backwards which again is an enumeration and office it is going around the read backwards and returning them the right string at the right point and we could all kinds of enumeration is here as you have something like a tree structure you can navigate it in prefix postfix or insects or whatever you want to do and in a much more simple way then you will be able to if you had to write in enumerating yourself so this is that we do and is about to say that if partly through this collection actually wanted to stop returning avenues syntax yield break and that is going to terminate the enumeration so if I wanted to innumerate it half my strengths and I could make shall only got
Minute 6
Eventually could yield an breaker that will be the end of the enumeration so as except this class that s the influence I innumerable itself and the way that I ve done that here is simply by saying that for the eye innumerable implementation which is getting in rate I does return for McFall would stop getting in rate to that is going to call this property get and I innumerable and then return the numerator office so that gets as enumeration is as well directly on the tie so if we look at the use of this you can see that term visits to the new stylus into the ring as it was before so here I am creating a new string vest with three strings in an eye on them we just enumerate around understand the fashion we can also numerate to the forwards property and we can also numerate through the backwards property so if you re a slightly suspicious person you likely wondering what exactly is going on here
Minute 7
To see chart compiled in this work for us and so we can get a little bit of a handle on what s going on by rising some simpler coach if we go across to class again must have crossed and used to enlist and Indonesia in this lets a sudden new collection here so sorry that Avenue function in summer to add an Iranian will property let s call this serves some strings and in the to those which has grown to say yield return one and will also yield return to Andrew yield return story so we built an innumerable hour collection if you like that has been returned from this property and we can go through and get 12 and three but still flat to ensure that compiles and I ve does r CIL disassembler I held out and have a look at the code has really been generated to
Minute 8
He continues to enlist and look at that get some strings to see some strings are putting in throat and in it as well as he happens in here ears code isn t in here at all there s no want and three here was that it happening is an instance of some new object called get some strings out of the magic name on it is reconstructed and it looks like this point was being stored in a object to figure out look at Agassi the compilers generated this class get some strings for us and if we look at this guy you ll see that he looks very much like a traditional Arab implementation of my enumerator he has moved next to the Hanson current on their he has all the stuff to expect because he does out in front I enumerator feeling I enumerator so the compilers may discuss up for us and if we have a look at something likely to
Minute 9
Next method here you see that in this method compiler in this simple case has effectively done and built a very simple state machine for us in that the first time that we come in its got a load of the string won and stored out into current and the next time you come into here is vital at that string to a store that into current and each time it is jumping to the end of this method after that since returning after this method so based on where we are at the collection and the compiler is going to switch between 12 and three and returned the right thing and when it is asked for the current value of the set enumeration to get it where we said that that everything was boilerplate than it really was by the plate because the compiler taxi generated for is if the compiler can do that then why not let the compiler do that it means less prototyping and therefore by honest definition letterbox to get created
Minute 10
So what have we seen in this short session with scene and you ve got these new iterators feature and see Shotley two acres is a shortened syntax and at more convenient syntax and building them innumerable types tax that we can attract around and this is being done by the seater compilers has nothing been added to the seal to facilitate this it s just that he shall compiler and generating to Mexico for advice on what we will attack into coat and the syntax that thing is that yield returns and acts of a yield of breaks and absolute looking on the slight inaccuracy is more information on this and other MSDN there s a whole developer centre dedicated to visual studio 2005 net framework 2 0 classifieds read articles on this technology I suggested a favourite search engine and enter and as the empty shop in iterators and find some great articles where or if you want to follow directly their favourites and e mail my addresses are on screen
Minute 11
And other than that
Click here to go to original video page
Thursday, July 24, 2008
Video: Iterators in C# V2.0
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment