C# Building Blocks: Methods

Mike Bischoff
4 min readMay 11, 2022

Variables provide a convenient way to store various types of data, but simply having the data isn’t very useful without a means to use them. A program isn’t really a program until it has some kind of functionality.

This is where methods come in.

Simply stated, methods are instructions for your program. They’re where calculations happen that affect how users will interact with your program and determine what kind of functionality your program will have.

Methods can be very simple, such as a single line to write out text to the console, or they can be extremely complex, with dozens or even hundreds of lines of code, nesting more methods inside of them, to perform difficult or intricate calculations based on numerous types of inputs.

Methods vs. Functions

If you have previous coding experience or familiarity with programming terms, you might wonder why they’re called methods instead of functions. Most people will say the terms are interchangeable and that it’s merely C# convention to use one name instead of the other. For the most part that explanation suffices, but it’s not the most accurate.

Technically, methods are a specific type of function that are contained within a class. For our purposes, our C# functions will always be contained within classes, therefore they will always be methods. But if you’re coming from another programming language where you’ve gotten accustomed to using the term “function,” you won’t be hurt by interchanging the two.

Syntax for Writing C# Methods

The basic syntax for methods in C# is as follows:

Just like with variable, the first declaration of the method is the access modifier. The two most common access modifiers are “public,” meaning this method can be accessed by other scripts inside the program, or “private,” which means it can only be run from within the script it’s contained in. Methods are private by default, so omitting the access modifier won’t cause a compile error, it will just make the method private.

The next declaration is the return type. In this case, it’s void, which means it’s not returning any data at the end of the calculation. More on this in a bit.

Next is the method name, which is conventionally written in PascalCase to differentiate from variables, which are named in camelCase. A rule of thumb for naming methods is to give them action-oriented names that describe the purpose of the method. For example, a method related to controlling character movement might be called CalculateMovement(), or one that’s responsible for keeping the data in HUD elements current could be called UpdateUI(), etc.

Finally, the code to be executed by the method is contained within the curly braces {}.

Return-type Methods

As I mentioned, methods can also return a value which can be used like any other data type. An example of a return-type method is as follows:

After the access modifier, instead of void, we now declare a data type. Just like with variables, common data types to use in these kinds of methods are floats, ints or bools.

Because the method needs to return a data type, inside our code block we need a variable that matches the data type we’ve declared. C# is smart enough that if you simply use the shorthand var inside a method, it will automatically assign it as the data type that matches whatever is on the other side of the equals sign.

Assuming the variable data is calculated as a float, we just need to use the keyword return followed by the variable name.

This data doesn’t do much on its own, but it can be used in exactly the same way as any other variable. In fact, you can even initialize variables as the result of a matching return-type method.

Methods with Parameters

Methods can also take in parameters which allow for even greater flexibility and ease of use, for instance when calling them from other scripts with private variables. Consider the previous example for CalculateFloat(). We can specifiy two parameters in the form of variables that are declared inside the parentheses ().

Now, whenever the CalculateFloat() method is called anywhere in the program, it will be required to include two float values. In this case I selected 7.21 and 8.45 arbitrarily, typically these would be variables derived elsewhere in the calling script.

Methods underpin almost all of the functionality of a given C# program. Understanding how they work, and how and when to use them efficiently and correctly, is one of the most important skills to develop as a programmer.

--

--

Mike Bischoff

Author, motion designer, Unity developer, my producers’ favorite freelancer