Skip to content

EN CSharp Methods

Codex edited this page Oct 11, 2026 · 2 revisions

Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어

Methods, parameters and local functions

Manual C# · Examples UO

The method declares the result type, name and parameters. void means no result; other methods return a value via return. Overloads differ in parameters. Named and optional parameters refer to the specific method declaration.

Simple example (Example.cs)

Save this example separately as Example.cs. Run it independently of the other examples on this page.

Example.cs:

// PROJECT-EXAMPLE
int Double(int value) => value * 2;
System.Console.WriteLine(Double(4));

Result:

8
string MakeMessage(string name, int count = 1)
{
    return $"{name}: {count}";
}
UO.Print(MakeMessage(name: "Items", count: 3));
UO.Print(MakeMessage("Attempt"));
int Total(params int[] numbers) => numbers.Sum();
int value = Total(2, 3, 4);
UO.Print(value.ToString());

Regular parameters pass the value of a variable; For a reference to an object, the reference itself is copied. ref allows you to read and change the variable of the calling code, out requires assigning an output value, in passes an argument via a readonly link. Returning a result, calling a method, and declaring it are different actions.

void Increment(ref int number) => number++;
int current = 1;
Increment(ref current);
bool parsed = int.TryParse("42", out int result);
UO.Print($"Current={current}, parsed={parsed}, result={result}");

Official documentation: Methods, parameters, and local functions - Microsoft Learn.

Clone this wiki locally