HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to C#VariablesData TypesOperatorsStringsListsConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 List & Dictionary
🔀 Condition & Loop
🧩 Function
C#›Functions

Functions

Local function definitions, parameters, return, and arrow (=>) concise syntax.

Simple enough for a 5-year-old to understand

Define a function

You define a local function in the outer program: return type, name, and parameters.

Example code
1
using System;
2
int Square(int x) {
Takes an int, returns an int.
FunctionTakes an int, returns an int.
3
return x * x;
Returns the result.
DefinitionReturns the result.
4
}
5
Console.WriteLine(Square(5));
25
Output / Input25
FunctionDefinitionOutput / Input
💡 Hint: return gives back a value; WriteLine just prints to the screen.

About this topic

In C#, a piece of code that performs a task is called a method, and every method lives inside a class. A method signature consists of a return type, a name, and a parameter list; if it returns nothing, void is written.

One of C#'s strengths is parameter flexibility: you can provide default values, specify parameters by name during a call, and accept a variable number of arguments. This allows you to combine different uses of the same method into a single signature.

Besides returning values, methods can also yield results via parameters: the out keyword provides a second output and is the basis of the TryParse pattern. Meanwhile, ref shares the variable directly.

What you will learn

  • Defining and calling methods
  • Using return types and void
  • Parameters with default values and named parameters
  • out and ref parameters
  • The difference between static methods and instance methods
  • Method overloading and expression-bodied methods

When to use?

If you are about to write a block of code a second time, it is time to extract it into a method. Named parameters make multi-parameter method calls readable — especially with boolean parameters, where the call makes it clear what is happening.

For methods that produce a result in a single line, the expression-bodied format is used: you define it with arrow syntax without curly braces and return.

Common mistakes

Wrong

static int Add(int a, int b) {
    int sum = a + b;
}

The return type is declared as int, but the method does not return a value; the compiler throws "error CS0161: not all code paths return a value". Every method specifying a return type must return a value.

Correct

static int Add(int a, int b) {
    return a + b;
}

Wrong

class Program {
    void Greet() {
        Console.WriteLine("Hello");
    }
    static void Main() {
        Greet();
    }
}

Main is static, while Greet is an instance method. You cannot directly call an instance method from a static context; you get "error CS0120: An object reference is required".

Correct

class Program {
    void Greet() {
        Console.WriteLine("Hello");
    }
    static void Main() {
        new Program().Greet();
    }
}

Wrong

static void Reset(int value) {
    value = 0;
}
// call:
int count = 5;
Reset(count);

C# passes parameters by value by default; assigning a value to a parameter inside the method does not affect the variable on the calling side, and count remains 5. You need to return the value or use ref.

Correct

static int Reset() => 0;
// call:
int count = 5;
count = Reset();

Frequently asked questions

What is the difference between out and ref?

An out parameter must be assigned a value INSIDE the method, and the variable does not need to be initialized before calling. On the other hand, ref must be initialized before calling, and the method can read and modify it. The TryParse pattern uses out.

What is method overloading?

It is writing multiple methods with the same name but different parameter lists. The compiler chooses which one to call based on the types of the arguments. Parameters with default values eliminate the need for overloading in most cases, meaning less code.

What is an expression-bodied method?

It is a way of writing a method consisting of a single expression using arrow syntax: you do not write curly braces and return. It is very common for short calculations and property accessors, and it clearly cleans up the code.

Summary

In C#, every method lives inside a class, and its signature consists of a return type, name, and parameters. Parameters are passed by value, an instance method cannot be called from a static context, and the out parameter is the basis of the TryParse pattern.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code