Google Search

Google
 

Wednesday, July 23, 2008

Video: C# Version 3.0 - Extension Methods

Presenter:Mike Taulty

In version 3.0 of the C# language you can write methods that 'appear' to belong to other classes. These are called extension methods, and we have a look at how it's done in this session.

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

Well an understanding of the accession is one in a series of short videos most than five to 10 minutes as looking an indistinct visual studio net framework minus might told you were the developer and platform group in Microsoft in the UK and it is one of a number of sessions around the perspective of version 3 of decision at language rate is currently kicking around under a preview with the ban on naming of language integrated query or link to searching for uncertainty do you think you find that their technology in assessing what I do is take a look at a small feature of the language that I was right now which is a cyclical extension methods such after an outlook that pockets on a visual studio 2005 at install the main preview of the link technology which gives me a seizure of legendary compiler and the idea of extension methods is to give the illusion that you ve added methods that I

Minute 1

That you may or may not own hands that you haven t really added a method to so what of the something otitis either the class but for a faster person onto person as their property for our first Marine Wexford property for last name and grade are casing whenever it doesn t really matter a little drive another classless cornerstone employee and is derived that from person network ahead and add a property analyst chorus and 409D with honour at a religious rather than the public unlimited protective so employee can see his own unemployed ID but the public will come to got personally gone fully simpler that is classless that was

Minute 2

Imagine I want to write a method that friends at the first and last name of a person now and they only photographed with their second on added person last by Nenagh and I would probably do but say writing class of year woman to play him a static class will call it can person extensions and and add a method of this medical exam at Iraq in Aberfoyle is quite different person as a second person p analyst who is consort or right linefeed offers known for speed last night prosper and there is well literary review string of format but would it work that don t build our own netted over here with a greater person if you seen the previous session about object initialisation in the syntax will not surprise you has

Minute 3

Do that in my shirts and Thatcherite no we didn t always not a person accessible and e classes published being a simple tailoring business analyst out of so clearly long or person extensions person and give it a person out why extension methods do is they allow us to make this kind of message print person here to be on the person class itself and the way that we do that from a syntactical point of view if he can take this method of paper in person and we can give it a keyword of this on parameter this person p biggest at more parameters following is but the first parameter in a special in that this marks this method out as an extension methods

Minute 4

About how has it to do is call p print person was built and we consider the Bill is just fine so there is no method courtroom person on the person object or person type rather that doesn t exist compilers that it is looking at the person type for a method called print person as it is a fine one then what is prepared to do is go and search all of me in the scope name spaces so any namespace which is currently in scope and try and find a method which looks like there saw in our case a method which takes no parameters it will translate into method at its one parameter here for the present instance itself so that it was not only my parameters are let out a couple of more parameter to get 10 and 20 year than this matter of who would lead to more parameters in Texan and why something like that but it right at Hexham

Minute 5

And why there is well it uses as France s about the compiling of the compiler searches in scope name spaces so what if we were to take our extension here and move it into another name special about any file of the class it will call its own extension one to take what senior are wary only many of us as to take that sort of coherent and only files that is person extensions and will put that into extension one you doesn t manage this residential monitor sort out that having been fallen and thousands from the compiler to an extension one and is only compile so we get a lot of

Minute 6

Parents and from recompilation by moving this class over into a new programme and just bringing thatis notis narrow in scope it will now be searched at the thin person method and of course when we comes out in Monaco if we would underpin this massive press 5 inch run at we can see that we do end up in print person so you had a censure method to Person object now what if we were to go and add another extension class to go ahead here and got the extension one faces into the projects as renamed as extension to persuaded ourselves suddenly I will put this intogood extension to look in aim of the class because it is relating differences in the different names must now I ll just change the current year so that it rights out extension to

Minute 7

Something like that back to our original code lets mainly in theextension to uncommon early extension one that is built but certain bovine here and on and was it only stepping there were popping up an extension to not extension while to the compiler is searching the Internet names faces for a resolution to this print person method doesn t really exist on the person class notably in both of theis at the same time scenario to potential extensions for the compiler to choose from than it will fail and say I don t know which of these two actions select so at any one time from any particular tie you can only really have one extension visible to the compiler in a particular model otherwise is going to give you an error at this becomes kind of interesting cars were all fined here is that in a

