C# - 类方法

C# - 类方法 首页 / C#入门教程 / C# - 类方法

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

定义方法

当你定义一个方法时,你基本上声明了它的结构的元素,在C#中定义方法的语法如下所示:-

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

下面的代码片段显示了一个函数FindMax,它接受两个整数值并返回两个中较大的一个,它具有public访问符,因此可以使用类的从类外部访问它。

class NumberManipulator {

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

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

      return result;
   }
   ...
}

调用方法

可以使用方法的名称调用方法。以下示例说明了此-

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

编译并执行上述代码时,将生成以下输出-

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/csharp-methods.html

来源:LearnFk无涯教程网

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

编译并执行上述代码时,将生成以下输出-

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/csharp-methods.html

来源:LearnFk无涯教程网

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

编译并执行上述代码时,将生成以下输出-

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/csharp-methods.html

来源:LearnFk无涯教程网

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

参数传递

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

Sr.No. Mechanism & 描述
1 Value parameters

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

2 Reference parameters

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

3 Output parameters

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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

左耳听风 -〔陈皓〕

技术管理实战36讲 -〔刘建国〕

深入剖析Kubernetes -〔张磊〕

Flutter核心技术与实战 -〔陈航〕

分布式技术原理与算法解析 -〔聂鹏程〕

NLP实战高手课 -〔王然〕

手把手带你写一门编程语言 -〔宫文学〕

React Native 新架构实战课 -〔蒋宏伟〕

遗留系统现代化实战 -〔姚琪琳〕

好记忆不如烂笔头。留下您的足迹吧 :)