In this article i am explaining how to call method in C#.After creating function, you need to call it in Main() method to execute programe. In order to call method, you need to create object of  class, then followed bydot(.) operator you can call the method. If method is static, then there is no need to create object of class and you can  call directly it followed by class name

Example:

Using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Declaring_Method
{
    class Program
    {
        string name, city;
        int age;
        public void AcceptDetails()
        {
            Console.Write("\nEnter your name:\t");
            name = Console.ReadLine();
            Console.Write("\nEnter Your City:\t");
            city = Console.ReadLine();
            Console.Write("\nEnter your age:\t\t");
            age = Convert.ToInt32(Console.ReadLine());
        }
        public void PrintDetails()
        {

            Console.Write("\n\n====================");
            Console.Write("\nName:\t" + name);
            Console.Write("\nCity:\t" + city);
            Console.Write("\nAge:\t" + age);
            Console.Write("\n====================\n");
        }
        static void Main(string[] args)
        {

            //creating object of class Program
            Program p = new Program();
            p.AcceptDetails(); // Calling method
            p.PrintDetails(); // Calling method
            Console.ReadLine();
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *