C# - Programming Language
This course covers the basics of programming in C#. Work your way through the videos/articles and I'll teach you everything you need to know to start your programming journey!
I'm starting a coding bootcamp
Want to learn web development in a fun and gamefied environment? Head over to simulator.dev and see what I'm working on!


Lesson 12
Author : Mike Dane
Last Updated : November, 2017

In this lesson I wanna talk to you about a really important concept in C# which makes it really easy to keep our programs organized and re-usable. I'm talking of course about Methods!

Maybe you've heard the word Method floating around as you've begun your programming journey, but today we're gonna break down exactly what they are and I'll show you how to use them in C#.

Methods and Organization

So far in this course we've written a lot of code. All sorts of code to do all sorts of different things. And as we go forward and begin to write more and more complex and time consuming programs, one thing that is going to be vitally important is that we have good organization.

Without good organization a program can quickly get out of hand. Eventually you'll find that you're writing separate pieces of code in different parts of the file or project which do exactly the same thing!

There's a principle in software development which all good programmers try to follow called the DRY principle. It stands for "Don't repeat yourself". In other words, it means, don't write the same code twice!

As programmers, a Method is our best defence against violating the DRY principal. A Method is basically a collection of code which can be executed anywhere in our program and used over and over again.

In many programs there will be certain functionality that needs to be re-used multiple times.

Maybe you want to format a string a specific way, or you want to perform some complex math calculation on two numbers, or you want to parse through an array looking for a particular element. All of these are situations where a Method can be written which encapsulates the code needed to do those things, and can then be re-used throughout the program again and again.

sayHi() Method

To fully illustrate Methods lets take a look at an example of a simple Method which says hi to the user. Below is the C# code for our sayHi Method.

Copypublic static void Main(string [] args){

public static void sayHi(){

Right off the bat you'll notice a few things here. First is that just like variables we can actually give Methods descriptive names. In this case we've named the Method sayHi because that's what it's doing, that's the functionality that it's performing.

It's really important that you give these good names, otherwise your program will be significantly less read-able.

We follow the Method name with a set of open and closed parenthesis, and then below the top line (which we call the Method declaration), we can write some code. All of the code below the declaration is considered inside the Method.

Once we've written the code inside the Method (note: you can write as much code as you like), we can then execute that code wherever and whenever we want in our programs by calling the Method.

Calling a Method

When you call a Method you are essentially telling C# that you want it to stop what it's doing and go execute all of the code contained in the Method. Then once it's done, to come back and keep going on with the normal flow of the program. Let's look at how we can call a Method:

Copypublic static void Main(string [] args){

To call a Method we simple state the Method's name and then directly after, place an open and closed parenthesis. The open and closed parenthesis tells C# that you want to actually execute the Method.

Like I mentioned above, when a Method is called C# will stop what it's doing, and jump over to wherever that Method is and execute all of the code inside. Once it's finished with the code in the Method it will return to where it was and keep going.

So if we wanted to sayHi in multiple places in our program, instead of having to duplicate the code for doing that multiple times, we could instead just call the sayHi Method.

This is also advantageous because if we wanted to change the way our program said hi then we could just update the code in the Method body and the program would automatically update the way it's say hi all throughout our program!

Parameters and Arguments

Methods are powerful not only because they allow us to re-use the same code over and over, but because they also allow us to pass them information.

When a Method is called in a program, the code calling it can supply it with information specific to it's wants and needs. For example let's say that we wanted to modify our sayHi to say hi to a specific person. The way it's written right now it can't do that, all it does it just say hello.

In order to allow it to say hi to a specific person, we need to specify in the Method declaration that it accepts the name of a person from the caller. This is referred to as a parameter. Check out our updated sayHi Method below and see if you can spot the parameter.

Copypublic static void sayHi(string name){
    Console.WriteLine("Hello " + name);

I'll give you a hint, look inside the parenthesis, notice how we've declared a variable there called name.

This variable is actually called a parameter, and it defines a piece of information that the Method accepts. Often times, Methods will accept parameters which help them to perform their task in a way that is more specific to the needs of the individual caller.

With this Method the way it is now, we can actually call it and have it say hi to whomever we want.

Copypublic static void Main(string [] args){

Notice above that now when I call the sayHi Method, instead of leaving the parenthesis blank, I place a string inside "Mike". This string is what's called an argument. This argument will get sent to the Method when it is called and be stored in the name parameter specified in the sayHi Method.

This is an important point of terminology.

A parameter is a special variable that a Method will use to perform it's task in a way that is more specific to the needs of the caller. An argument is the value the caller gives to the Method which will get stored inside the parameter variable.

In the case above we only specified one single parameter, but you can specify as many as you want (although any more than 4 or 5 is generally considered too many). Let's take a look at this same Method but with an additional parameter age:

Copypublic static void Main(string [] args){
  sayHi("Mike", 24)

public static void sayHi(){
    Console.WriteLine("Hello " + name + " you are " + age);

This looks pretty similar to our previous example, but now in addition to the name parameter, I've also specified the age parameter. Multiple parameters are separated with a comma, and you can't have two parameters with the same name.

When we call the Method now we can pass in the age as well. Notice that the position of the arguments matches the position of the parameters; The name is first and the age is second in both cases, this is crucial!

Wrapping it up

So there you have it, an introduction to Methods. This is just the first step, but an important step towards mastering Methods and being able to really take advantage of them in our programs. Try writing some Methods of your own and see if you can come up with anything cool!