C# Reference ยท Classes and Object-Oriented Concepts
Methods
Reference for C# methods: declaration syntax, parameters, ref, out, in and params, return values, overloading, expression-bodied members and local functions.
A method is a named block of code that can take parameters and return a value. Every statement in C# lives inside a method (or an accessor, constructor or other function member). Methods belong to a type, either to the type itself (static) or to each instance.
A method is identified by its signature: its name plus the number, types and kinds (ref, out, in) of its parameters. The return type is not part of the signature, so you cannot overload on return type alone.
public static int Add(int a, int b) { return a + b; } public static int Twice(int x) => x * 2; // expression-bodied
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
[attributes] [access] [modifiers] ReturnType Name<TParams>(parameters) [where ...] { body }
| Part | Description |
|---|---|
| Return type | Any type, or void for none. Task, Task<T> and ValueTask<T> for async methods. |
| Modifiers | static, virtual, override, abstract, sealed, new, async, extern, partial, unsafe. |
| Parameters | By value (default), ref, out, in, ref readonly, params, scoped; may have default values. |
| Body | A block, or an expression after =>. |
| Parameter kind | Effect |
|---|---|
| (none) | Copy of the value; for reference types, a copy of the reference. |
ref |
Alias to the caller's variable; must be initialized before the call. |
out |
Alias that the method must assign before returning; no initial value needed. |
in |
Read-only reference, avoids copying large structs. |
params |
Accepts a variable number of arguments as an array (or span or collection in C# 13). |
Errors: not assigning an out parameter, or not returning a value on every path, is a compile error.
Usage notes
- Overload resolution picks the best match by argument types; ambiguities are compile errors. Optional and named arguments are covered in Optional and named arguments.
- Reference types are passed by value too: the method receives a copy of the reference, so it can change the object's contents, but assigning a new object to the parameter does not affect the caller. Use
reffor that. - Return multiple values with tuples (
(int Min, int Max)) or anoutparameter. Tuples are usually clearer in new code;TryXxx(out ...)is the established pattern for operations that can fail without exceptions. - Expression-bodied methods (
=> expr) suit one-liners. Use a block for anything with several statements. - Local functions can capture variables and, when marked
static, are guaranteed not to. They are good for recursion and for helpers that only one method needs. - Pass big structs by
in, not by value, when copying shows up in profiles. Small structs are cheaper to copy. - Methods are not first-class values until converted to a delegate:
Func<int,int> f = Twice;creates one from a method group. - Keep methods small and named after what they do; a long parameter list is a hint to introduce a parameter object (a record).