TExcelTAD
Home › C# › C# Methods

C# Methods

A method is a named block of code that performs a task. You write it once and call it as often as you like. Methods break big programs into small, understandable pieces, remove duplicated code and make testing easier. (Other languages call them functions; in C# every function belongs to a type, so they are called methods.)

Declaring and calling a method

csharp · try it yourself
using System;

class Program
{
    static void SayHello()
    {
        Console.WriteLine("Hello from a method!");
    }

    static void Main()
    {
        SayHello();
        SayHello();
        SayHello();
    }
}
Hello from a method!
Hello from a method!
Hello from a method!

Let's look at the declaration static void SayHello():

Part Meaning
static The method belongs to the class itself, so Main (also static) can call it without creating an object
void The method does not return a value
SayHello The method's name, in PascalCase by convention — usually a verb
() The parameter list, empty here
{ … } The method body

You call a method by writing its name followed by parentheses: SayHello();.

Methods with parameters and return values

Methods become really useful when they take input (parameters) and produce output (a return value). The next lessons cover both in depth; here is a first taste:

csharp · try it yourself
using System;

class Program
{
    static double CircleArea(double radius)
    {
        return Math.PI * radius * radius;
    }

    static void PrintArea(double radius)
    {
        Console.WriteLine(
quot;r = {radius}: area = {CircleArea(radius):F2}"); } static void Main() { PrintArea(1); PrintArea(2.5); PrintArea(10); } }
r = 1: area = 3.14
r = 2.5: area = 19.63
r = 10: area = 314.16

Methods can call other methods: Main calls PrintArea, which calls CircleArea.

Methods with top-level statements

With top-level statements you can declare local functions right in the file. You can call them before they are declared — the compiler sees the whole file — and they need no static keyword (though adding it prevents them from accidentally using outer variables).

csharp · try it yourself
using System;

PrintBanner("Report");
Console.WriteLine(
quot;Total: {Add(19, 23)}"); PrintBanner("End"); static void PrintBanner(string title) { Console.WriteLine(
quot;===== {title} ====="); } static int Add(int a, int b) { return a + b; }
===== Report =====
Total: 42
===== End =====

Expression-bodied methods

When a method consists of a single expression, you can use the => arrow syntax instead of braces and return:

csharp · try it yourself
using System;

class Program
{
    static int Square(int x) => x * x;
    static bool IsEven(int n) => n % 2 == 0;
    static void Log(string message) => Console.WriteLine(
quot;[log] {message}"); static void Main() { Log(
quot;7 squared is {Square(7)}"); Log(
quot;Is 10 even? {IsEven(10)}"); } }
[log] 7 squared is 49
[log] Is 10 even? True

Why use methods?

  • Reuse — write the logic once and call it from many places.
  • Readability — CalculateShipping(order) explains itself; forty lines of arithmetic do not.
  • Maintainability — fix a bug in one place instead of in every copy.
  • Testability — small methods with clear inputs and outputs are easy to test.

Tip: A good method does one thing and has a name that says what it is. If you struggle to name a method, it probably does too much.

Static and instance methods

All methods in this lesson are static. Methods without static are instance methods: they belong to an object and are called on it, like name.ToUpper(). You will create your own in the Classes section.

Test yourself

What does void mean in static void Print()?

  1. The method is empty
  2. The method returns no value
  3. The method takes no parameters
  4. The method is private

Which is a valid expression-bodied method?

  1. static int Twice(int x) -> x * 2;
  2. static int Twice(int x) => x * 2;
  3. static int Twice(int x) = x * 2;
  4. static int Twice(int x) => { x * 2 }

Why can Main call SayHello() directly without creating an object?

  1. Because both are in the same file
  2. Because SayHello is static
  3. Because it returns void
  4. Because it has no parameters