如何在 C# 中呼叫類的方法


要呼叫方法,請在物件名稱後使用該方法的名稱,例如,−

obj1. Display();

假設類名為 ApplicationOne,因此要呼叫該方法 −

ApplicationOne one = new ApplicationOne();

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

以下是顯示如何在 C# 中呼叫方法的示例 −

示例

 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();
      }
   }
}

輸出

Max value is : 700

更新於:2020-06-20

2 千+ 瀏覽量

開啟您的 職業生涯

完成課程並獲得認證

開始
廣告
© . All rights reserved.