Overloading is a functionality that you can utilize in a number of different places in C#, especially as an outgrowth of the Object Orientated aspects of C Sharp. And you really need to understand this because it's going to come into play in a lot of different ways that you're going to see later on when we get into some code examples. Now Inheritance can produce a lot of options in the number of available methods in our classes or functions in our classes that become methods in our objects when we instantiate them. Think about this in this little model we were using before in some different videos. We had the Animal Class and the dog inherited from animal and collie inherited from dog, then we did the same similar thing on the cat side. We could have, say if we had, 10 methods in each one of these classes that's 50 methods. Well think about this, there maybe instances where for example a Feed Method, we could have a generic method for feeding the animal but then feeding a dog could be a little different from feeding just generic animal. So we want to a Feed Method so that when we need to call it, it would only apply to the dog and then a Feed Method that when we call it would only apply to the collie, same thing for cats. Well how could we pull that off? Well the way we do that is to simply name the methods the same thing and I know what you're thinking, now wait a minute how can you distinguish them? I'll show you in just a second. So we can have Method Names that are duplicated in Child Classes and each method may be required in different locations in our program based on the functionality that is needed. And so while we have Multiple Methods with the same name, we may want to call, say the method from the Collie Class instead of the method from the Dog Class or the method from the Dog Class instead of the Animal or the Collie Class. Alright. So we already know methods can share names but here's the catch, only if they have different Signatures. So let's go look at what we're talking about here. Overloading is where we use different Signatures for the same Method Name. Now a Signature is the name of the method, this is a Do This Method and the number of parameters that get passed in and the type of parameters. Okay. So this particular method called Do This is a public and it's going to return an integer, but notice it's Do This and it takes an integer of A. Now I can have another method called Do This, notice the same name as this but this one takes 2 parameters, one's an integer A, one's an integer B. So here's the deal, I'm overloading the Do This Function here or the Do This Method. If I call Do This and pass it 1 single number it's going to run this one. Now obviously there would be some code under here. Right. If I call Do This and pass it 2 integers, it's going to run this one. Now this one could be in one Class, this one could be another Class somewhere else and it would automatically know to go pick that one up and run it. This gives us the ability to produce a lot of different options and the number of available methods and yet make it very, very easy to pinpoint exactly what functionality we want to run, just based on what we pass to the method. And we'll look at some, a little bit more complicated examples of this a little bit later on in the course, but that is functionally what Overloading is. It is creating Multiple Methods and Multiple Functions with the same name, in this instance, its Do This. But the Signature is different in that they take different numbers of and different Data Types of parameters that are passed in. And of course, also known as arguments. Alright. So that's the basics on Overloading.
| Course: | Microsoft C# 2010 |
| Author: | Mark Long |
| SKU: | 34306 |
| ISBN: | 978-1-61866-037-4 |
| Release Date: | 2012-03-19 |
| Duration: | 8.5 hrs / 105 lessons |
| Work Files: |
Yes |
| Captions: | No |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |