3.2. Designing Methods#
3.2.1. Method Signature#
The first method you have encountered in C# is the Main method, in which Main is the name that you can to refer to this set of statements. A Main method is special because it is designated as the only entry point of a C# application and therefore the first method to be invoked in a class [^1].
Note that the Main method is contained in a class (Program), which is a standard arrangement for OOP languages. You define a method by writing the method’s header and body. The header is also called a method signature. [^2] When defining methods, you follow a recipe (standard syntax) to specify the method signatures. The standard syntax template for method signatures is as follows: [^3]
[Access Modifier] [static modifier] [Return Type] MethodName(Parameter List)
{
// Method Body: a set of statements enclosed in curly braces { }
}
In this template, the signature include the following parts:
Signature |
Description |
|---|---|
Access Level |
Optional. This determines visibility of methods between classes, and whether or not it can instantiate new objects. Since our methods in this chapter are limited to this class, we will leave them off for now. |
Static modifier |
Define the method as a static member method of the class. When not defined as static, it is an instance member. [#f3]_ |
Return Type |
If the method is returning a value, this indicates what data type it will be. Not all methods return values. Methods that do not return a value have a return type of |
Method Name |
The method’s unique |
Parameter List |
A parameter is a placeholder for specific data that the method will act upon. Parameters are optional. In these cases, the |
Method Body |
This is where you code your method. Note it is contained between |
With method signatures, you can design methods specifically to your needs. But for now, let us analyze the two methods in class TryMethods below to apply our understanding of signatures:
using System; // the using statement
#pragma warning disable CS7022
class TryMethods // class declaration
{
// public static void Main(string[] args) // static modifier, return modifier, and method name
// { // static: can be invoked directly; void, no return to caller
// MyMethod(); // method MyMethod (line# 9) is called
// }
public static void MyMethod() // static modifier, return modifier, and method name
{ // static: can be invoked directly; void, no return to caller
Console.WriteLine("IST 1551"); // Method body
Console.WriteLine("T.Y. Chen"); // Method body
}
}
TryMethods.MyMethod(); // invoking the Main method of TryMethods class
#pragma warning restore CS7022
IST 1551
T.Y. Chen
You have probably noticed that you can prepare this code by making the MyMethod
project folder (mkdir MyMethod), then creating the project by running dotnet new console --use-program-main (or just do dotnet new console then change the code to include
the Main method). After that, you run code . from the terminal to open
the project in VS Code and click on Program.cs to edit the source code.
3.2.2. Method Calls & Returns#
In the preceding code, we add a new method, MyMethod, and call it directly from the program body using top-level statements — no Main() wrapper is needed.
When making a method call, you use the method name followed by
parentheses. The method header static void MyMethod() indicates the return type is void and there are no formal parameters, which means you can call it simply by writing MyMethod();.
An important feature of functions/methods is that they can return values to their
callers. In the example below, SquareTheNumber() is defined with a return type of int
and is called directly from the top-level code, passing a parameter.
1static int SquareTheNumber(int num)
2{
3 return num * num; // the value to be returned is given by the expression in the return statement.
4}
5
6int digit = 4;
7int squaredNum = SquareTheNumber(digit);
8int squaredAndSummed = squaredNum + SquareTheNumber(digit);
9
10Console.WriteLine(squaredNum);
11Console.WriteLine(squaredAndSummed);
12Console.WriteLine(SquareTheNumber(5));
In the preceding code, we see that:
line# 3 uses the
returnkeyword to create a return statement to return the resulted value to the caller.Line# 6 initializes the value of int variable digit to 4
Line# 7 calls the squaredNum() method with argument digit (4) and save the resulted return value to int variable squaredNum.
Line# 8 add squaredNum and add it to the return value of the method call with argument value of 5.
Since methods can return data, and all data in C# is typed,
there must be a type given for the value returned. Note that in the preceding
code the method header does not start with static void.
In place of void is int. The void in method headers
mean nothing was returned. The int here means that a value is
returned and its type is int.
3.2.3. Flow of execution#
In terms of the construct of sequential processing, functions/methods alter code execution order in several ways: by statements not being executed as the definition is first read, and then when the method is called during execution, jumping to the method code, and back at the end of the method execution. [^5]
A class can contain multiple methods. It can be tempting to think the methods are executed in the order they appear in the class, but this is not the case. A program always begins at the first statement in the Main method. Each statement in the main is executed sequentially, one at a time, until you reach a method call. A method call causes the program execution to jump to the first line of the called method. Each statement in the called method is then executed in order. When the called method is done, the program returns to the main method. [^6]
In other words, the order in which the method definition code blocks does
not matter to C#. It is a human choice. One good practice is to show
Main first. This means a human reading in order gets an overview
of what is happening by looking at Main, but does not know the details
until reading the definitions of other methods.
Footnotes
[^1] Note that if you have more than one Main method in you have to use the StartupObject compiler option to specify which Main method to use as the entry point.
[^2] Note that, in OOP, method signature usually refers the method name and the type of its parameters (enclosed in parentheses and separated by commas) while the method header means the whole first line of the method definition. Here we use the definition from Microsoft Learn to make it simple.
[^3] This template and explanation is adopted from https://education.launchcode.org/intro-to-programming-csharp/chapters/methods/method-signatures.html
[^4] Main method is required for console and Web apps in .NET.