Access Modifier

Method (C#)

A method in C# is a function defined inside a class that defines what an object can do.

This page covers Method in C#. For a language-agnostic introduction, see Method.

Defining a Method#

A C# method has an access modifier, a return type, a name, and a body.

public void Greet()
{
    Console.WriteLine("Hello!");
}
  • public — the access modifier; public means the method can be called from anywhere
  • void — the return type; void means the method returns nothing
  • Greet — the method name; use PascalCase by convention
  • () — the parameter list; empty here, but arguments go inside these parentheses

Return Types#

If a method produces a value, declare the return type instead of void and use return to send it back.