C# Methods with Examples

A method in C# is a block of code used to perform a particular task. Methods are used when we need to create a block of code once and execute it multiple times. You will understand why I am saying this later on this page. But for now, let's show you the general form for creating a method in C#.

accessSpecifier returnType methodName(parameterList)
{
   // block of code
   // or
   // body of the method
}

For example:

static void greeting()
{
   Console.WriteLine("I hope you enjoyed your weekend.");
}

Since I used static as an access specifier, it is not required to create an instance of the program for this method to work. Then I used void as the return type, which means that the method greeting() will not return any value.

Now, to call a C# method, just write the method along with its parameters if the method that is going to be called accepts parameters. Since the previous method I defined, which was greeting(), does not accept any parameters, to call this method I have to write the name of the method followed by parantheses or () in this way.

greeting();

Let me create a proper C# example to demonstrate the definition and calling of a function.

using System;
namespace MyFirstCSharpProgram
{
    class MyClassXYZ
    {
        static void greeting()
        {
            Console.WriteLine("I hope you enjoyed your weekend.");
        }

        static void Main(string[] args)
        {
            greeting();
        }
    }
}

The snapshot given below shows the exact output produced by the above C# example. This snapshot was taken from the output console of Microsoft Visual Studio after executing the above C# program.

c sharp methods example

I used the red arrow to draw your attention to the output. Since the execution of the C# program starts with the Main() method, you need to call your defined method from inside this method. Also, you can call the same method multiple times.

C# Methods with Parameters

In this section, I will tell you how you can pass parameters to a method.

A parameter is used to pass it as information. The parameter will act as a variable inside the method definition. Let me give you an example, and then I will explain how the thing works.

using System;
namespace MyFirstCSharpProgram
{
    class MyClassXYZ
    {
        static void add(int a, int b)
        {
            int sum = a + b;
            Console.WriteLine("\nSum = {0}", sum);
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Enter the First Number: ");
            int numOne = Convert.ToInt32(Console.ReadLine());
            int numTwo = Convert.ToInt32(Console.ReadLine());

            add(numOne, numTwo);
        }
    }
}

The sample run with user inputs of 12 and 43 as the first and second numbers is shown in the snapshot given below.

C sharp methods with parameters example

The dry run of the above C# program goes this way.

C# Methods with Return Value

We can also create a method in C# that can return values. Here is an example demonstrating a function with a return value in C#.

using System;
namespace MyFirstCSharpProgram
{
    class MyClassXYZ
    {
        static int add(int a, int b)
        {
            int res = a + b;
            return res;
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Enter the First Number: ");
            int numOne = Convert.ToInt32(Console.ReadLine());
            int numTwo = Convert.ToInt32(Console.ReadLine());

            int sum = add(numOne, numTwo);
            Console.WriteLine("\nSum = {0}", sum);
        }
    }
}

You will get the same output as the previous example's output.

C# Online Test


« Previous Tutorial CodesCracker.com »


Liked this post? Share it!