‫Welcome to the Methods section and this section.
‫We are going to cover methods and have a look at what they do and how we can use them.
‫And they are super important in any object oriented programming language.
‫And we're going to start off by checking out the definition, which is from the official documents from
‫Microsoft.
‫A method is a code block that contains a series of statements.
‫A program causes the statements to be executed by calling the method and specifying any required method
‫arguments.
‫In C-sharp, every executed instruction is performed in the context of a method.
‫The main method is the entry point for every C-sharp application and it is called by the Common Language
‫Runtime Sealer when the program is started.
‫And so far we have seen one method that does exactly that.
‫It's the main method and it starts whenever we start our program.
‫So we have used that and we have not created our own method yet, but we will do so in the next videos.
‫But first of all, let's have a look at what the syntax for a method looks like.
‫So you have the access specified, then the return type, the method name, then in parentheses you
‫have the parameter list and then finally you have the method body.
‫And as usual, when creating classes methods, as I explained earlier, you have the opening curly bracket
‫and the closing curly bracket.
‫So that's the syntax.
‫But what are all of these things?
‫What is an Excel specify?
‫What is a return type, what's the method name and so forth?
‫And there is a great instruction on this, on tutorials point and they have given a great explanation
‫on the access.
‫Specify the it determines the visibility of a variable or method from another class.
‫That means that when you create multiple classes, you can access methods that have a specific access
‫specify and cannot access others.
‫So there are multiple types of access specifiers.
‫For example, public.
‫If you use public, you can access that method from another class.
‫If you use private, for example, you cannot.
‫And there are other access specifiers and we're going to cover them later on in the course.
‫Then return type A method may return a value, so it may it doesn't have to.
‫The return type is the data type of the value did the method returns.
‫So that means that if you want the method to give back something, so return something, you have to
‫specify which data type you wanted to return.
‫So for example, if you want to return a number, then you could use, for example, integer.
‫If the method is not returning any values, then the return type is void.
‫So there are cases where a method should just run some code and shouldn't return anything.
‫Then you would simply use void instead of integer or string or double or something similar.
‫Then you have the method name.
‫The method name is a unique identifier and it is case sensitive.
‫It cannot be same as any other identifier declared in the class.
‫So you have to create a name that is specific and unique and then in this case, sensitive in in the
‫sense that you can use capital letters and so forth.
‫But what is best practice is that you start off with a capital letter and then you usually use lower
‫capital letters and except you have, for example, drive car, then you would write the C as a capital
‫letter as well.
‫Then you have the parameter list and closed between parentheses.
‫The parameters are used to pass and receive data from a method.
‫The parameter list refers to the type order and number of the parameters of a method.
‫Parameters are optional.
‫That is, a method may contain no parameters, which means that there is an input to the method and
‫those are the parameters.
‫And you can put something into the method and you can get something back and that's the return type.
‫So and we will have a look at that in a minute.
‫So we'll have an example where you can see the parameter list and the return type and then the method
‫body.
‫This contains the set of instructions needed to complete the required activity.
‫And yeah, that's pretty much simply what the method should execute.
‫So a method should execute some code, and whenever you call that method, it should always execute
‫that code.
‫So let's have a look at an example.
‫So we have here an example where I created a method which says public int add.
‫So public is the access specified, which means this method can be used from other classes as well.
‫For now, it doesn't really make a difference for us because we haven't created multiple classes yet,
‫but we will do so later on.
‫Then you have int, which is the return type.
‫So this method will return an integer which is a number, then the name of the method in this case add.
‫So this method should simply add two values and the two values it should add are the parameters.
‫So it's int num one and int numb two.
‫And what's important is that you add the data type, then you enter the name of the parameter, you
‫separate them by a comma, and then you go ahead.
‫So you have, you can have multiple different parameters.
‫It doesn't have to be two, it could be one, it could be 50, whatever.
‫So what usually is the case that you don't use more than, let's say, five or six, but it can be as
‫many as you want.
‫All right.
‫And then what you do here is you have two numbers and then the parentheses closed and you open your
‫curly brackets.
‫And within the curly brackets you have the method content or the method body.
‫And in here you run the code.
‫So whenever you have a data type, which is, for example, an integer, you have to return something
‫that is of that data type.
‫So in our case, a return result and result is an integer which is equal to num one plus num two.
‫So this method is a simple example of just a method that will add to values and return the result of
‫those two values.
‫So the sum of those two values.
‫So you see, this is a simple example.
‫Then you can even reduce the code a little.
‫So you could get rid of this additional method.
‫You could simply say, okay, return num one plus num two, so you won't need that additional step in
‫there.
‫But you can of course go ahead and create that just for easier reading, for example.
‫But we will have a look at multiple different examples in the next video.
‫So in the next video, we're just going to create a void example which doesn't return something, and
‫then we'll have a look at methods that do return something and so forth.
‫All right.
‫So that was just the theoretical part.
‫Let's go to the demo and let's have a look at what we can do with methods and what the purpose is.