C# Methods - Lessons

 

Introduction to Methods

Methods! We used to call these things subroutines back in the days of BASIC and COBOL. Algorithms or routines that perform a task is what they actually are. Each one can be called many times from many different locations. Basically, they are buttons without the button.

The VOID Method

The VOID method returns no values. That’s why the data type is VOID. It is a snippet of code or an algorithm that performs a specific task every time you call it. Think of it as a subroutine. It does something.

Passing Arguments to Methods

A better and safer way to program is to create variables in your methods and events. If you need to share them with another routine, you can pass the value (not the variable) to the new method. It can do what it wants with that data, but it will never damage what you originally have stored in that variable.

Value-Returning Methods

Value-Returning methods allow us to return a value to a variable in the calling modules. We will bypass global variables entirely. We will create a variable and place an equal sign after the variable. That equal sign is then followed by a call to a method.

Passing Arguments by Reference

By using a method that is given access to the memory location of our variable rather than the value of the variable, our method could make the change to the value right in the calling module. Typically, these are VOID methods. They don’t need to return any values because they are messing with the actual original value.