📜  C#-方法

📅  最后修改于: 2020-12-28 05:04:37             🧑  作者: Mango


方法是一起执行任务的一组语句。每个C#程序至少都有一个类,带有一个名为Main的方法。

要使用一种方法,您需要-

  • 定义方法
  • 调用方法

C#中的定义方法

定义方法时,基本上是声明其结构的元素。在C#中定义方法的语法如下-

  (Parameter List) {
   Method Body
}

以下是方法的各个要素-

  • 访问说明符-确定另一个类中的变量或方法的可见性。

  • 返回类型-方法可以返回一个值。返回类型是方法返回的值的数据类型。如果该方法未返回任何值,则返回类型为void

  • 方法名称-方法名称是唯一标识符,区分大小写。它不能与该类中声明的任何其他标识符相同。

  • 参数列表-用括号括起来的参数用于传递和接收方法的数据。参数列表是指方法的参数的类型,顺序和数量。参数是可选的;也就是说,一个方法可以不包含任何参数。

  • 方法主体-这包含完成所需活动所需的一组指令。

以下代码段显示了一个函数FindMax ,该函数采用两个整数值并返回两个中较大的一个。它具有公共访问说明符,因此可以使用类的实例从类外部进行访问。

class NumberManipulator {

   public int FindMax(int num1, int num2) {
      /* local variable declaration */
      int result;

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

      return result;
   }
   ...
}

C#中的调用方法

您可以使用方法名称来调用方法。以下示例说明了这一点-

using System;

namespace CalculatorApplication {
   class NumberManipulator {
      public int FindMax(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 = 100;
         int b = 200;
         int ret;
         NumberManipulator n = new NumberManipulator();

         //calling the FindMax method
         ret = n.FindMax(a, b);
         Console.WriteLine("Max value is : {0}", ret );
         Console.ReadLine();
      }
   }
}

编译并执行上述代码后,将产生以下结果-

Max value is : 200

您还可以使用该类的实例从其他类调用public方法。例如,方法FindMax属于NumberManipulator类,您可以从另一个类Test调用它。

using System;

namespace CalculatorApplication {
   class NumberManipulator {
      public int FindMax(int num1, int num2) {
         /* local variable declaration */
         int result;
         
         if(num1 > num2)
            result = num1;
         else
            result = num2;
         
         return result;
      }
   }
   class Test {
      static void Main(string[] args) {
         /* local variable definition */
         int a = 100;
         int b = 200;
         int ret;
         NumberManipulator n = new NumberManipulator();
         
         //calling the FindMax method
         ret = n.FindMax(a, b);
         Console.WriteLine("Max value is : {0}", ret );
         Console.ReadLine();
      }
   }
}

编译并执行上述代码后,将产生以下结果-

Max value is : 200

递归方法调用

方法可以自行调用。这称为递归。以下是使用递归函数为给定数字计算阶乘的示例-

using System;

namespace CalculatorApplication {
   class NumberManipulator {
      public int factorial(int num) {
         /* local variable declaration */
         int result;
         if (num == 1) {
            return 1;
         } else {
            result = factorial(num - 1) * num;
            return result;
         }
      }
      static void Main(string[] args) {
         NumberManipulator n = new NumberManipulator();
         //calling the factorial method {0}", n.factorial(6));
         Console.WriteLine("Factorial of 7 is : {0}", n.factorial(7));
         Console.WriteLine("Factorial of 8 is : {0}", n.factorial(8));
         Console.ReadLine();
      }
   }
}

编译并执行上述代码后,将产生以下结果-

Factorial of 6 is: 720
Factorial of 7 is: 5040
Factorial of 8 is: 40320

将参数传递给方法

调用带有参数的方法时,需要将参数传递给方法。参数可以通过三种方式传递给方法-

Sr.No. Mechanism & Description
1 Value parameters

This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.

2 Reference parameters

This method copies the reference to the memory location of an argument into the formal parameter. This means that changes made to the parameter affect the argument.

3 Output parameters

This method helps in returning more than one value.