Learn to Code via Tutorials on Repl.it!

← Back to all posts
C# Dash: Level 4 - Dry Methods
DungeonMaster00 (190)

C# Dash: Level 4 - Dry Methods

Yes this series is BACK to compete with @Wuru and to spread more education about C#

This tutorial is about methods and defining your own methods.

The reader is expected to know what Object Oriented Programming is and to have read the last few tutorials.

Some information was retrieved from the Microsoft .Net Online C# documentation

More tutorials:

Scratch Tutorials:


Anyways...

LET'S GET STARTED!!!!!!!!!!!!!!!


What is a method?

A method (sometimes called a function) is a series of commands, called with (). Console.WriteLine() is a method, Convert.ToDouble() is a method, you name it. Anything that does something and has () at the end is a method.


Defining your own

Method definitions occur in classes (not in main or other methods). There are three keywords before the method name. The first two can also be used for classes

First keyword, accessibility level

These will determine the accessibility level of the method. Here are two you need to know about:

  • public - Can be accessed throughout the file.
  • private - Can only be accessed through the same class it's defined in.

For more on accessibility levels, click here.

I will be using public throughout this tutorial, because that's what I always use.

Second keyword, modifier

The simplest modifier is static, which means it can't be changed outside of the method/class.

For more on modifiers, click here.

Third keyword, return type

This will be the type the method will return. If it does not return anything, put void.

About return

return will make the method return a value, allowing it to be used for variables.

Consider the following:

using System;

class insert_class_name_here {
	public static int twoPlusTwo() {
		int sum = 2 + 2;// variable sum is created with value of 4
		return sum;// this method will return the sum variable, with a value of 4
	}
	public static void Main (string[] args) {
		int four = twoPlusTwo();//returns 4
		Console.WriteLine(four);
		/*
		Will output:
		4
		*/
	}
}

The value returned has an int type, so the return type keyword for return type must be int.


Naming your methods and parameters

When naming methods and parameters, follow the same guidelines as naming variables.


Parameters

Parameters are like local variables inside the method. They are declared inside the parenthesis.

Here is a code snippet as an example:

public static double add(double firstNumToAdd, double secondNumToAdd) {
        double sum = firstNumToAdd + secondNumToAdd;
        return sum;
}

When the method is called, the values are declared in things called arguments. They assign a value for the parameter they're referencing.

Example code snippet:

add(1.25, 100.25);// Returns 101.5

To give an argument for a specific parameter, use parameterName: argumentValue.

You can also assign a parameter a default value if it is not referenced in an argument when called.

Same method example but with default values:

public static double add(double firstNumToAdd = 1.34, double secondNumToAdd = 1.16) {
        double sum = firstNumToAdd + secondNumToAdd;
        return sum;
}

If no arguments are given, then the numbers will be 1.34 and 1.16.


Method Overloading

Finally, there are overloads. Overloads are basically methods with the same name but different parameters.

Here is an example of method overloading:

public static void saySomething() {
        Console.WriteLine("Hi.");
}
public static void saySomething(string text) {
        Console.WriteLine(text);
}

The methods have the same name but different parameters, which is exactly what method overloading is.


Level Complete!

I will probably release another bonus tutorial with some cool stuff and other stuff I should have released in earlier tutorials.


Coding challenge (optional)

Make two methods that will square a double and square an integer. Save results to two variables. Print variables to the console.


Example for previous challenge: