1 00:00:00,360 --> 00:00:06,570 ‫And this video, we are going to look at the parents keyword that you might have come across or maybe 2 00:00:06,570 --> 00:00:13,410 ‫not, but it's definitely something that is very useful when programming and it's going to be very important 3 00:00:13,410 --> 00:00:18,930 ‫for your arsenal of features that you can control in C-sharp development. 4 00:00:18,930 --> 00:00:20,220 ‫So let's look at it. 5 00:00:20,430 --> 00:00:25,380 ‫The thing is, we have used methods that use params already, and I can tell you that because we have 6 00:00:25,380 --> 00:00:29,820 ‫used right line quite a bit and right line, as you see here, says hello world. 7 00:00:29,820 --> 00:00:34,830 ‫Well, the cool thing is if you check out the right line like so you hover over it and then you can 8 00:00:34,830 --> 00:00:37,290 ‫see that it has multiple overloads. 9 00:00:37,710 --> 00:00:44,250 ‫So basically let me type it in here once again, right line like so. 10 00:00:44,370 --> 00:00:50,700 ‫And then once I open the bracket, it shows that there are 18 different overloads. 11 00:00:50,730 --> 00:00:55,410 ‫Overloads are basically different kind of implementations for this method. 12 00:00:55,410 --> 00:01:00,960 ‫So there is not just one implementation for this method inside of the console class, but there are 13 00:01:00,960 --> 00:01:02,010 ‫multiple ones. 14 00:01:02,310 --> 00:01:05,490 ‫So you can check it out here by clicking into it. 15 00:01:05,530 --> 00:01:09,330 ‫You can see there are a bunch of right line implementations here. 16 00:01:09,330 --> 00:01:16,530 ‫So the simple one, then the one that takes a boolean value, the one that takes the buffer for well, 17 00:01:16,530 --> 00:01:19,320 ‫basically a set of characters and so forth. 18 00:01:19,560 --> 00:01:23,310 ‫So there are a bunch of different overloads that this bright line has. 19 00:01:23,310 --> 00:01:30,300 ‫And the one that I am referring to in particular is going to be the one that we have here. 20 00:01:30,300 --> 00:01:34,320 ‫So it's the 15th one, at least in my version of Visual Studio. 21 00:01:34,320 --> 00:01:36,450 ‫This might change depending on when you check it out. 22 00:01:36,450 --> 00:01:43,110 ‫And you can see here there is this params keyword, so it says params object array arg. 23 00:01:43,380 --> 00:01:51,630 ‫So this means that it will accept as many parameters as you give it and it will accept parameters of 24 00:01:51,630 --> 00:01:52,290 ‫type object. 25 00:01:52,290 --> 00:02:00,600 ‫Basically, every single class element that you create or every single one, basically objects of anything 26 00:02:00,600 --> 00:02:02,850 ‫is going to be of that type object. 27 00:02:02,850 --> 00:02:08,280 ‫So even this class program internally is going to inherit from object. 28 00:02:08,280 --> 00:02:13,680 ‫We're going to look at inheritance a little later, but basically it just says it accepts any type of 29 00:02:13,680 --> 00:02:16,050 ‫information or any type of data. 30 00:02:16,380 --> 00:02:22,320 ‫So that's really the cool thing about this particular example, but you don't have to use it in a way 31 00:02:22,320 --> 00:02:24,510 ‫that it accepts any type of information. 32 00:02:24,510 --> 00:02:28,290 ‫So before we get too complicated, let's just actually see an example. 33 00:02:28,920 --> 00:02:36,030 ‫So we saw in this overload that it accepts a string format and then the parameters later. 34 00:02:36,030 --> 00:02:40,800 ‫So let's just pass a couple of values to this right line method. 35 00:02:40,890 --> 00:02:47,670 ‫So we're just going to use price is and then whatever the value or the first value that we have after 36 00:02:47,670 --> 00:02:55,410 ‫this string and a comma that we pass as a parameter is going to be so it will be this position zero. 37 00:02:55,440 --> 00:03:03,360 ‫Then PI is 314 which is going to be at this position and ET is and this is going to be this ad sign. 38 00:03:04,110 --> 00:03:05,430 ‫So if we run this. 39 00:03:06,230 --> 00:03:11,570 ‫You will see that it basically accepted all of our parameters. 40 00:03:11,570 --> 00:03:16,970 ‫So it just price is 32, pi is 3.148 is the ADD sign. 41 00:03:17,360 --> 00:03:24,550 ‫So we could, of course, also add as many numbers as we wanted or as many parameters as we wanted. 42 00:03:24,560 --> 00:03:31,940 ‫So let's have this stupid example where we are just passing the numbers like so, so one plus two plus 43 00:03:31,940 --> 00:03:33,350 ‫three and so forth. 44 00:03:33,380 --> 00:03:37,490 ‫Basically, these are the values that we have here, but if you want to use them in the string, we 45 00:03:37,490 --> 00:03:39,380 ‫need to start with zero. 46 00:03:39,380 --> 00:03:44,540 ‫So this will be the zero position, which just means that it will display the one, the second one will 47 00:03:44,540 --> 00:03:47,420 ‫display the two, which will be this one here and so forth. 48 00:03:47,420 --> 00:03:51,290 ‫So if we run this code, you will see that it still works. 49 00:03:51,290 --> 00:03:56,360 ‫So it says one plus two plus three and so forth, until it then gives us a 28. 50 00:03:56,360 --> 00:03:58,190 ‫So you see we don't have an eight here. 51 00:03:58,190 --> 00:03:58,880 ‫We have a nine. 52 00:03:58,880 --> 00:04:03,590 ‫We have done all of those together, which is the 28 here. 53 00:04:03,590 --> 00:04:07,010 ‫So you see here we even have a calculation that we're passing to it. 54 00:04:07,010 --> 00:04:08,690 ‫So it accepts all kind of stuff. 55 00:04:08,900 --> 00:04:14,840 ‫But the important part that we wanted to look at are not the objects, but we are looking at the params 56 00:04:14,840 --> 00:04:15,500 ‫keyword. 57 00:04:15,500 --> 00:04:22,430 ‫So let's create our own example where we are going to use the params keyword in order to allow as many 58 00:04:22,430 --> 00:04:27,110 ‫parameters as the colour of the method wants to throw at us. 59 00:04:28,280 --> 00:04:31,820 ‫So let's create a new method, public static. 60 00:04:31,820 --> 00:04:36,110 ‫We make it static so that we can use it inside of our main method, or we can call it from our main 61 00:04:36,110 --> 00:04:43,790 ‫method and I'm going to return nothing and it will be of type while we will give it a name of params 62 00:04:43,790 --> 00:04:44,300 ‫method. 63 00:04:45,080 --> 00:04:47,420 ‫And here we're going to use this params keyword. 64 00:04:48,130 --> 00:04:56,530 ‫And then it will be, in our case, not an object type, but it will be an array of strings that we 65 00:04:56,530 --> 00:04:57,990 ‫can pass to it. 66 00:04:58,000 --> 00:04:59,680 ‫And I'm going to call this. 67 00:05:00,580 --> 00:05:02,250 ‫Array of strings sentence. 68 00:05:03,070 --> 00:05:05,020 ‫So inside of our parents method. 69 00:05:05,020 --> 00:05:08,620 ‫Now, what we're going to do is we're going to use a four loop. 70 00:05:10,490 --> 00:05:13,040 ‫And I is going to be zero. 71 00:05:13,070 --> 00:05:17,870 ‫I is going to be less than the length of the sentence. 72 00:05:18,730 --> 00:05:21,760 ‫And then we increment by one. 73 00:05:21,760 --> 00:05:23,260 ‫So a plus plus year. 74 00:05:24,290 --> 00:05:24,510 ‫Okay. 75 00:05:24,530 --> 00:05:26,360 ‫So what is it that I want to do in this loop? 76 00:05:26,390 --> 00:05:33,800 ‫Well, basically, I just want to write every single item of that parameters list that was thrown at 77 00:05:33,800 --> 00:05:34,220 ‫me. 78 00:05:35,100 --> 00:05:40,770 ‫I want to write it onto the console and I can just do it using the console. 79 00:05:42,900 --> 00:05:44,550 ‫Thought right statement here. 80 00:05:44,760 --> 00:05:45,270 ‫So. 81 00:05:45,270 --> 00:05:45,990 ‫Right. 82 00:05:46,780 --> 00:05:55,090 ‫Using the sentence at the position of I because sentence, if you look at it is an array, it's an array 83 00:05:55,090 --> 00:05:55,930 ‫of strings. 84 00:05:56,200 --> 00:05:58,840 ‫So the position I will at the beginning be zero. 85 00:05:58,840 --> 00:06:05,230 ‫So whatever the first parameter is that was given to me as something that I'm going to write at this 86 00:06:05,230 --> 00:06:09,910 ‫point and then I will add an empty space like so. 87 00:06:10,780 --> 00:06:13,790 ‫And go to the next iteration of the for loop. 88 00:06:13,810 --> 00:06:20,920 ‫So this for loop now will iterate as many times as we pass parameters to this params method. 89 00:06:21,190 --> 00:06:24,370 ‫So let's look at it in our main method. 90 00:06:24,380 --> 00:06:26,050 ‫Let's call this method actually. 91 00:06:26,170 --> 00:06:33,610 ‫So here I'm just going to call this params method with a super simple or stupid sentence, but the sentence 92 00:06:33,610 --> 00:06:35,770 ‫will be broken down into individual words. 93 00:06:36,070 --> 00:06:39,040 ‫So I'm just going to say this is a long string. 94 00:06:39,070 --> 00:06:41,420 ‫I have no idea when it's going to end. 95 00:06:41,440 --> 00:06:42,220 ‫Dot, dot, dot. 96 00:06:42,940 --> 00:06:45,910 ‫And we could go to keep going at this point. 97 00:06:45,910 --> 00:06:51,700 ‫But basically we're just passing one string, another string, another string and so forth. 98 00:06:51,700 --> 00:06:53,290 ‫We're passing a bunch of strings here. 99 00:06:54,100 --> 00:06:55,930 ‫So let's run this application. 100 00:06:56,620 --> 00:06:58,930 ‫And we can see this is a long string. 101 00:06:58,930 --> 00:07:01,030 ‫I have no idea when it's going to end. 102 00:07:01,390 --> 00:07:04,150 ‫We could now change this at any time. 103 00:07:04,150 --> 00:07:05,760 ‫So let's do it. 104 00:07:05,770 --> 00:07:06,850 ‫Let's run it again. 105 00:07:07,360 --> 00:07:10,570 ‫And now you see, we have crippled our sentence. 106 00:07:10,570 --> 00:07:16,540 ‫This is a no idea when it's going to end, but the code still works. 107 00:07:16,540 --> 00:07:21,820 ‫So the sperms keyboard basically just allows us to pass as many parameters as we want. 108 00:07:21,820 --> 00:07:24,730 ‫We could have also passed no parameters at all. 109 00:07:25,420 --> 00:07:28,120 ‫So let's pass no parameters. 110 00:07:29,910 --> 00:07:32,160 ‫And we will see that nothing happens. 111 00:07:32,160 --> 00:07:34,200 ‫But our code doesn't crash either. 112 00:07:35,160 --> 00:07:35,300 ‫Okay. 113 00:07:35,370 --> 00:07:37,530 ‫So that's the cool thing about this params keyword. 114 00:07:39,420 --> 00:07:44,990 ‫Now let's create a method that will allow objects as well. 115 00:07:45,000 --> 00:07:47,280 ‫So let's create another params method. 116 00:07:47,670 --> 00:07:53,430 ‫This one will be parents method two and it will not take strings, but it will just take objects and 117 00:07:53,430 --> 00:07:54,510 ‫we call it stuff. 118 00:07:54,510 --> 00:07:59,430 ‫So whatever stuff we give to it, what we're going to do now is we're going to use this for each loop 119 00:07:59,430 --> 00:08:05,160 ‫which will go through the stuff that we pass to it, which is an array of objects. 120 00:08:05,580 --> 00:08:12,750 ‫And for every single object, it's going to put that object into the right statement here. 121 00:08:13,110 --> 00:08:14,550 ‫Now what? 122 00:08:14,550 --> 00:08:22,680 ‫The cool thing is, every single class has internally a method called to string. 123 00:08:23,220 --> 00:08:29,460 ‫So every single class, no matter which class you create it internally, has this method or is inheriting 124 00:08:29,460 --> 00:08:32,400 ‫from object, which means that it has this method called. 125 00:08:33,080 --> 00:08:34,060 ‫To string. 126 00:08:34,070 --> 00:08:40,580 ‫So that means that whatever object you give to our method, it will be able to make a string out of 127 00:08:40,580 --> 00:08:41,030 ‫it. 128 00:08:41,060 --> 00:08:48,230 ‫Even if the string will just be something like this is a class or this is the class called so-and-so. 129 00:08:48,890 --> 00:08:54,440 ‫So it might not be any useful information that you get there, but it will at least make it a string 130 00:08:54,440 --> 00:08:56,390 ‫and the application will not crash. 131 00:08:57,020 --> 00:09:00,890 ‫So as I said, inheritance is something we are going to cover later on. 132 00:09:01,100 --> 00:09:08,360 ‫Basically, it's the relationship between different classes and one class is going to be the parent. 133 00:09:08,360 --> 00:09:15,530 ‫And then you have well, you can have multiple children to this class and basically the parent has a 134 00:09:15,530 --> 00:09:25,790 ‫certain feature set for certain functions and members in general and the inheriting classes, they will 135 00:09:25,790 --> 00:09:31,610 ‫have the same features as the parent, so the same members as the parent, but they can extend later 136 00:09:31,610 --> 00:09:31,880 ‫on. 137 00:09:31,880 --> 00:09:35,450 ‫But as I said, we're going to cover this in depth in a separate chapter. 138 00:09:35,990 --> 00:09:39,710 ‫So here, if we run this well, nothing happens. 139 00:09:39,710 --> 00:09:42,080 ‫So let's actually make this run. 140 00:09:42,080 --> 00:09:47,240 ‫Let's call the parents method too, with some cool values that we're going to pass to it. 141 00:09:47,430 --> 00:09:51,590 ‫Therefore, I'm going to pass it some stupid values. 142 00:09:51,590 --> 00:09:52,880 ‫Let's just do it like this. 143 00:09:52,880 --> 00:09:58,700 ‫Okay, so here I have an integer which I will call price, and I'm giving it the value of 50. 144 00:09:58,700 --> 00:10:02,000 ‫Then I'm passing a float which will be my pi. 145 00:10:02,030 --> 00:10:04,250 ‫Then I have a character which will be the ad sign. 146 00:10:04,250 --> 00:10:06,980 ‫Then I have a string called book, which will be The Hobbit. 147 00:10:06,980 --> 00:10:16,010 ‫And what I do now is I just go ahead and use this method to which accept it a parameters list of objects, 148 00:10:16,280 --> 00:10:20,870 ‫however many it is, and we're just passing four because we decided to. 149 00:10:20,900 --> 00:10:26,480 ‫We could have passed three or seven or however many we wanted, and now we're just passing all of those 150 00:10:26,480 --> 00:10:27,800 ‫different variables. 151 00:10:27,860 --> 00:10:31,850 ‫So we are passing the price high at and book. 152 00:10:31,850 --> 00:10:33,110 ‫And if we run this now. 153 00:10:34,390 --> 00:10:39,180 ‫We will see that it says 53.14 AD and The Hobbit. 154 00:10:39,190 --> 00:10:43,270 ‫And then and the second line, what we call Paramount's method method two. 155 00:10:43,270 --> 00:10:46,030 ‫Once again, we are just passing values directly. 156 00:10:46,030 --> 00:10:53,020 ‫So we're passing a string here, a 55.3 and then a character dollar sign. 157 00:10:53,020 --> 00:10:58,900 ‫So a character, it is because we're using the single quotes here. 158 00:11:00,870 --> 00:11:03,450 ‫Or basically the apostrophes. 159 00:11:05,440 --> 00:11:09,700 ‫So now you might wonder, why would I care about this feature? 160 00:11:10,090 --> 00:11:16,060 ‫Because you gave me some examples here, Dennis, but they're not really useful to me, so why would 161 00:11:16,060 --> 00:11:16,450 ‫I care? 162 00:11:16,450 --> 00:11:22,570 ‫Because I can define specifically how many parameter, how many parameters I want to pass to you, and 163 00:11:22,570 --> 00:11:24,880 ‫then I can be very specific about it. 164 00:11:25,000 --> 00:11:29,230 ‫Well, the thing is, there are plenty of applications where that's not the case, where you will actually 165 00:11:29,230 --> 00:11:35,440 ‫need to make sure that you allow the freedom of passing multiple different values. 166 00:11:35,440 --> 00:11:40,930 ‫And we're going to look at it well at in a specific example in the next video. 167 00:11:41,500 --> 00:11:42,970 ‫So see you there.