‫And this video, we are going to look at the parents keyword that you might have come across or maybe
‫not, but it's definitely something that is very useful when programming and it's going to be very important
‫for your arsenal of features that you can control in C-sharp development.
‫So let's look at it.
‫The thing is, we have used methods that use params already, and I can tell you that because we have
‫used right line quite a bit and right line, as you see here, says hello world.
‫Well, the cool thing is if you check out the right line like so you hover over it and then you can
‫see that it has multiple overloads.
‫So basically let me type it in here once again, right line like so.
‫And then once I open the bracket, it shows that there are 18 different overloads.
‫Overloads are basically different kind of implementations for this method.
‫So there is not just one implementation for this method inside of the console class, but there are
‫multiple ones.
‫So you can check it out here by clicking into it.
‫You can see there are a bunch of right line implementations here.
‫So the simple one, then the one that takes a boolean value, the one that takes the buffer for well,
‫basically a set of characters and so forth.
‫So there are a bunch of different overloads that this bright line has.
‫And the one that I am referring to in particular is going to be the one that we have here.
‫So it's the 15th one, at least in my version of Visual Studio.
‫This might change depending on when you check it out.
‫And you can see here there is this params keyword, so it says params object array arg.
‫So this means that it will accept as many parameters as you give it and it will accept parameters of
‫type object.
‫Basically, every single class element that you create or every single one, basically objects of anything
‫is going to be of that type object.
‫So even this class program internally is going to inherit from object.
‫We're going to look at inheritance a little later, but basically it just says it accepts any type of
‫information or any type of data.
‫So that's really the cool thing about this particular example, but you don't have to use it in a way
‫that it accepts any type of information.
‫So before we get too complicated, let's just actually see an example.
‫So we saw in this overload that it accepts a string format and then the parameters later.
‫So let's just pass a couple of values to this right line method.
‫So we're just going to use price is and then whatever the value or the first value that we have after
‫this string and a comma that we pass as a parameter is going to be so it will be this position zero.
‫Then PI is 314 which is going to be at this position and ET is and this is going to be this ad sign.
‫So if we run this.
‫You will see that it basically accepted all of our parameters.
‫So it just price is 32, pi is 3.148 is the ADD sign.
‫So we could, of course, also add as many numbers as we wanted or as many parameters as we wanted.
‫So let's have this stupid example where we are just passing the numbers like so, so one plus two plus
‫three and so forth.
‫Basically, these are the values that we have here, but if you want to use them in the string, we
‫need to start with zero.
‫So this will be the zero position, which just means that it will display the one, the second one will
‫display the two, which will be this one here and so forth.
‫So if we run this code, you will see that it still works.
‫So it says one plus two plus three and so forth, until it then gives us a 28.
‫So you see we don't have an eight here.
‫We have a nine.
‫We have done all of those together, which is the 28 here.
‫So you see here we even have a calculation that we're passing to it.
‫So it accepts all kind of stuff.
‫But the important part that we wanted to look at are not the objects, but we are looking at the params
‫keyword.
‫So let's create our own example where we are going to use the params keyword in order to allow as many
‫parameters as the colour of the method wants to throw at us.
‫So let's create a new method, public static.
‫We make it static so that we can use it inside of our main method, or we can call it from our main
‫method and I'm going to return nothing and it will be of type while we will give it a name of params
‫method.
‫And here we're going to use this params keyword.
‫And then it will be, in our case, not an object type, but it will be an array of strings that we
‫can pass to it.
‫And I'm going to call this.
‫Array of strings sentence.
‫So inside of our parents method.
‫Now, what we're going to do is we're going to use a four loop.
‫And I is going to be zero.
‫I is going to be less than the length of the sentence.
‫And then we increment by one.
‫So a plus plus year.
‫Okay.
‫So what is it that I want to do in this loop?
‫Well, basically, I just want to write every single item of that parameters list that was thrown at
‫me.
‫I want to write it onto the console and I can just do it using the console.
‫Thought right statement here.
‫So.
‫Right.
‫Using the sentence at the position of I because sentence, if you look at it is an array, it's an array
‫of strings.
‫So the position I will at the beginning be zero.
‫So whatever the first parameter is that was given to me as something that I'm going to write at this
‫point and then I will add an empty space like so.
‫And go to the next iteration of the for loop.
‫So this for loop now will iterate as many times as we pass parameters to this params method.
‫So let's look at it in our main method.
‫Let's call this method actually.
‫So here I'm just going to call this params method with a super simple or stupid sentence, but the sentence
‫will be broken down into individual words.
‫So I'm just going to say this is a long string.
‫I have no idea when it's going to end.
‫Dot, dot, dot.
‫And we could go to keep going at this point.
‫But basically we're just passing one string, another string, another string and so forth.
‫We're passing a bunch of strings here.
‫So let's run this application.
‫And we can see this is a long string.
‫I have no idea when it's going to end.
‫We could now change this at any time.
‫So let's do it.
‫Let's run it again.
‫And now you see, we have crippled our sentence.
‫This is a no idea when it's going to end, but the code still works.
‫So the sperms keyboard basically just allows us to pass as many parameters as we want.
‫We could have also passed no parameters at all.
‫So let's pass no parameters.
‫And we will see that nothing happens.
‫But our code doesn't crash either.
‫Okay.
‫So that's the cool thing about this params keyword.
‫Now let's create a method that will allow objects as well.
‫So let's create another params method.
‫This one will be parents method two and it will not take strings, but it will just take objects and
‫we call it stuff.
‫So whatever stuff we give to it, what we're going to do now is we're going to use this for each loop
‫which will go through the stuff that we pass to it, which is an array of objects.
‫And for every single object, it's going to put that object into the right statement here.
‫Now what?
‫The cool thing is, every single class has internally a method called to string.
‫So every single class, no matter which class you create it internally, has this method or is inheriting
‫from object, which means that it has this method called.
‫To string.
‫So that means that whatever object you give to our method, it will be able to make a string out of
‫it.
‫Even if the string will just be something like this is a class or this is the class called so-and-so.
‫So it might not be any useful information that you get there, but it will at least make it a string
‫and the application will not crash.
‫So as I said, inheritance is something we are going to cover later on.
‫Basically, it's the relationship between different classes and one class is going to be the parent.
‫And then you have well, you can have multiple children to this class and basically the parent has a
‫certain feature set for certain functions and members in general and the inheriting classes, they will
‫have the same features as the parent, so the same members as the parent, but they can extend later
‫on.
‫But as I said, we're going to cover this in depth in a separate chapter.
‫So here, if we run this well, nothing happens.
‫So let's actually make this run.
‫Let's call the parents method too, with some cool values that we're going to pass to it.
‫Therefore, I'm going to pass it some stupid values.
‫Let's just do it like this.
‫Okay, so here I have an integer which I will call price, and I'm giving it the value of 50.
‫Then I'm passing a float which will be my pi.
‫Then I have a character which will be the ad sign.
‫Then I have a string called book, which will be The Hobbit.
‫And what I do now is I just go ahead and use this method to which accept it a parameters list of objects,
‫however many it is, and we're just passing four because we decided to.
‫We could have passed three or seven or however many we wanted, and now we're just passing all of those
‫different variables.
‫So we are passing the price high at and book.
‫And if we run this now.
‫We will see that it says 53.14 AD and The Hobbit.
‫And then and the second line, what we call Paramount's method method two.
‫Once again, we are just passing values directly.
‫So we're passing a string here, a 55.3 and then a character dollar sign.
‫So a character, it is because we're using the single quotes here.
‫Or basically the apostrophes.
‫So now you might wonder, why would I care about this feature?
‫Because you gave me some examples here, Dennis, but they're not really useful to me, so why would
‫I care?
‫Because I can define specifically how many parameter, how many parameters I want to pass to you, and
‫then I can be very specific about it.
‫Well, the thing is, there are plenty of applications where that's not the case, where you will actually
‫need to make sure that you allow the freedom of passing multiple different values.
‫And we're going to look at it well at in a specific example in the next video.
‫So see you there.