• 首页
  • 新手教程库
  • 在线工具
  • 视频教程
  • 代码片段库
  • 更多功能
    • 在线测验
    • 手机APP
Hostwinds建站首选4刀起支持Linux/Win(支付宝)
PacificRack $12/年起三网线路优化(可支付宝)
国外VPS哪个最好?
搬瓦工VPS-新增日本机房限量方案年付69
HostDare CN2 GIA线路4k无压力
美国和欧洲vps
  1. 首页
  2. 新手教程库
  3. Microsoft技术教程
  4. C# - 方法
C# - 概述 C# - 概述 C# - 环境 C# - 程序结构 C# - 基本语法 C# - 数据类型 C# - 类型转换 C# - 变量 C# - 常量和文字 C# - 运营商 C# - 决策 C# - 循环 C# - 封装 C# - 方法 C# - Nullables C# - 数组 C# - 字符串 C# - 结构 C# - 枚举 C# - 类 C# - 继承 C# - 多态性 C# - 运算符重载 C# - 接口 C# - 命名空间 C# - 预处理器指令 C# - 正则表达式 C# - 异常处理 C# - 文件I / O. C# - 属性 C# - 反思 C# - 属性 C# - 索引器 C# - 代表 C# - 活动 C# - 收藏 C# - 泛型 C# - 匿名方法 C# - 不安全代码 C# - 多线程 C# - 有用的资源
教 程 目 录
上一节
下一节  

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

要使用方法,您需要 :

  • 定义方法

  • 调用方法

在C#中定义方法

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

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


以下是方法的各种元素 :

  • 访问说明符 : 这决定了来自另一个类的变量或方法的可见性.

  • 返回类型 : 方法可以返回值.返回类型是方法返回的值的数据类型.如果方法没有返回任何值,则返回类型为 void .

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

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

  • 方法主体 : 这包含完成所需活动所需的一组说明.

示例

以下代码snippet显示一个函数 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


递归方法调用

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

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值参数(Value parameters)

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


2参考参数(Reference parameters)

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


3输出参数(Output parameters)

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


上一节
下一节  

相关新手教程:

Powershell教程
VBA教程
高级Excel教程
MFC 教程
C#教程
vb.net教程
Microsoft Project教程
LinQ教程
IT屋 ©2016-2020 京ICP备14011762号 鄂公网安备42018502004713号 站点地图 站点标签 意见&反馈  SiteMap <免责申明> 本站内容来源互联网,如果侵犯您的权益请联系我们删除.
  • 首页
  • 教程
  • 工具
  • 视频
  • 代码
  • 联系站长