如何在C#中定义自定义方法?

王林
发布: 2023-09-15 18:17:06
转载
1083人浏览过

如何在c#中定义自定义方法?

在C#中定义自定义方法,使用以下语法 −

<Access Specifier> <Return Type> <Method Name>(Parameter List) {
Method Body
}
登录后复制

The following are the various elements of a method −

  • Access Specifier − This determines the visibility of a variable or a method from another class.

  • Return type − A method may return a value. The return type is the data type of the value the method returns. If the method is not returning any values, then the return type is void.

  • Method name − Method name is a unique identifier and it is case sensitive. It cannot be same as any other identifier declared in the class.

    Koobi Pro
    Koobi Pro

    主要功能: 无限级分类,商品可在各类别间自由转移; 组合商品概念,可以用于组配商品销售(比如服装鞋帽的颜色、尺码大小等),组合销售等销售方式; 商品的自定义属性功能,商品类别扩展属性,满足商品多属性需求(比如某一笔记本电脑,可以有cpu、内存、显示屏、硬盘等等扩展属性); 按照商品类别查看热卖、特价,允许按每个类别增加当前类别的热卖、特价等商品; 会员分级功能,会员积分功能。可根据会

    Koobi Pro 0
    查看详情 Koobi Pro
  • Parameter list − Enclosed between parentheses, the parameters are used to pass and receive data from a method. The parameter list refers to the type, order, and number of the parameters of a method. Parameters are optional; that is, a method may contain no parameters.

  • Method body − This contains the set of instructions needed to complete the required activity.

Let us see an example −

Example

 Live Demo

using System;

namespace Demo {
   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 = 90;
         int b = 15;
         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 : 90
登录后复制

以上就是如何在C#中定义自定义方法?的详细内容,更多请关注php中文网其它相关文章!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号