Methods in C#

In C#, methods are like functions. Methods can be created to store a list of instructions that can then be called on later on in the program. Methods can be used more than once so if there are instructions you need to use constantly throughout a program, then it makes sense to write them only once and call on them when needed rather than writing the instructions multiple times.

Methods also allow you to break up different tasks in your program an make them easily identifiable, rather than having a heap of different tasks in one big block of code (or spaghetti code).

The code below shows how to make a command-line calculator that breaks up each function of the calculator (adding, subtracting, multiplying and dividing numbers and checking which calculation option the user chose). The methods are declared and then called on in the program.

Sample C# code

using System; // tells this file we will be using the System namespace

namespace MyCSharpApp // new namespace called MyCSharpApp which will contain classes
{
  class MainClass // new class called MainClass
  {
    public static void Main (string[] args) //new method called Main (executes as soon as the app runs)
    // which has to be a public static void and will receive an array of strings
    {
      Console.WriteLine ("Hello World!");
      Console.WriteLine ("What is your name?");
      // WriteLine method is used to write a string to the console
      string name = Console.ReadLine();
      // we can add this method so the program will wait until the user
      // types something in or presses a key to continue
      // By creating a variable called name, now the ReadLine input is stored in a variable
      Console.WriteLine("Hi there, " + name);
      calculator ();
    }

    public static void calculator()
    {
      Console.WriteLine("Enter two numbers...");
      Console.WriteLine ("Enter the first number:");
      float num1 = float.Parse (Console.ReadLine ());
      // float.Parse() is used to convert string to float
      Console.WriteLine ("Enter the second number");
      float num2 = float.Parse (Console.ReadLine ());
      // could write Console.WriteLine(num1+num2);
      // or could use a method instead...
      Console.WriteLine ("Select an option\n1 - Add\n2 - Subtract\n3 - Multiply");
      int option = int.Parse(Console.ReadLine());
      checkOption (option, num1, num2);
    }

    // this creates a new method called addNums which takes two float arguments
    public static float addNums(float n1, float n2)
    {
      float answer = n1 + n2;
      return answer;
    }

    // this creates a new method called subtractNums which takes two float arguments
    public static float subtractNums(float n1, float n2)
    {
      float answer = n1 - n2;
      return answer;
    }

    // this creates a new method called multiplyNums which takes two float arguments
    public static float multiplyNums(float n1, float n2)
    {
      float answer = n1 * n2;
      return answer;
    }

    public static void checkOption(int option, float n1, float n2){
      if (option == 1) {
        Console.WriteLine ("The answer is: " + addNums (n1, n2));
      }

      else if (option == 2) {
        Console.WriteLine ("The answer is: " + subtractNums (n1, n2));
      }

      else if (option == 3) {
        Console.WriteLine ("The answer is: " + multiplyNums (n1, n2));
      }

      else {
        Console.WriteLine ("Option not recognised.");
      }
    }

  }
}