Exam 70-483: Programming with C# - Objective 2.4 Create and implement a class hierarchy

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome to objective two point four create and implement a class hierarchy today's objectives cover design and implement interfaces create and use base classes and use some of the standard dotnet framework interfaces so the first section that the book starts with is like I said designing and implementing interfaces but I quickly want to just go over the idea of what inheritance kind of brings to the table because is what we're really talking about and in the most general way I just want to say that you know inheritance allows us to establish the the idea of and is a relationship and when we use inheritance and create is a relationships basically what that's saying or doing is that whatever class uh that whatever derived class that is derived from a base class is essentially the base class so what I mean by that is put it in perspective if we have a say in animal class and then a dog class that derives from that animal class that means a dog is a animal and that's where the is comes from so essentially in that kind of situation anywhere that I had an animal maybe it's a parameter for a function or or something else I could replace that with a dog because a dog is an animal because the dog inherits all of the characteristics and traits of the animal thus logically we can think of it as the dog is all animal so that's where the is a relationship comes into play okay so now with that aside let's go ahead and jump right into designing and implementing interfaces so an interface is basically a guideline for some class or struct to follow so for example if I create an interface let's say I test my interface is called I test by the way there is a convention in c-sharp that naming interfaces you should only start with the capital I so that you can just quickly tell that the thing is an interface so let's say we had an example of an and interface called i test i test would contain function prototypes or just the signature of a function so let's say we had a function set that said get result and return an integer would you say int get results and then it's pretty much semicolon and that's just the function header and that goes into the interface and any class that I attach this interface to has to follow those guidelines meaning that that class must implement a function called get result that returns an integer it's pretty much a guideline or a contract and I'll talk about that idea of the contract in a little bit but yeah you can think of the interface as a contract it's what anything that implements an interface must follow the guidelines of the interface so let's go ahead and just create an example of this so let's go ahead and create let's go to my solution Explorer I'm going to create an interface so I go to new item and I'm going to create an interface and I'm going to call this interface example inside my interface I'm gonna list the the possible things that we could put into an interface which are functions properties events and indexers so I'm going to create a function get results which turn the string int value which is a property event event handler Zylstra tree which is a event and then an index gets so this is our basic interface and for members that are in this interface now if I create a class and that implements this interface that means that that class is required to implement all the requirements of this interface so all four of these members so let's go ahead and create a class that will do this add class and I'll call it example implementation so now in my example notation I'm going to implement the I example input interface right there I example now because I implemented this if I try to compile and run this you see right away we're getting errors it's saying that our example implementation class does not implement the interface member get result etc except et cetera so basically what it's saying is it's you know this is breaking the contract so I can just right click on I example and hit implement interface and this is just a shortcut there's you can implement the interface implicitly or explicitly I'm sure we'll go over explicit actually I did I already do that in a later time in a previous lesson I don't know we're going to implement the interface regularly and by doing that it creates all the requirements that we need to satisfy this interface but for things that require us to actually put some code into it it automatically just throws and not implemented exception so we have to write the implementation for get results value and index we would have to write all this implementation and just for some sample data I could just write return result for this one for our value we'll just make it into an auto implemented like that and then for this string index I don't know I'm just I'm looking at the book right now is what the books doing and they just return 42 basically doesn't really matter essentially what they're trying to show you is that now we implemented all the requirements of this interface called I example which we're going to go a lot more into interfaces it's just a quick introduction now if I try to compile this I still have an error what is the error Oh set I need to sit okay so that does nothing if I compile this as you can see we get the rebuild all succeeded so this compiled fine because this class is satisfying all the requirements of I example this is our contract I guess you could say um another note in the interface definition you do not specify access modifiers on any member because by default all members are public so we do not specify an access modifier okay another note I want to point out is that interfaces can inherit I guess you could say from other interfaces so if I have two interfaces and say I example and then we have another interface I test if example is implementing or inheriting from I test that means that anything that implements I example will not have to implement both so what that looks like if I have another interface called I test and this just has I don't know void a do test I don't know some kind of function in it if I make the example inherit that like this now in my example implementation even though I'm just implementing I example I example inherits from I test so that means that now I example also has do test as a requirement and we didn't implement that so if I try to compile that now we get the error I can right click implement it again and we'll get the implementation for it I mean I'm in the definition for it and then we have to supply the implementation but the error will go away so yes you can stack up interfaces making them inherit from each other okay let's go ahead and remove this and we're going to create a different example real quick okay what I'm gonna change this around but so basically in interface one watt or one day the thing to think about interface is that it interface cannot be instantiated because it interface has no implementation and interface is simply just a definition of what needs to be implemented if you implement this interface but the interface itself cannot be instantiated so example meaning I can't go I example a equals new example example I cannot do that because it doesn't have any implementation however this does not mean that we can't have any interface variables now if we if we instantiate an object that implements an interface any interface variable could hold a reference to that object so so let's go ahead and create a let's change this interface to I animal instead of I example and I animal it's going to have one required function void move inside of example invitation we're going to rename this as dog going to implement I animal so implement that we had to implement the interface which is public a void move and I don't know it's not gonna really do anything I don't know let's go counsel about right line move but I'm also going to add another function is on top of that I'm gonna say public void bark because you know talking bark and this will just go counselor right lion bark but in the side the interface I did not specify a bark function so now in my program when I go ahead and say dog D equals new dog if I did this you know this would work as expected dog bark dog that's move I run that we get the bark and move but I said I wanted to you know use a variable of an interface so if I go I don't know I what is it I animal let's say D equals new dog what that is perfectly fine what what this line is doing it's saying I animal D which is my variable is pointing to some kind of dog object implementation this dog object is floating in the heap right now and we're pointing to it essentially now the tricky part comes if I try to go D dot you can see that we have the move function and I could run the move function but we do not have the bark function although I animal D is pointing to a dog object so there is a bark function there somewhere we just do not see it because we're looking at the dog object through the eyes of this eye animal interface and in the eyes of the eye animal interface we only have a function called void move so the only function that this variable is comfortable with accessing is the move function because it's it's positive that it has that because a dog implements I an animal it's a hundred percent positive that that dog has this move function so when I'm looking at dog through the eyes of the eye animal interface I can access that now like I said before even with this behind the scenes eye animal is still pointing to a dog so it is a dog so if we mess around with some casting we could soap extract that bark function somehow out of this and still call bark now I think I went over this in objective two point two I'm pretty sure so you can check that out if you want to figure out how to do some casting and stuff to manipulate this okay let's go ahead and create another up implementation and this one will be classic cat and it implements I animal and we're going to implement the same function except we're just going to specify that the dog move and cat move so we know which one's going on so another example that where we can use an interface is in a function parameter so if I had it for a function as a static void move animal that took in an eye animal variable or any object that implements eye animal now if because I have a dog and a cat both of those implements so I could pass them in so I can know cat C equals new cat and then in hammer go animal dot move essentially I want to remove that from there so in this example I'm just going to go move animal passing in D and then move animal passing in C and because of this we pretty much created a very generic function this this function then this is one benefit of using interface our interfaces this function says I require an eye animal interface and because of that I can pass in a cat a dog a lion anything that implements this interface called AI animal can be passed into here because ever all these animals implement I animal so it's pipes it gets guarantee that it knows that it has the move function so it all works if I run this we'll see dog move and cat move and this is where we're sort of seeing this really idea of this interface as being a contract and and this is a pretty important concept in programming and it's basically what you want to do is you want to normally you want to try to program against contracts rather than the actual implementations the interface guarantees that certain functionality is available but you don't really care how its implemented behind the scenes a really good example of this being used is in WCF windows communication foundation which I'm going to be doing actually a video series on soon and basically with window communication foundation allows you to create applications that have clients and and you can think of services or clients and servers and basically allows you to write applications that you know your client can interact with your server then your server can send information back to your clients and information to other clients and things like that and how that works kind of behind the scenes that well there's a couple ways but one way of doing it is using this or it uses this idea of contracts or service contracts and basically with with this service contract the clients pretty much has access to this contract but not the actual implementation of functions that lie on the service so when a client makes a function call to the service saying get value or something to the service it doesn't actually know what code that the service is using to get this value has no idea that the client is only using the service contract in order to know that it has a function that exists that's called get value the contract ensures that that function exists on the service but it doesn't insure anything or tell the client anything about how it's implemented it has no idea it just knows that what its methods signature essentially so that's what contracts are and that's really a good way to program ah another side note about inheritance c-sharp does not support multiple inheritance however like there are some language like C++ is a perfect example that supports multiple inheritance but in c-sharp it does not support multiple inheritance meaning you cannot you cannot derive from more than one base class however c-sharp does offer multiple multiple interface inheritance and yeah and basically with multiple interfaces inheritance you can sort of get around and do everything the same way ok so like with what interfaces like we were saying interfaces only define the public signature so deriving from an interface or implementing interface you don't actually get any implementation it's just telling you what you have to implement so if you implement an interface you have to invite the implementation for everything that is in that interface if you want to inherit it and derive from something and get implementations rather than having to write your own this is where you'll use inheritance that's inheriting from a base class ok so let's go ahead and let's delete this stuff I should I just delete the both V okay so for an example of creating a base class and inheriting from a base class I'm going to use the example from the book although I mean I don't think it's the best way to show you know this idea of basic inheritance you know it's trying to make it a little bit more complex level so I'm gonna try to explain it the best way I can so everyone understands what's going on inside of this example so I'm just going to first you know write out their example run it show you what happens or it doesn't even run I don't even think we yeah for the most part it doesn't even really run I mean they left out implementations in it so I'm just gonna write the code and then explain what would happen and try just try to explain this part of it because I don't know I just don't like this and it's also another part in upcoming in this lesson that I don't like again from this author which I'll point out obviously because it's what's in the book is actually wrong yeah so let's go have my go ahead and write this all out and then explain it okay so I just typed the code for you guys and now I just want to go over it before I start I just want to point out that you know the main point of this is just to emphasize a couple things the first thing is well they the author use a generic example and because of this is generic anything that implements I entity could essentially use this okay that's that's great and also but at the main point there they're trying to do this is that they're trying to emphasize that inheritance can help you reuse your code and while you're well if you want to you can reuse old code add new code anything this is just a basic example of a basic class hierarchy now as I go over this I'm gonna make my own simple hierarchy and then we'll move forward um will be pretty basic the one I do so to just quickly look at this I'm not gonna go into too much detail I'm just want it just so maybe clear up some of the issues that you guys are having with it I don't like this example I don't know why it's here basically so an interface is defined called AI entity and the only requirement of this is that you implement improperly called ID of type integer okay then we have our base class which is repository which is a generic class we can see by this and on the generic class there's a constraint specified this is a constraint and it's specifying that any generic or any object that's passed into this to the generic property must be or must implement a entity that's the constraint right there to ensure that everything goes into here is only AI entity they have a protected i innumerable generic variable called underscore elements and these would be the elements of whatever's passed into the repository you can see in the constructor the the parameter argument for the constructor is that it has to be either passing an ienumerable of t elements and essentially that is anything that implements an ienumerable interface and that is all collection types arrays lists so on any kind of clash type it could even be a custom thing that you implement as long as you implement this interface so that's been passed in and then stored into this protected variable protected if you didn't know I think I went over this in one these lessons it means that it's private to everyone besides the it's all the only things I concede are the class itself and derived classes then we have a driven our another function that returns T which is just whatever is passed in something that T is essentially anything that implements I entity and then it does a find by ID we type an ID and this returns essentially what this is saying return the currents that meets this it wants a query what is it so do condition and you're pretty much is saying whatever enters in if if an element matches that ID of whatever is passed into that just returned the elements is what this is saying so that's fine by ID so basically here's a way that we can search and find by ID of this repository now we have a class called order which implements our I entity so this order can be used in our repository so in our order repository this is just a class now the IMP this is then the class that's inheriting from the base class so once it inherits the base class which is right here it gets all the characteristics of this base class so it has this fine by ID function that's built into it but on top of that they added this other function called filter orders on a mount so we could we can filter the orders on a mount or we could filter orders on ID depending if there was amounts in here but it was omitted in the code the amount should go here somewhere so we can filter on amount or ID that's basically what's going on here this is the constructor call and this is calling the base constructor passing in whatever is passed into this order repository okay so that's all I want to talk about this I will I don't really care for this example I'm going to go ahead and write my own example with you guys very basically a really basic example to show you you know just quickly explain what base classes are inheriting from base classes what happens etc as fast as I can so let's go ahead and create a little animal hierarchy so we're going to create a class I will just put all learn to put all our classes into one class file see s file so um I'll call it animal but I'm gonna put everything into here so your class called animal and our animal is going to basically have let's see an up C public string name it will have a name public int age so these are two things that our our animal has it maybe will make it public void move it has the function that it can move this is our animal base class is our base class now an another class called dog which implements or an error inherits animal now by doing that dog has all the characteristics of an animal a dog is a animal now but on top of that I'm going to add in some other things I'm gonna actually what am I doing with this move I have to implement it let's go ahead and counsel that ready line new animal dot move animal dot move so in our dog now we could add something else I'm gonna say public void bar and here council dot readline dog bark so now I have an animal and then a dog my dog is my derived class of my animal so let's go ahead in my program if I create an animal animal a equals a new animal my animal can has an age a name and it can move that makes sense so let's go ahead and run that and we can see animal dot move is despite if I create a dog dog giggles new dog D dots we can see we have aged bark move and Nate even though in the class definition for dog you know age name and move were programmed in here but because it's implement or it's inheriting from animal and it inherits all the characteristics of animal because it creates this is a relationship a dog is a animal now this is the same thing that we did before with the interfaces when a class what's an interface it it had to import the interface kind of forced it to implement its own implementation of the interface this is sort of the same way but now we get to kind of derive the implementations we get that we get to have the implementations of the animal class in our dog class so yeah I have dog dot I could do all the things now anywhere I see a function static void move animal taking in an animal a anywhere I can see this I could do okay a dot move right but I could pass in my dog to this function because my dog is a type of animal so when I run that now we have animal dot move animal dot move twice because I called move twice yeah so I could pass in my dog to this animal or this function that requires an animal because a dog is an animal so this is a basic hierarchy with animal and dog this is a base class derived class ok let's talk about changing behavior with this kind of setup now so when we're building a class hierarchy we sometimes will want to you know not only extend the behaviors of the base class but we might want to replace them and modify them to make you know maybe our derived class more specific so for example right now if I go let's remove this if I go dog D equals new dog d dot move but I do this I get an old dot move even though my dog even though it's dog it's saying animal don't move it's not specific enough for me it should be able to say Adam or dog dot move so I can override this move function from the animal class to specify my own behavior for this specific instance adult so to do that I would mark my move function as virtual and by marking it as virtual this it now allows me to override this function in any derived class okay public override we can see I can override a move now and now I could provide a different kind of behavior for it now I can say console dot write line dog dot move now basically what's happening is when I run this code now it's going to say dog dot move because I'm I'm calling the dog version of this basically it's when you override the function of a base class it uses the the override adverse the derived class and this allows you to just make it more specific for your class because so I don't want to say animal Dom if I wanted to say dog move so I override this move and now I can write my own behavior now what if I just wanted to you know keep the keep the old one but then just add to it well I could do that also I could just in that case a base dot move and by doing that I'm saying okay override move but still do the same move that it does now so is based on move or base class that move so it calls this one and then it does my own custom behavior that I added so now when I run this now I'll have animal that move and dog that move like this animal dot move dog dot move yeah so by using virtual and override I can change the behavior of a virtual function in the base class in the derived class to maybe add things to it to completely remove it to something different and things like that okay so if a base class doesn't declare a method as virtual a derived class cannot override the method so you can only override functions or methods that were defined defined as virtual now this leads me into listing 2-4 eight inside of this chapter I you know I've looked at this couple times and it's completely wrong what they're saying in this - - it - ash for you unless I'm wrong and if I have someone correct me but I learned program in completely different way apparently but I'm pretty sure they're wrong now you read what they're doing here I'm going to read essentially the paragraph above doesn't that's true - for a to show you what they're trying to do so it says if a base class doesn't declare a method as virtual derived class cannot override the method it can however use the new keyword which explicitly hides the member from a base class this can cause some tricky situations so what they're saying is we can use the new keyword which explicitly hides that member using the new keyword on from the base class and pretty much that's incorrect that's not true at all um unless I'm wrong which I said someone corrected me using the new keyword is used for hiding a method they are correct with that but it doesn't hide the the function that that or as they say it doesn't hide the member from the base class it hides the base class function um rather so I'm going to go ahead and write their example and then show you what I'm talking about so in their example they have a class called base base inside a base public void if a function called execute which just writes to the console saying base dot execute that's the execute of the base then we have class derived which implements or inherits from base and derive just says public new void execute so this is where the new operator is coming in and now like I said when you use new like this this hides type what what it what am i doing console that right line drive that's why I get for trying to copy and speak drive that execute okay now when you use the new clear that I said what really happens is it hides the base class implementation so that you can use a function with the same name that's in the base class and I'll show what I mean by that so that's that so let's go ahead and create or inside of my main let's go base B this is what they did by I'm just copying what they did right now based B to execute B equals new derived and then they do B that executes again they run this and we get base that execute based execute and from what they did they're saying okay look we got based on execute two times even though the second time we create a new memory means that the beads equal to a new object called derived so that it should call on this execute arrive at X cube so they're saying the new keyword because of that it doesn't get that output they're saying a new keyword is causing it to hide the member from the base class so they're saying the new keyword is hiding this function from the base class that's what they're saying which is not correct and maybe I'm reading this wrong like I said if someone has the book look at listing 2 - 4 8 on page 130 and you can tell me what they think about this but basically the reason why they're getting this base not execute to display twice it's nothing to do with the new keyword it has to do with because when we say base B equals new base B to execute that's normal but when we say B equals new derived yes we're allocating memory for this new object that's of type derived but we are looking at that object through the eyes of a base class so through the eyes of a base class we're looking at it as as a base so we only see this base function which is based on execute so because that the base of execute function is being called and that that's why the bait that's why we're getting a two x if mean if we can't say derive you got it and then did B to execute we would get the derived execute like we properly would but it's because we're accessing it through umm the eyes of the base class now to prove my point let's go ahead and just mess around with this and if I remove this recompile around I was good morning Randy and I highlight this warning the warning says uh without so this is without the new keyword lesson - - for derive dot execute which is this function hides inherited member based execute use the knee the new keyword if hiding was intended so look look what it's saying it's saying the derive dot execute is hiding the inherited member base that execute derived is hiding base now listen to this the book says the new keyword or whatever which explicitly hides the member from the base so this is saying the book says hide member from base this is saying hides heids bass from uh hi it's base from derive so yeah this is saying it's hot it's hiding lean the bass from the drive so pretty much what saying is because if you have a function in the base class it's called execute and a function the derived class it's called execute it's going to use the one in the derived class the one in derived class is hiding them in the base class it's not going to see the one in the in the in the base class and to prove that if I go derive D equals new D or I D that executes as you can see we get derived dot execute so this is what it's doing and this is not a problem it's just it's a shino-san warning and it's saying that we should just specify it as new it does not change anything you just mix the warning go away uh now when I run it again I'm just looking to get the same exact result to arrive dot execute except the warning just went away so even with this like I said we get the the correct behavior now we're going to derive dot execute rather than the base out execute because we're accessing it through a variable type to ride and if I had base B goes new base then B equals new derived I don't know why I did that I couldn't face B equals new to ride but B I execute I'm going to get the base execute because I'm looking at it through the eyes of base it only sees the the base function essentially what's happening so yeah that's what new does so the book I don't know correct me if I'm wrong below the book is wrong once again the book says using the new keyword which explicitly hides the member from a base class so saying it's hi it's saying it's hiding this from the base class what's not true this is hiding the base function from the derived class it's hiding it so yeah they said take a look at that so yeah that this is the correct way using the new keyword hides the base class function so that you can use a function with the same name that's in the base class if it's not virtual so if you if you want to create if you don't have a very even if the function the base class is not virtual and you want a function with the same name you can mark it as new and it will just hide the base when I will use the derived one that's what's the new keyword is really for but even if you don't put a new keyword it will still work the same way but you'll just it will issue a warning to you that's it so yeah this is the correct way to do it the book is wrong listen listening to that for eight is not correct okay so moving on so let another feature or a couple features that we have with this kind of stuff we have we can mark classes as abstract and we can mark classes as sealed um so basically if we don't want to allow a base class to be instantiated meaning that the base class is only there to be used as a base class it's never there to be used as on its own so an abstract class cannot be instantiated it can only be used as a base class you can declare an abstract class this is what the book said an abstract class can implement or have implementation code for members and they can also not have them so it's not required and you'll you'll see what I mean by that right now so let's go ahead and create an asteroid base class I want to mark this as abstract and inside I'm going to have two function definitions the first one I haven't said I'm going to say public virtual void method with M implementation and it does some saw something there whatever public now this one this function on a mark as abstract abstract method and notice how I do not supply an implementation for this so if I have an abstract class if I have a function in it that's marked virtual that's not actually okay yeah if I have a function that has an implementation that's fine but if you have a function that's marked as abstract you do not supply the implementation rather a derived class has to implement it itself so by doing this it's an basically a nice way way that we can share both um we can pretty much share an interface and implementations so basically what this is saying is the derived class well what's a good way to say the abstract class is acting as an interface with the regards to that it has an attractive function meaning that the derived class must implement this abstract function it must implement it but if you supply a function with an implementation then the derived class will just inherit that and have it um so in this I'm just gonna I can't do that with that I'm just going to implement this public override Void abstract method thrown okay where that's fine so based if I compile is this should yeah okay it compiled fine so basically if you have an abstract function you have to override it or implement it and this is just the syntax for we have to implement it in the derived class so we override it in the derived class and we didn't override this one because the implementation is given here and then we just we inherit all those that we inherit the implementation for the derived class and then just on the side that I just want to note that a sealed class anymore class as sealed it just prevents it from being derived from so an abstract class cannot be instantiated a sealed class cannot be derived from strux are implicitly sealed so it's never possible to inherit from a struck so a good exam tips giving is make sure that you know the difference between an interface and an abstract class an interface has no implementation code an abstract class can choose to implement methods or leave it to the derived class so it's saying that an abstract class can implement methods or not implement method and force the derived class to implement them like we see here by overriding it okay so the last section of this lesson is implementing standard net framework interfaces I'm gonna try to go through this pretty quickly because I don't know you guys look this up this isn't it's pretty important but I don't know so Donette has interfaces that we can use on our custom to find types that we create and basically the benefit of using the.net standard interfaces is that we can use once we our classes implement them there are classes can be used in the infrastructures that dotnet framework offers and why me what it means by that is for example the first interface is I comparable and by making a class that implements I comparable it allows that class to be used um just to sort on things so we can use the built in list or array sort functions if our class implements I comparable and I'll show you what I mean by that so let's go ahead and create a student let's make a class called student and the student class has a public what does it have public int ID gets it as a public int ID this is what our student class has so if we take this and let's say we create a list of Stu students and then we go students dot add new student ID equals to I D equals 3 not as 50 no let's say 5 3 4 so we have a list of students and our students are in it and the IDs are 5 3 4 respectively if I try to go students that's sort and I want to sort my students we s we got a problem so basically let's sort it now let's display it so for each X in student write line X dot ID so let's see it if it worked and ok and we get a problem so we get to see the error 'invalid operation exception failed to compare two elements in the array at least one object must implement I comparable so this is what it means the book means by that if we implement a dot nets interfaces we can use infrastructures that are built for that are built into the language so this sort function is that infrastructure and we want to sort our things but if you think about it logically if I try to sort these students if I don't tell it how to sort how will it know what to sort if I'm say students that sort you know should it based how should have sorted you know it's just a student has no idea how to sort it so we this specify sort it based off of ID and that's what we need to do by implementing I comparable to our student so if I implement I comparable coming to the generic one so there's generic and non-generic functions and I'm going to implement this interface so I just implemented and that is only one functional way to implement which is compared to and basically what I'm going to do is because is a dotnet or int is built into c-sharp it automatically implements I comparable so I don't have to worry about it so I can take advantage of that to make this work so I'm going to say return this that I be compared to other dot ID and basically what it's doing is ID I mean I mean the compareto function that's built into this class will basically do one thing it will check if it's less than the same value or greater depending what it is over it will return less than zero zero or greater than zero and that will be that's why it's returning an integer value and because of that now our class can be sorted because it's sorting on the IDs it's calling as compared to and it's saying is this greater less than whatever it returns them it does some things behind the scenes and ultimately it can sort them now because I'm telling it to sort on the ID all because I'm implementing this interface I comparable now if I run this look what happens we get that three four five so my students were sorted out basically based off of the ID that I supplied right there so that's I compare Ville okay the next interface is I innumerable and ienumerable is an interface not net that helps you implement the iterator pattern the iterator pattern allows us to access elements in a collection without care matter how exactly it's implemented that's trial jury for another book so basically when we implement ienumerable we that essentially allows us to it's basically implementing an iterator that that allows us to then basically use a for each loop because it allows us to use a for each loop on some kind of data for example let me now let's create this create a class called let's bring let's bring back our student so we have class called student let's create a class called I don't know class room inside of classroom it has a guest in a row I guess we'll do an array called students and there's only three students in our class let's just say students sub zero equals new student so we have a student array and then we have three students one two and three let's let's override to string so that it returns the ID and it prints the ID for each ID de so it prints the ID for each student now to think about this logically let's say I want to use a for each loop with my classroom so I have classroom R equals new classroom so now behind the scenes we have all our three students I want to say for each of our students in our how would I do that basically right now I can't I want to display all the students in our how would I do that or despite all my students in my classroom basically I want to use this for each loop so essentially we need to kind of implement an iterator or some kind of mechanism that kind of sends the values of the array to the caller and by implementing ienumerable we are allowed to do this now and there's a listing listing - 2 - 5 5 which just shows you know basically what the for each statement or for each loop is doing behind the scenes and it has move necks currents and and stuff like that to keep and maintain state and we're going to see that here in a second so basically in our classroom what we need to do is we need to we need to implement this AI innumerable interface so ienumerable of students let's say the generic one and then let's right click implement interface off the bat we get two implementations that are to function we need to implement the first one is a generic getenumerator and the second one is the non generic getenumerator this one is simply just going to call our return it's going to call the generic one so if you if you're using a non generic interface we'll call the generic interface automatically but we still have to implement this generic interface so how we're going to do this is interesting we're going to set up a loop for int I equals 0 as long as I is less than students dot length I plus plus here's a new keyword yield return students sub I so basically actually let's go back to here let's try this now just so you can see how it works a student let's run that and we get the IDs 1 2 3 so we get words we're seeing all the students inside of our classroom by doing this so basically what's happening when we're implementing this ienumerable interface this get enumerator function is called when we enter this for each loop it calls get a numerator and the job of the enumerator is to enumerate or iterate over our internal data structures or our internal array that we have set up it's going to air it over that and call this yield return and this yield return basically allows you to send the value back to the caller so back to the for each loop give it the value and then stop then when it wants its next iteration it will come back to this for loop to get the next value and start off where it left off now it's able to pick up where it left off because this yield keyword does all things behind the scenes by creating that move next function and the current property so that it knows how to maintain state this yield keyword basically allows you to maintain States and it knows where it is it members were left off so that it could pick up right where it left off um so basically if I were to put a breakpoint in here and run this and I were to step through this when the for each loop starch will see will enter the getenumerator it sorry I said of disables taking screenshots with many f11 ah so it enters the enumerator eight enters this for loop we hit the yield return look what happened it goes back to the for each loop it prints that return which was one goes to the two string we couldn't keep on going so then we get to the next iteration of the for each loop it comes back into the enumerator so it remembers its state it remembers where it left off because of this yields word and it picks up where it left off so every iteration of the for each loop it's going back into this um iterator or this yield return and you're getting the value so that that yield is maintaining state so you may be saying okay why not just create a list and put all the values into a listen and return the list so we don't have to use this yield stuff and yes that works but it's just creating more overhead essentially because we're creating that new list which member it has to be managed by the garbage collector and everything so we will list we have a list here we have a list that we're creating and returning and that also being managed for ultimately you know even if we just returned the list when we do the for each on that list you know somewhere behind the scenes it's still doing this yield return to to actually get the data from the class to the caller it has to do this yield return so basically by implementing this yield return and this get enumerator ourselves we can skip the hassle of having to create that overhead with that extra list or something like that okay so that's basically it I'm going to skip over I disposal and I unknown just look them up they're pretty basic things I disposals dealt with cleaning up unmanaged resources and I known is used for comm so that's it for this lesson if I went fast towards the end because this video is getting really long so if you have any questions let me know and I will try to answer them alright thanks for watching
Info
Channel: Jesse Dietrichson
Views: 14,522
Rating: undefined out of 5
Keywords: C# (Programming Language), Programming Language (Literary Genre), Technology, Class, .NET Framework (Programming Language), Interfaces, classes, base class, derived class, Virtual, override, Ienumerable, generics
Id: 92FVIpantyk
Channel Id: undefined
Length: 60min 37sec (3637 seconds)
Published: Thu Feb 20 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.