In this example, i’ll show you How to call method in C#.
After creating function, you need to call it in Main() method to execute. In order to call method, you need to create object of containing class, then followed bydot(.) operator you can call the method. If method is static, then there is no need to create object and you can directly call it followed by class name.
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | 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(); } } } |