Introduction

This lesson introduces methods and explains how to declare them using example

What is a method?

Functions declared inside a struct or class are called methods.

A method encapsulates a block of code that can be called from other parts of the program.

The use of methods in C# increases code reusability. Literally, you can tag a particular section of code with a name (method name) and just call that name from anywhere of your program.

Note: In C# a method may or may not have a return type.

Declaring a Method

Every method has a unique signature consisting of:

  • accessor (public, private, …)
  • optional modifier (abstract)
  • a name
  • method parameters (if needed )

Note: The return type is not part of the signature.

A method prototype looks like the following:

Get hands-on with 1400+ tech skills courses.