CSharp基础 专题
专题目录
您的位置:csharp > CSharp基础 专题 > C#方法
C#方法
作者:--    发布时间:2019-11-20

c#中的方法是一组执行任务的语句。 每个 c# 程序至少有一个类包含一个名称为main()的方法。

要使用方法,需要:

  • 定义方法
  • 调用方法

c# 中定义方法

当要定义一个方法时,需要声明它的结构元素。 c# 中定义方法的语法如下:

<access specifier> <return type> <method name>(parameter list)
{
   method body
}

以下是方法中的各种元素说明:

  • 访问说明符(access specifier):这决定了一个类的变量或方法的可见性。
  • 返回类型(return type):方法可能返回一个值。返回类型是方法返回的值的数据类型。 如果方法没有返回任何值,则返回类型为void
  • 方法名称(method name):方法名称是唯一标识符,区分大小写。它不能与在类中声明的任何其他标识符相同。
  • 参数列表(parameter list):括号内的参数用于从方法传递和接收数据。参数列表是指方法参数的类型,顺序和编号。参数是可选的; 也就是说,方法可能不使用参数。
  • 方法体(method body):这包含完成所需操作的一组代码语句和具体实现。

例子

以下代码片段显示了一个函数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 = 101;
         int b = 199;
         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 : 199

还可以使用该类的实例从其他类调用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
         console.writeline("factorial of 6 is : {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

将参数传递给方法

调用参数的方法时,需要将参数传递给方法。有三种方式可以将参数传递给方法,分别如下表格中所列:

机制 简介
按值传递参数 将参数的实际值复制到函数的形式参数中。在函数内对参数所做的更改对参数没有影响。
按引用传递参数 将对参数的内存位置的引用复制到形式参数中,在函数内对参数的更改会影响参数值。
输出参数 此方法用于返回多个值。

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册