Menu
Coddy logo textTech

Declaring Methods

Part of the Fundamentals section of Coddy's C# journey — lesson 49 of 69.

A method is a sequence of code that has a name. The purpose of a method is to reuse a piece of code multiple times.

For example, take a look at this code:

Console.WriteLine("Welcome to Coddy");
Console.WriteLine("New session...");
Console.WriteLine("Welcome to Coddy");
Console.WriteLine("Another session...");
Console.WriteLine("Welcome to Coddy");

We use the same code Console.WriteLine("Welcome to Coddy") over and over again. Another issue with this code is that if we wanted to change the message: Welcome to Coddy to something different, like Welcome aboard it would have to change 3 different lines of code. To solve this issue, we will use methods.

To declare a method, we use the following syntax:

access_modifier [modifiers] return_type method_name(parameters) {
	// code
}

For our example, we will create a method named Greet and it will look like this:

public static void Greet() {
	Console.WriteLine("Welcome to Coddy");
}

If we break it down:

  • public is the access modifier (controls who can use this method)
  • static is a modifier (means the method belongs to the class itself)
  • void is the return type (means it doesn't return any value)
  • Greet is the method name
  • () contains parameters (empty here)

For now, just always write public static before your method name - we'll explain what each word means later.

To use/call/execute the method, we write Greet();:

Greet();
Console.WriteLine("New session...");
Greet();
Console.WriteLine("Another session...");
Greet();

This will result in the same output as above.

Note: In C#, the order of method declarations within a class does not matter. The compiler reads the entire class first, so a method can be called before or after its definition in the source code — both are valid.

For example, both of these are correct in C#:

// Method defined FIRST, then called
public static void Greet() {
    Console.WriteLine("Welcome to Coddy");
}

Greet();
Console.WriteLine("New session...");
Greet();
// Method called FIRST, defined after — also valid in C#
Greet();
Console.WriteLine("New session...");
Greet();

public static void Greet() {
    Console.WriteLine("Welcome to Coddy");
}
challenge icon

Challenge

Easy

Write a program that gets one input, a number. The input number indicates how many times to execute the below method. 

Create a method that calculates the sum of all of the numbers between 1 and 1000 (including) and prints it, name the method however you like.

Cheat sheet

A method is a sequence of code that has a name, used to reuse code multiple times.

Method syntax:

access_modifier return_type method_name(parameters) {
	// code
}

Example method declaration:

public static void Greet() {
	Console.WriteLine("Welcome to Coddy");
}

To call/execute a method:

Greet();

Note: In C#, the order of method declarations does not matter — a method can be called before or after its definition, as long as it is defined within the same class.

Try it yourself

using System;

public class Program {
    // Method declaration
    public static void SumNumbers() {
        // Complete Method
        
    }
    
    
    public static void Main(string[] args) {
        int n = int.Parse(Console.ReadLine());
        for (int i = 0; i < n; i++) {
            // Call the method n times
        }
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals