1 00:00:00,330 --> 00:00:03,090 ‫Welcome to the Methods section and this section. 2 00:00:03,090 --> 00:00:08,190 ‫We are going to cover methods and have a look at what they do and how we can use them. 3 00:00:08,190 --> 00:00:12,900 ‫And they are super important in any object oriented programming language. 4 00:00:12,900 --> 00:00:20,460 ‫And we're going to start off by checking out the definition, which is from the official documents from 5 00:00:20,460 --> 00:00:21,330 ‫Microsoft. 6 00:00:21,360 --> 00:00:24,840 ‫A method is a code block that contains a series of statements. 7 00:00:24,840 --> 00:00:31,620 ‫A program causes the statements to be executed by calling the method and specifying any required method 8 00:00:31,620 --> 00:00:32,490 ‫arguments. 9 00:00:32,490 --> 00:00:37,470 ‫In C-sharp, every executed instruction is performed in the context of a method. 10 00:00:37,560 --> 00:00:44,160 ‫The main method is the entry point for every C-sharp application and it is called by the Common Language 11 00:00:44,160 --> 00:00:47,130 ‫Runtime Sealer when the program is started. 12 00:00:47,250 --> 00:00:52,110 ‫And so far we have seen one method that does exactly that. 13 00:00:52,110 --> 00:00:56,550 ‫It's the main method and it starts whenever we start our program. 14 00:00:56,550 --> 00:01:02,870 ‫So we have used that and we have not created our own method yet, but we will do so in the next videos. 15 00:01:02,880 --> 00:01:07,590 ‫But first of all, let's have a look at what the syntax for a method looks like. 16 00:01:07,590 --> 00:01:15,030 ‫So you have the access specified, then the return type, the method name, then in parentheses you 17 00:01:15,030 --> 00:01:18,660 ‫have the parameter list and then finally you have the method body. 18 00:01:18,660 --> 00:01:24,990 ‫And as usual, when creating classes methods, as I explained earlier, you have the opening curly bracket 19 00:01:24,990 --> 00:01:27,510 ‫and the closing curly bracket. 20 00:01:27,510 --> 00:01:29,040 ‫So that's the syntax. 21 00:01:29,040 --> 00:01:30,930 ‫But what are all of these things? 22 00:01:30,930 --> 00:01:32,370 ‫What is an Excel specify? 23 00:01:32,400 --> 00:01:35,520 ‫What is a return type, what's the method name and so forth? 24 00:01:35,520 --> 00:01:43,320 ‫And there is a great instruction on this, on tutorials point and they have given a great explanation 25 00:01:43,320 --> 00:01:43,940 ‫on the access. 26 00:01:43,960 --> 00:01:50,340 ‫Specify the it determines the visibility of a variable or method from another class. 27 00:01:50,370 --> 00:01:58,290 ‫That means that when you create multiple classes, you can access methods that have a specific access 28 00:01:58,290 --> 00:02:01,050 ‫specify and cannot access others. 29 00:02:01,050 --> 00:02:03,960 ‫So there are multiple types of access specifiers. 30 00:02:03,960 --> 00:02:05,190 ‫For example, public. 31 00:02:05,220 --> 00:02:09,630 ‫If you use public, you can access that method from another class. 32 00:02:09,630 --> 00:02:12,240 ‫If you use private, for example, you cannot. 33 00:02:12,240 --> 00:02:16,620 ‫And there are other access specifiers and we're going to cover them later on in the course. 34 00:02:16,860 --> 00:02:22,350 ‫Then return type A method may return a value, so it may it doesn't have to. 35 00:02:22,380 --> 00:02:28,170 ‫The return type is the data type of the value did the method returns. 36 00:02:28,170 --> 00:02:35,130 ‫So that means that if you want the method to give back something, so return something, you have to 37 00:02:35,130 --> 00:02:37,800 ‫specify which data type you wanted to return. 38 00:02:37,800 --> 00:02:42,000 ‫So for example, if you want to return a number, then you could use, for example, integer. 39 00:02:42,510 --> 00:02:46,860 ‫If the method is not returning any values, then the return type is void. 40 00:02:46,860 --> 00:02:51,720 ‫So there are cases where a method should just run some code and shouldn't return anything. 41 00:02:51,720 --> 00:02:59,460 ‫Then you would simply use void instead of integer or string or double or something similar. 42 00:02:59,490 --> 00:03:00,990 ‫Then you have the method name. 43 00:03:00,990 --> 00:03:05,280 ‫The method name is a unique identifier and it is case sensitive. 44 00:03:05,280 --> 00:03:09,750 ‫It cannot be same as any other identifier declared in the class. 45 00:03:09,750 --> 00:03:17,190 ‫So you have to create a name that is specific and unique and then in this case, sensitive in in the 46 00:03:17,190 --> 00:03:20,790 ‫sense that you can use capital letters and so forth. 47 00:03:20,790 --> 00:03:27,870 ‫But what is best practice is that you start off with a capital letter and then you usually use lower 48 00:03:27,870 --> 00:03:35,640 ‫capital letters and except you have, for example, drive car, then you would write the C as a capital 49 00:03:35,640 --> 00:03:36,600 ‫letter as well. 50 00:03:36,840 --> 00:03:40,980 ‫Then you have the parameter list and closed between parentheses. 51 00:03:40,980 --> 00:03:45,090 ‫The parameters are used to pass and receive data from a method. 52 00:03:45,820 --> 00:03:51,610 ‫The parameter list refers to the type order and number of the parameters of a method. 53 00:03:51,640 --> 00:03:53,350 ‫Parameters are optional. 54 00:03:53,350 --> 00:04:00,910 ‫That is, a method may contain no parameters, which means that there is an input to the method and 55 00:04:00,910 --> 00:04:02,830 ‫those are the parameters. 56 00:04:02,830 --> 00:04:07,570 ‫And you can put something into the method and you can get something back and that's the return type. 57 00:04:07,570 --> 00:04:10,930 ‫So and we will have a look at that in a minute. 58 00:04:10,930 --> 00:04:16,000 ‫So we'll have an example where you can see the parameter list and the return type and then the method 59 00:04:16,000 --> 00:04:16,480 ‫body. 60 00:04:16,480 --> 00:04:20,950 ‫This contains the set of instructions needed to complete the required activity. 61 00:04:20,950 --> 00:04:25,480 ‫And yeah, that's pretty much simply what the method should execute. 62 00:04:25,480 --> 00:04:31,120 ‫So a method should execute some code, and whenever you call that method, it should always execute 63 00:04:31,120 --> 00:04:31,810 ‫that code. 64 00:04:32,020 --> 00:04:34,030 ‫So let's have a look at an example. 65 00:04:34,030 --> 00:04:40,870 ‫So we have here an example where I created a method which says public int add. 66 00:04:40,870 --> 00:04:47,020 ‫So public is the access specified, which means this method can be used from other classes as well. 67 00:04:47,050 --> 00:04:52,180 ‫For now, it doesn't really make a difference for us because we haven't created multiple classes yet, 68 00:04:52,180 --> 00:04:54,370 ‫but we will do so later on. 69 00:04:54,400 --> 00:04:57,100 ‫Then you have int, which is the return type. 70 00:04:57,100 --> 00:05:04,480 ‫So this method will return an integer which is a number, then the name of the method in this case add. 71 00:05:04,480 --> 00:05:10,030 ‫So this method should simply add two values and the two values it should add are the parameters. 72 00:05:10,030 --> 00:05:13,690 ‫So it's int num one and int numb two. 73 00:05:13,690 --> 00:05:20,980 ‫And what's important is that you add the data type, then you enter the name of the parameter, you 74 00:05:20,980 --> 00:05:24,460 ‫separate them by a comma, and then you go ahead. 75 00:05:24,460 --> 00:05:27,910 ‫So you have, you can have multiple different parameters. 76 00:05:27,910 --> 00:05:33,490 ‫It doesn't have to be two, it could be one, it could be 50, whatever. 77 00:05:33,490 --> 00:05:39,850 ‫So what usually is the case that you don't use more than, let's say, five or six, but it can be as 78 00:05:39,850 --> 00:05:40,870 ‫many as you want. 79 00:05:41,080 --> 00:05:41,500 ‫All right. 80 00:05:41,500 --> 00:05:47,950 ‫And then what you do here is you have two numbers and then the parentheses closed and you open your 81 00:05:47,950 --> 00:05:48,940 ‫curly brackets. 82 00:05:48,940 --> 00:05:53,140 ‫And within the curly brackets you have the method content or the method body. 83 00:05:53,230 --> 00:05:55,990 ‫And in here you run the code. 84 00:05:56,020 --> 00:06:03,400 ‫So whenever you have a data type, which is, for example, an integer, you have to return something 85 00:06:03,400 --> 00:06:04,930 ‫that is of that data type. 86 00:06:04,930 --> 00:06:14,110 ‫So in our case, a return result and result is an integer which is equal to num one plus num two. 87 00:06:14,110 --> 00:06:20,410 ‫So this method is a simple example of just a method that will add to values and return the result of 88 00:06:20,410 --> 00:06:21,400 ‫those two values. 89 00:06:21,400 --> 00:06:23,260 ‫So the sum of those two values. 90 00:06:23,260 --> 00:06:25,810 ‫So you see, this is a simple example. 91 00:06:25,810 --> 00:06:29,320 ‫Then you can even reduce the code a little. 92 00:06:29,320 --> 00:06:32,470 ‫So you could get rid of this additional method. 93 00:06:32,470 --> 00:06:39,460 ‫You could simply say, okay, return num one plus num two, so you won't need that additional step in 94 00:06:39,460 --> 00:06:39,850 ‫there. 95 00:06:39,850 --> 00:06:45,910 ‫But you can of course go ahead and create that just for easier reading, for example. 96 00:06:45,910 --> 00:06:49,480 ‫But we will have a look at multiple different examples in the next video. 97 00:06:49,480 --> 00:06:54,550 ‫So in the next video, we're just going to create a void example which doesn't return something, and 98 00:06:54,550 --> 00:06:58,720 ‫then we'll have a look at methods that do return something and so forth. 99 00:06:59,050 --> 00:06:59,710 ‫All right. 100 00:06:59,710 --> 00:07:02,050 ‫So that was just the theoretical part. 101 00:07:02,050 --> 00:07:07,720 ‫Let's go to the demo and let's have a look at what we can do with methods and what the purpose is.