Methods

This section covers the fundamentals of method syntax, including parameters, return types, and control flow using return statements.

Syntax Overview

Methods are defined to encapsulate reusable logic in programming to "do stuff". Here's the general syntax for a method:

[access-modifier] return-type MethodName(parameters) {
    // method body
}
  • Access modifier: Defines the visibility (e.g., public, private, internal, protected) - we'll discuss more about these later. For now, we'll mostly use public.
  • Return type: The type of data returned by the method
  • Method name: The unique identifier of the method
  • Parameters: Inputs that the method accepts

Method names in C# are typically Pascal-cased (e.g. ThisIsPascalCase()).

Example:

public int AddNumbers(int a, int b) {
    return a + b;
}

Parameters

Methods can accept inputs known as parameters, which are declared inside parentheses in the method definition.

Types of parameters

  • Required parameters: Inputs that must be provided when calling the method.
  • Optional parameters: Inputs that can have default values.
  • Variable-length parameters: Methods can accept a variable number of arguments.
public void GreetUser(string name) {
    Console.WriteLine("Hello, " + name);
}

With optional parameters:

public void GreetUser(string name = "User") {
    Console.WriteLine("Hello, " + name);
}

With variable-length parameters:

public void GreetUsers(params string[] names) {
    foreach (string name in names) {
        Console.WriteLine("Hello, " + name);
    }
}

GreetUsers("Alice", "Bob", "Charlie");
GreetUsers("Alice");

Parameters are used to pass data into the method. A method can have zero or more parameters.


Void-returning Methods

Void methods do not return any value. They simply execute logic without providing any output directly.

Syntax:

[access-modifier] void MethodName(parameters) {
    // method body
}

Example:

public void DisplayMessage() {
    Console.WriteLine("This is a void method.");
}

Value-returning methods

A value-returning method provides a result after performing some logic. The return type is specified before the method name.

Syntax:

[access-modifier] return-type MethodName(parameters) {
    // method body
    return value;
}

Example:

public int Multiply(int a, int b) {
    return a * b;
}

Methods must return a value that matches the return type. The return statement exits the method and provides a result.

Return statement

The return statement is used to exit a method and optionally return a value. In void methods, return can be used without a value to exit early.

Syntax:

return [expression];

Example:

public bool IsEven(int number) {
    if (number % 2 == 0) {
        return true;
    } else {
        return false;
    }
}

The return statement breaks the flow of the method and sends the result back to the caller. In a value-returning method, every code path must end with a return (with one exception (pun intended) - a thrown exception. More on that later.)