So far, everything we’ve been doing has been in the
Main method. However, one of the features of good code writing is that methods are kept small and organized. One method may call another, and another, and another, with each one achieving a specific task.
If meant to be accessible by another class (file), the method declaration should begin with the
public modifier. The default access level is
private which means the method can only be called from within the same class.
Static is a more advanced concept, yet it is always used in the
Program class for any methods called via
Main. Static means that the method belongs to the Type of the class, not to a particular object instantiation. We will return to it at a later point.
The method name can be anything, but it is good to make it a clear description of what the method does. Method names cannot have spaces or special characters, so most .NET programmers use either
PascalCase (capitalize every word) or
camelCase (capitalize every word except the first).
When creating a method, you can pass variables to it by way of
parameters listed in parentheses. When calling the method, these same variables are referred to as