Declaring, Implementing, and Invoking a Method
There are three main stages involved in working with methods in C#:
- Declaration
- Implementation
- Invocation
Let's go through each stage step-by-step, using examples to illustrate the concepts.
Declaration of a Method
When you declare a method, you define its name, return type, and any parameters it takes. Let’s revisit the Caesar Cipher
program, starting with the automatically generated code in Visual Studio when you created the project:
In this code, Visual Studio generated a class named CaesarCipher
with a method named Main()
. Notice the curly braces { ... }
enclosing the body of both the class and the method.
- The
class
keyword declares theCaesarCipher
class. - The
Main()
method is the entry point of the program and is declared inside theCaesarCipher
class.
We can add more methods to this class. For example:
Here, two additional methods (DisplayGreeting()
and EncryptMessage()
) have been declared within the CaesarCipher
class.
Components of a Method Declaration
Every method declaration includes the following elements, in this order:
- Return Type: Specifies what kind of data the method will return. If the method doesn't return any value, the return type is
void
. - Method Name: A descriptive name that starts with an uppercase letter and follows PascalCase naming convention.
- Parameter List: A comma-separated list of parameters enclosed in parentheses. Parameters provide input values to the method and can be empty.
Let’s analyze the Main()
method again:
- Return Type:
void
means this method doesn't return any value, making it a procedure. If it returned a value, we would use types likeint
orstring
, making it a function. - Method Name:
Main
is the default entry point of a C# program, and it must be named exactlyMain
. - Parameter List:
(string[] args)
is an array of strings namedargs
. This allows the program to accept command-line arguments when it starts.
Access Modifiers
Methods can also have access modifiers that control their visibility:
public
: The method can be accessed from outside the class.private
: The method can only be accessed from within the same class (default if no access modifier is specified).protected
: The method can only be accessed within its class and by derived classes.
The static
keyword means the method belongs to the class itself, not to an instance of the class. This concept is fundamental in object-oriented programming (OOP) and can be explored further as you progress.
Method Naming Conventions
- Start with an uppercase letter.
- Use PascalCase (e.g.,
CalculateSum()
). - Prefer descriptive names, ideally a verb or verb-noun combination (e.g.,
PrintReport()
). - Avoid abbreviations and keep the name meaningful.
Implementation of a Method
Implementing a method means writing the code that performs the task described by the method name. The code is placed between the curly braces following the method declaration. For example:
This method displays a message and waits for the user to press a key before exiting.
Local Variables
Variables declared inside a method are called local variables because their scope is limited to the method. Once the method finishes executing, these variables are no longer accessible.
In this example, number
is a local variable. It only exists within the GetInt()
method and is not accessible outside.
Scope of Local Variables
Local variables are only in scope within the code block they are declared in. For example, variables declared in a for
loop only exist while the loop is executing.
Invoking or Calling a Method
Invoking or calling a method is done by writing its name followed by parentheses. If the method requires parameters, you provide them inside the parentheses.
GetInt()
returns an integer, so its return value is stored in the variablex
.ExitConsole()
doesn’t return a value, so it can be called on its own line.
Passing Parameters to Methods
Methods can take parameters that are passed in when the method is called. For example:
The PrintMessage()
method accepts a string parameter message
and prints it to the console.
Summary
Understanding the stages of method declaration, implementation, and invocation is crucial for structuring programs effectively in C#. Practice by creating different methods, experimenting with return types and parameters, and calling them from the Main()
method.