Methods

The Purpose of Methods

In general, methods act as an interface between a program and the fields of a class in that program.

These methods can either alter the data stored in the fields or use their existing values to perform a certain computation. All the useful methods should be public, although, some methods which do not need to be accessed from the outside could be kept private.

Definition and Implementation

A method is a group of statements that performs some operations and may or may not return a result.

Here is an example of a Buy() method implemented inside the VendingMachine class. The method is being invoked in the Main() method, inside the Demo class:

Get hands-on with 1200+ tech skills courses.