0

0

详细介绍C#中的常用方法

Y2J

Y2J

发布时间:2017-04-21 14:15:14

|

1562人浏览过

|

来源于php中文网

原创

本篇文章介绍了,c#中方法的详细说明。需要的朋友参考下

1.让方法返回多个参数

1.1在方法体外定义变量保存结果

 代码如下:

using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 namespace Method
 {
     class Program
     {
         public static int quotient;
         public static int remainder;
         public static void pide(int x, int y)
         {
             quotient = x / y;
             remainder = x % y;
         }
         static void Main(string[] args)
         {
             Program.pide(6,9);
             Console.WriteLine(Program.quotient);
             Console.WriteLine(Program.remainder);
             Console.ReadKey();
         }
     }
 }

1.2使用输出型和输入型参数

代码如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace Method{
    class Program
    {
       public static void pide(int x, int y, out int quotient, out int remainder) 
       {
            quotient = x / y;
            remainder = x % y;
        }
        static void Main(string[] args)
        {
            int quotient, remainder;
            pide(6,9,out quotient,out remainder);
            Console.WriteLine("{0} {1}",quotient,remainder);
            Console.ReadKey();
        }
    }
}

2.方法的重载

方法重载是面向对象对结构化编程特性的一个重要扩充

构成重载的方法具有以下特点:

(1)方法名相同

(2)方法参数列表不同

判断上述第二点的标准有三点,满足任一点均可认定方法参数列表不同:

睿拓智能网站系统-网上商城
睿拓智能网站系统-网上商城

睿拓智能网站系统-网上商城1.0免费版软件大小:5M运行环境:asp+access本版本是永州睿拓信息专为电子商务入门级用户开发的网上电子商城系统,拥有产品发布,新闻发布,在线下单等全部功能,并且正式商用用户可在线提供多个模板更换,可实现一般网店交易所有功能,是中小企业和个人开展个人独立电子商务商城最佳的选择,以下为详细功能介绍:1.最新产品-提供最新产品发布管理修改,和最新产品订单查看2.推荐产

下载

(1)方法参数数目不同:

(2)方法拥有相同数目的参数,但参数的类型不一样。

(3)方法拥有相同数目的参数和参数类型,但是参数类型出现的先后顺序不一样,

需要注意的是:方法返回值类型不是方法重载的判断条件。

3.方法的隐藏

代码如下:

namespace 方法隐藏
 {
     class Program
     {
         static void Main(string[] args)
         {
             Parent p = new Child();
             p.show();
             Console.ReadKey();
         }
     }
     class Parent
     {
         public void show()
         {
             Console.Write("父类方法");
         }
     }
     class Child : Parent
     {
         public new void show()
         {
             Console.Write("子类方法");
         }
     }
 }

代码如下:

namespace 方法隐藏
{
    class Program
    {
        static void Main(string[] args)
        {
            Parent.show();
            Console.ReadKey();
            Child.show();//父类方法
        }
    }
    class Parent
    {
        public static void show()
        {
            Console.Write("父类方法");
        }
    }
    class Child : Parent
    {
        public static new void show()
        {
            Console.Write("子类方法");
        }
    }
}


在未指明成员存储权限的前提下,其中的成员都是私有的。

代码如下:

namespace 方法隐藏 {
     class Program
     {
         static void Main(string[] args)
         {
             Parent p1= new Parent();
             Parent p2 = new Child();
             p1.show();//父类方法
             p2.show();//父类方法
             ((Child)p2).show();//父类方法
             Console.ReadKey();
         }
     }
     class Parent
     {
         public  void show()
         {
             Console.WriteLine("父类方法");
         }
     }
     class Child : Parent
     {
           new void show()
         {
             Console.WriteLine("子类方法");
         }
     }
 }

4.方法重写和虚方法的调用

 代码如下:

namespace 方法重写
 {
     class Program
     {
         static void Main(string[] args)
         {
             Parent p1 = new Parent();
             Parent p2 = new Child();
             p1.show();
             p2.show();
             ((Parent)p2).show();//子类方法
             Console.ReadKey();
         }
     }
     class Parent
     {
         public virtual void show()
         {
             Console.WriteLine("父类方法");
         }
     }
     class Child:Parent
     {
         public override void show()
         {
             Console.WriteLine("子类方法");
         }
     }
 }

相关专题

更多
虚拟号码教程汇总
虚拟号码教程汇总

本专题整合了虚拟号码接收验证码相关教程,阅读下面的文章了解更多详细操作。

29

2025.12.25

错误代码dns_probe_possible
错误代码dns_probe_possible

本专题整合了电脑无法打开网页显示错误代码dns_probe_possible解决方法,阅读专题下面的文章了解更多处理方案。

20

2025.12.25

网页undefined啥意思
网页undefined啥意思

本专题整合了undefined相关内容,阅读下面的文章了解更多详细内容。后续继续更新。

37

2025.12.25

word转换成ppt教程大全
word转换成ppt教程大全

本专题整合了word转换成ppt教程,阅读专题下面的文章了解更多详细操作。

6

2025.12.25

msvcp140.dll丢失相关教程
msvcp140.dll丢失相关教程

本专题整合了msvcp140.dll丢失相关解决方法,阅读专题下面的文章了解更多详细操作。

2

2025.12.25

笔记本电脑卡反应很慢处理方法汇总
笔记本电脑卡反应很慢处理方法汇总

本专题整合了笔记本电脑卡反应慢解决方法,阅读专题下面的文章了解更多详细内容。

6

2025.12.25

微信调黑色模式教程
微信调黑色模式教程

本专题整合了微信调黑色模式教程,阅读下面的文章了解更多详细内容。

5

2025.12.25

ps入门教程
ps入门教程

本专题整合了ps相关教程,阅读下面的文章了解更多详细内容。

4

2025.12.25

苹果官网入口直接访问
苹果官网入口直接访问

苹果官网直接访问入口是https://www.apple.com/cn/,该页面具备0.8秒首屏渲染、HTTP/3与Brotli加速、WebP+AVIF双格式图片、免登录浏览全参数等特性。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

218

2025.12.24

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
C# 教程
C# 教程

共94课时 | 5.4万人学习

C 教程
C 教程

共75课时 | 3.7万人学习

C++教程
C++教程

共115课时 | 10.1万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

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