目录

C# - Methods

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

要使用方法,您需要 -

  • 定义方法
  • Call the method

在C#中定义方法

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

<Access Specifier> <Return Type> <Method Name>(Parameter List) {
   Method Body
}

以下是方法的各种要素 -

  • Access Specifier - 这确定了来自另一个类的变量或方法的可见性。

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

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

  • Parameter list - 括在括号中,参数用于传递和接收方法中的数据。 参数列表指的是方法的参数的类型,顺序和数量。 参数是可选的; 也就是说,方法可能不包含任何参数。

  • Method body - 包含完成所需活动所需的一组指令。

例子 (Example)

下面的代码片段显示了一个函数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

您还可以使用类的实例从其他类调用公共方法。 例如, 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

递归方法调用

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

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. 机制和描述
1 值参数

此方法将参数的实际值复制到函数的形式参数中。 在这种情况下,对函数内部参数所做的更改不会对参数产生影响。

2 参考参数

此方法将对参数的内存位置的引用复制到形式参数中。 这意味着对参数所做的更改会影响参数。

3 输出参数

此方法有助于返回多个值。

↑回到顶部↑
WIKI教程 @2018