C# Tutorials
C# Methods
C# Classes
C# Examples
The method is a code block that only works when called.
You can transfer data, known as parameters, into a method.
Methods are used to perform certain actions, and are also known as functions.
Why should you use methods? Code reset: define code once, and use it multiple times.
Method is defined by the name of the method, followed by parentheses (). C# offers pre-defined methods, which you are already familiar with, such as Main(), but you can also create your own actions:
Create a method inside the Program class:
class Program
{
static void MyMethod()
{
// code to be executed
}
}
To drive (remove) a path, type the path of the path followed by two parentheses () and a semicolon;
In the following example, MyMethod () is used to print text (action), if it is called:
Inside Main(), call the myMethod() method:
static void MyMethod()
{
Console.WriteLine("I just got executed!");
}
static void Main(string[] args)
{
MyMethod();
}
// Outputs "I just got executed!"
The method can be called multiple times:
static void MyMethod()
{
Console.WriteLine("I just got executed!");
}
static void Main(string[] args)
{
MyMethod();
MyMethod();
MyMethod();
}
// I just got executed!
// I just got executed!
// I just got executed!