资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C#中有哪些比较常用的方法-创新互联

今天小编给大家分享的是C#中有哪些比较常用的方法,相信很多人都不太了解,为了让大家更加了解C#中比较常用的方法,所以给大家总结了以下内容,一起往下看吧。一定会有所收获的哦。

成都创新互联-专业网站定制、快速模板网站建设、高性价比广灵网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式广灵网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖广灵地区。费用合理售后完善,10年实体公司更值得信赖。

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)方法参数数目不同:

(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("子类方法");
         }
     }
 }

以上就是C#中有哪些比较常用的方法的详细内容了,看完之后是否有所收获呢?如果想了解更多相关内容,欢迎来创新互联成都网站设计公司行业资讯!

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


新闻标题:C#中有哪些比较常用的方法-创新互联
URL链接:http://cdkjz.cn/article/coejih.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220