Methods
Learn to create and call methods in .NET apps.
We'll cover the following
Create methods
A variable is a named area of memory that holds a value of some type. The methods are similar, but instead of holding a value, methods are named blocks of instructions.
The basic syntax for creating a method is as follows:
[modifiers] return_type method_name([parameters])
{
// Method body
}
Note: Modifiers (not covered in this lesson) and parameters are optional.
We’ve already addressed methods. Apart from the WriteLine()
method of the Console
class we’ve been using to print to the console, we’ve created one method that’s mandatory in all .NET programs:
static void Main(string[] args)
{
}
The static
keyword is a modifier, void
is the return type, Main
is the name of the method, and args
is a parameter of type, string[]
(an array of string
objects).
Inside the curly braces, we write the body of the method. The method’s body is what action the method performs.
Let’s create two methods that produce a greeting in English and Spanish.
Get hands-on with 1400+ tech skills courses.