Methods

In C#, methods are used to write a block of code and name it so you can re-use that block of code later on. This is one of the most basic building blocks of C# so that is why it is important for us to understand it early on.

method is a block of code which only runs when it is called.

You can pass data, known as parameters, into a method.

Methods are used to perform certain actions, and they are also known as functions.

Why use methods? To reuse code: define the code once, and use it many times.

Example:

using System;

namespace SimpleMethod
{
    class Program
    {
        static void Main(string[] args)
        {
            sayHello();
        }

        public static void sayHello()
        {
            Console.WriteLine("Hello Sir or Ma'am. ");
        }
    }
}

using System;

See also W3SCHOOLS page: https://www.w3schools.com/cs/cs_methods.php

Back to MAIN CSHARP PAGE