函数 都以static开头

 

namespace 可变参数的函数

{

    class Program

    {

        static void Main(string[] args)

        {

            string[] names = { "Coco", "Lily", "Lucy" };

            string country = "China";

            VFunc(names);

            VFunc(country);

      

            SayHello("Tom", "张大虎", "狗剩");

            Console.ReadKey();

        }

        static void VFunc(params string[] values)  //params 表示参数的数量可变。用一个数组来接收。

        {

            foreach (string value in values)

                Console.WriteLine(value);

        }

 

        //长度不可变与长度可变参数结合

        static void SayHello(string name, params string[] nickName) //params参数必须是形参表中的最后一个。

        {

            Console.WriteLine("我的名字是{0}", name);

            foreach (string nickname in nickName)

            {

                Console.WriteLine("我的昵称是{0}", nickname);

            }

        }

    }

}

 

函数重载

 

namespace 函数重载  //参数类型不同或个数不同才可以重载,而与返回值无关

{

    class Program

    {

        static void Main(string[] args)

        {

            SayHello("Tom");

            SayHello(18);

            Console.ReadKey();

        }

        static void SayHello(string name)

        {

            Console.WriteLine("我的名字是{0}", name);

        }

        static void SayHello(int age)

        {

            Console.WriteLine("我今年{0}岁了",age);

        }

    }

}

相关文章:

  • 2021-07-02
  • 2021-08-13
  • 2022-12-23
  • 2021-07-10
  • 2021-08-17
  • 2021-06-20
  • 2022-01-09
  • 2022-01-22
猜你喜欢
  • 2021-09-03
  • 2021-06-13
  • 2021-08-04
  • 2021-08-20
  • 2021-12-05
  • 2022-12-23
  • 2021-06-08
相关资源
相似解决方案