Minute 8

Are extension methods or here we build an extension for person we also does an extension of very derived type is everything about our original programme and create an employee for either print person on the whistle numbers because it has to prime provider of private anger at a grey one of these extensions at staple extension one then that compiles well so when we come to build an extension of the person is built an extension of a person and every derived type out there so does that mean if you can write potentially some quite scary extension of the right extension methods of system object and they re going to appear everywhere suffer it is as if we were to write public static last year an extension that is deleted in is that it matters my extensions with the public static unemployed on here but all it object extension object

Minute 9

Extension I will pass an object all I was wearing a console the right line and I m an extension to object on what would have found is that we should protocol done any object that the likes of us call object extension on this person for a restaurant or recompile so now everyone run that code will find that were actually stepping as we go along year into the object extension to we just extend the system to object and should mean causes we can do things like insecure or five but sales in integer object extension I should compile fine for us and as we set belonging to the programme again were popping into the object extension here on a boxed integer to have extended everything by writing this and object based extension

Minute 10

Now undercover here lets a study of the little that is what access to these extension methods has to the classes that they are extending so for instance previously wrote a person extension and we left in extension to a lame specialist worth a visit extension to an alternative to an employee he extension will call E and I should be fine but not back and will create ourselves an employee here and will call at method on think that a couple of parameters must get rid of those tidy governors were so we felt here is if I does renamed that time called a right to a built in as an extension to employ E

Minute 11

Pockley deny extension friends and family right out employed at first man and the answer of course is yes driver has employed at last name in the SEM Cinema yesterday that the question is can we write as employee employee ID and the answer to that is no and essentially reason why is that the extension method only get access to the public properties and methods and so on of the type that you re extending so this isn t some kind of weird friend access or anything like that listening audience to see a unique public face of the class that you re extending such like a good thing we don t get some kind of hidden access into a type that you really shouldn t have anything to say that if the latter working tirelessly to we can rebuild a case we ve now got an extension to employee open person and I forgot that raincoat now and on to the

Minute 12

Type we had a new method public void print person well glad to present a one Person you wish what we call we end up calling the member method so it s important to realise that extensions are only going to be considered if there is undoubtedly a method on the class itself or of a farce because remember him after using an employee and yet were calling and method on person here sales in the base class so if there s a method that is available like that they may not like to go ahead and consider extension methods in a case which has gone to call the real method on the real time soap and member methods if you are a member functions get priority from the compiler over any extension of the jungle looking for extensions at

Minute 13

After you will notice if we do other parameter to this to say that printers with one writer then once again this will resolve the extension method because there is no method in the class itself employee or person that matches the signature Karadzic article and is well lasting about this resolution noticed that if we had a print person here which matched and yet was private or protected then once again what will happen is London calling the extension method set out in the compilers look into methods of term match if you like in terms of signature and who doesn t finalise prepared to go off in search extension methods and dealers that point where we bring in the incident name spaces we started looking from methods actually match like extension method back here in them theextension to thought everything in this session

Minute 14

An extension method with his ideas illusionary of appearing to add methods to apply and all the types that are derived from outside the extension that you might have a special signature at its first parameter has another keywords this on it and that extension method only has access to the public methods of the type and derived types of the extension applies to seek start tinkering with private state and so on the compiler of this sort is lovely and there will choose methods on the type in preference to methods of extensions so if you ve got the method flew on your class and you call through we will call at member function unless it is not visible to the caller in which case for the looking extension methods in the unskilled name spaces and will try and find a flu that matches the signature that you looking for working over more information at each version 3 and 1

Minute 15

Great enquiry well as a site of MSDN link is there on the screen also if you follow that link you can find at the bottom that page privacy sharp version 3 language specifications and standards that we should give you all the information you need on what s happening at each other century and if you want feedback and something in a specific little video it only I m just might not hold the Microsoft com or the likes of listening and will catch you next time you pick up one of his videos

Click here to go to original video page

No comments: