When a complex application is divided into methods, code becomes more flexible, easy to maintain and to debug. For performing repetitive tasks methods are useful. Methods allow breaking an application into discrete logical units, which makes the application more readable. Code written in a method can be reused since it can be executed any number of times by calling the method itself with little or no modification.
The syntax of defining a method is given below:
-------------------------------------------------
<>
{
//Method body
}
-------------------------------------------------
Let's look at an example:
-------------------------------------------------
public void Hello()
{
Console.WriteLine("Hello World!");
}
-------------------------------------------------
Here "public" keyword shows the access to this method/function. When we use public access specifier the method can be accessible inside and outside the class.
"void" refers to the return type of the method. Void means nothing should be returned and Hello() is the function/method name. Functions/methods will be having opening and closing parenthesis.
No comments:
Post a Comment