Back to Blog
C#

C# Method vs Function: What Actually Differs

c# method vs function: Understand the real difference between methods and functions in C#, including local functions, static methods, delegates, and when the distincti...

C# methodsC# functionsmethod syntaxlocal functionsdelegatesC# language
Illustration contrasting a C# method inside a class with a standalone function concept, showing how methods are bound to types.

c# method vs function requires a clear understanding of the core syntax, runtime behavior, and practical implementation patterns demonstrated in the examples below.

In C#, the terms method and function often appear interchangeably, but they refer to distinct concepts. The difference matters when you read documentation, write APIs, or reason about where code can live. This article explains how C# treats functions and methods, why almost every function you write is a method, and where the language provides genuine function-like constructs.

What C# Calls a Function vs a Method

C# is an object-oriented language, so all executable code must belong to a type. A method is a function declared inside a class, struct, interface, or record. It has a return type, a name, parameters, and a body. When you write int Add(int a, int b) inside a class, you are defining a method.

The word function is a broader term from mathematics and programming theory. In in C#, every method is a function, but not every function is a method. The C# language also supports local functions, which are declared inside another method, and anonymous functions expressed as lambdas. These are functions in the sense that they take inputs and return outputs, but they are not methods because they are not directly members of a type.

In practice, when developers say "function" in a C# context, they usually mean a method. The distinction becomes relevant when you need to pass behavior as data, which C# supports through delegates and functional-style APIs.

Declaring a Method in C#

A method is the most common way to encapsulate reusable logic. Here is a minimal declaration inside a class:

public class Calculator { public int Add(int left, int right) { return left + right; } }

The method Add belongs to Calculator. To call it, you create an instance and invoke it:

var calc = new Calculator(); int sum = calc.Add(3, 4);

Methods can also be static, meaning they are called on the type rather than an instance:

public static class MathHelper { public static int Multiply(int a, int b) => a * b; }

Static methods are functions that do not require object state. They are common for utility operations and are the closest thing to standalone functions in C#.

Local Functions: Functions Inside Methods

C# lets you declare a function inside another method. This is called a local function. It is scoped to the enclosing method and can capture local variables.

public int Calculate(int[] numbers) { int Sum() { int total = 0; foreach (var n in numbers) { total += n; } return total; } return Sum(); }

Local functions are useful when you need a helper that is only relevant within one method. They keep the logic close to its use and avoid polluting the class with private methods. They can also be recursive and can be defined after the calling code, which improves readability in some cases.

One key difference from a method is that a local function is not a member of the type. It is compiled into a method behind the scenes, but from the language perspective it is a function that lives inside a method body.

Static Methods and Extension Methods

Static methods are methods that do not operate on an instance. They are called using the type name. For example:

int result = MathHelper.Multiply(5, 6);

Extension methods are a special kind of static method that appear as instance methods on a type. They are defined in a static class and use the this keyword on the first parameter:

public static class StringExtensions { public static bool IsNullOrEmpty(this string value) { return string.IsNullOrEmpty(value); } }

You can then call it as if it were an instance method:

string text = null; bool empty = text.IsNullOrEmpty();

Extension methods are still static methods; they are just syntax sugar. They let you add behavior to existing types without inheritance. This is a functional pattern because the method is essentially a function that takes the receiver as its first argument.

Method Overloading and Function Signatures

A method's identity in C# is determined by its name and parameter list. This is called a signature. You can define multiple methods with the same name as long as they have different parameter types or counts:

public class Printer { public void Print(string text) { } public void Print(int number) { } public void Print(string text, int times) { } }

Overloading is a compile-time mechanism. The compiler chooses the correct method based on the arguments at the call site. This is different from languages where functions are first-class values and can be passed around freely.

In C#, methods are not first-class values by themselves. To pass a method as a parameter, you need a delegate or a functional interface.

Delegates and Lambdas as Function Values

A delegate is a type that represents a reference to a method. It is the C# way to treat functions as values. For example:

public delegate int Operation(int a, int b); public static int Execute(Operation op, int x, int y) { return op(x, y); }

You can pass a method or a lambda to Execute:

int result = Execute((a, b) => a + b, 3, 4);

Lambdas are anonymous functions. They are not methods because they have no name and are not members of a type. They are compiled to delegate instances or expression trees depending on the context.

This is where the function vs method distinction becomes practical. When you use LINQ, you are passing functions as arguments. The methods you define are still methods, but the lambdas you write are functions in the functional sense.

Performance and Maintainability Considerations

Method calls in C# have a small overhead due to stack frame setup and argument passing. For most applications this is negligible. However, in hot paths, the JIT compiler may inline small methods, eliminating the call overhead. You can also use MethodImplOptions.AggressiveInlining to hint the compiler, but this is rarely necessary.

Local functions and lambdas can capture variables, which may allocate a closure object on the heap. If you create many such functions in a loop, this can increase memory pressure. In performance-sensitive code, consider whether a static method or a local function without captures would be more efficient.

From a maintainability perspective, methods give you a named, reusable unit that can be tested independently. Local functions are harder to test because they are scoped to a method. Use a local function when the logic is trivial and only used in one place. Use a method when the logic is complex or might be reused.

When the Distinction Matters in Real Code

The decision to use a method vs a local function vs a delegate depends on what you need:

  • Use a method when you need a reusable operation that belongs to a type.
  • Use a local function when you need a helper that is only relevant inside one method and you want to capture local state.
  • Use a delegate or lambda when you need to pass behavior as an argument, such as in LINQ or event handlers.

Understanding the difference helps you read C# code more accurately. When you see a method declaration, you know it is part of a type's contract. When you see a lambda, you know it is a transient function that may capture context. This awareness improves code design and makes it easier to reason about lifetime and state.

In C#, the language does not have standalone functions like in C or JavaScript. Every function you write is either a method, a local function, or a lambda. The term function is a generic concept, while method is the concrete C# construct. Knowing this distinction prevents confusion when reading documentation or discussing design with other developers.

c# method vs function: Practical Usage and Code Examples | RYUSLOG DEV