Method (C#)

A method is a named, reusable block of code inside a class that performs a task. You define its return type and parameters, then call it by name to run its code.

C#

Methods help you avoid repeating code and keep programs organized. The return type says what kind of value the method gives back; use void when it returns nothing.

Values you pass in are called arguments, and the named placeholders in the method definition are parameters.

Syntax

returnType Name(parameters) { ... return value; }

Examples

C#

string Greet(string name)
{
    return "Hi, " + name;
}

Console.WriteLine(Greet("Lee"));

Output:

Hi, Lee

Learn it in a lesson

See also

Back to the Dictionary