Intro to C#: Methods

Redmond Chan
4 min readJan 1, 2020

A method contains a block of code to be executed when the method is called. Methods are declared in a class or struct. A class is a user-defined blueprint or prototype from which objects are created. A struct is a light weight version of a class. I won’t go into depth of the differences here since this post is about methods. So back to methods…

Line 3: a class was declared.

Line 5: a method was declared in the class.

Line 12: calls the method that was declared.

How to write a method? First, you need the static keyword so the method can be accessible in Main. Followed by the data type that the method will return and then the name of the method. Within the curly brackets, you’ll write the code you want to be executed when this method is called. Let’s take a look at this:

The data type that the method will return is set to void which means this method does not return a value. Whenever a method does not return a value, you set the return data type to void. The name of this method is SayHi.

Here is an example of a method that will return a value:

This method will return a value with the data type of int. Since it will return a value, you need to specify that in the block of code that will be executed, this is done in line 11. The name of this method is Double. You may have noticed that this method takes in a parameter. Within the parenthesis, you can define a list of parameters to work with. A data type and variable name needs to be specify. In the example above, it’ll take in one integer parameter and it’ll be stored as “x” for that method.

Here’s an example of multiple parameters:

This method will take in two parameters. All you have to do to add more parameters is add a comma behind the previous parameter and then declare the data type and variable name.

In line 14, you may be a little confused with “int y = 2”. What this does is sets the default value of y to 2. If this method is called without a second parameter then it will use 2 as the value of y. For example, Diff(4): this would return 2 because:

x = 4

y = 2

x -y

4 -2 = 2

So far we’ve only seen multiple parameters of the same data type. Here’s an example of different data type parameters:

It doesn’t matter what data type you want to include for your parameters. You only have to make sure the return data type value is the same as the one specified before the method name.

Here’s an example of method overloading:

Method overloading is when multiple methods have the same name but different parameters. The method YourAge can accept integers, double or strings as an argument.

Here’s an example of named arguments:

Imagine you forget the order of the parameters for this method. Line 19 would be your savior. You can use the variable names for each parameter to include parameters in any order. You just have to write the name followed by a colon and then the value for the parameter. In line 19, I set y to 4 and x to 8. This will work even though they’re in the wrong order.

Here is the link to all the examples in this post so you can play around with it:

https://repl.it/@redmondchan/CSharp-Methods

This is most of the information that I know about methods that would prove to be useful to jump start your adventure with methods. I’m not sure who would be reading this today but if you are…Happy New Year!

--

--