Friday, 17 August 2018

C#: Methods

Method is a block of statements, you can define method once and execute it any number of times.

Syntax
accessModifier returnType methodName(arguments){
         //Body Of the method
}

Don’t worry about accessModifier now, I will explain about this, after explaining about classes.

returnType is the value returned by the method, this can be any valid predefined (or) custom data type. If you don’t want to return any value from the method, make the return type as ‘void’.

methodName is any valid name which is not a C# keyword.

You can pass zero (or) more arguments to the method.

Example
int sum(int a, int b)
    {
        return a + b;
    }

int : Return type of the method sum
sum: Method name
int a, int b : Arguments to the method sum

Program.cs
using System;

class Program
{
    static void Main(string[] args)
    {
        Program obj = new Program();

        int result1 = obj.sum(10, 5);
        int result2 = obj.sub(10, 5);
        int result3 = obj.mul(10, 5);
        int result4 = obj.div(10, 5);

        Console.WriteLine("Sum of 10 and 5 is {0}", result1);
        Console.WriteLine("Subtraction of 10 and 5 is {0}", result2);
        Console.WriteLine("Multiplication of 10 and 5 is {0}", result3);
        Console.WriteLine("Division of 10 and 5 is {0}", result4);

        obj.printEvenNumbers();
    }

    int sum(int a, int b)
    {
        return a + b;
    }

    int sub(int a, int b)
    {
        return a - b;
    }

    int mul(int a, int b)
    {
        return a * b;
    }

    int div(int a, int b)
    {
        return a/ b;
    }

    void printEvenNumbers()
    {
        Console.WriteLine("Even Numbers from 0 to 10 are : ");
        for(int i=0; i<10; i += 2)
        {
            Console.WriteLine(i);
        }
    }

}


Output

Sum of 10 and 5 is 15
Subtraction of 10 and 5 is 5
Multiplication of 10 and 5 is 50
Division of 10 and 5 is 2
Even Numbers from 0 to 10 are :
0
2
4
6
8


Notify above snippet, methods are called using the instance (object) of the class Program.

        Program obj = new Program();

        int result1 = obj.sum(10, 5);
        int result2 = obj.sub(10, 5);
        int result3 = obj.mul(10, 5);
        int result4 = obj.div(10, 5);

Methods defined without ‘static’ keyword are called instance methods. Instance methods must be called by defining the object to a class. In the above example, sum, sub, mul, div, printEvenNumbers are the instance methods.

Following statement defines the objects to the class Program.
        Program obj = new Program();

Static Methods
Methods defined using static keyword are called static methods. These are called class level methods. Static methods are called by class name.

Ex:
    static int sum(int a, int b)
    {
        return a + b;
    }

You can call above method by using ‘Program.sum(10, 5)’, where Program is the class name.


Program.cs

using System;

class Program
{
    static void Main(string[] args)
    {
        Program obj = new Program();

        int result1 = Program.sum(10, 5);
        int result2 = Program.sub(10, 5);
        int result3 = Program.mul(10, 5);
        int result4 = Program.div(10, 5);

        Console.WriteLine("Sum of 10 and 5 is {0}", result1);
        Console.WriteLine("Subtraction of 10 and 5 is {0}", result2);
        Console.WriteLine("Multiplication of 10 and 5 is {0}", result3);
        Console.WriteLine("Division of 10 and 5 is {0}", result4);

        Program.printEvenNumbers();
    }

    static int sum(int a, int b)
    {
        return a + b;
    }

    static int sub(int a, int b)
    {
        return a - b;
    }

    static int mul(int a, int b)
    {
        return a * b;
    }

    static int div(int a, int b)
    {
        return a/ b;
    }

    static void printEvenNumbers()
    {
        Console.WriteLine("Even Numbers from 0 to 10 are : ");
        for(int i=0; i<10; i += 2)
        {
            Console.WriteLine(i);
        }
    }

}


Output

Sum of 10 and 5 is 15
Subtraction of 10 and 5 is 5
Multiplication of 10 and 5 is 50
Division of 10 and 5 is 2
Even Numbers from 0 to 10 are :
0
2
4
6
8




Previous                                                 Next                                                 Home

No comments:

Post a Comment