How to call a method of a class in C#


To call a method, use the name of the method after the object name, for example, −

obj1. Display();

Let’s say the class name is ApplicationOne, so to call the method −

ApplicationOne one = new ApplicationOne();

//calling the displayMax method
ret = one.displayMax(a, b);

The following is the example showing how to call a method in C# −

Example

 Live Demo

using System;

namespace Demp {
   class ApplicationOne {
      public int displayMax(int num1, int num2) {
         /* local variable declaration */
         int result;

         if (num1 > num2)
         result = num1;
         else
         result = num2;
         return result;
      }

      static void Main(string[] args) {
         /* local variable definition */
         int a = 700;
         int b = 400;
         int ret;
         ApplicationOne one = new ApplicationOne();

         ret = one.displayMax(a, b);
         Console.WriteLine("Max value is : {0}", ret );
         Console.ReadLine();
      }
   }
}

Output

Max value is : 700

Updated on: 20-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements