method is a series of statements that together perform a task. In C# program we have at least one class with a method named Main. methods do only one specific task that is good for programming. Right use of methods get the following advantages:
Method is followed by parentheses() & it is defined with the name of the method. There are some pre-defined methods in C#, which you already know about Main(), but to perform certain action you can also create your own methods.
There are various elements of a method −
using System;
namespace MyCode
{
class Program
{
static void MethodName()
{
Console.WriteLine("This is Declaration of Method"); //Declaration of method
}
static void Main(string[] args)
{
MethodName();
}
}
}
OUTPUT
This is Declaration of Method
calling a method means write the method name with parentheses() and semicolon ; In the example we call method i.e. MethodName(); when we call a method then these functionality happen:
using System;
namespace MyCode
{
class Program
{
static void MethodName()
{
Console.WriteLine("This is Declaration of Method");
}
static void Main(string[] args)
{
MethodName(); //calling method
}
}
}
OUTPUT
This is Declaration of Method