日期:2014-05-16  浏览次数:20907 次

C#初学者,很简单的一道题目
using System;
class Test
{
  static void F(params int[]args)
  {
      Console.WriteLine("# of argument:{0}",args.Length);
      for(int i=0;i<args.Length;i++)
          Console.WriteLine("\targs[{0}]={1}",i,args[i]);
  }
}
static void Main()
{
F();
F(1);
F(1,2);
F(1,2,3);
F(new int[]{1,2,3,4});
}

求教哪儿错了?
------解决方案--------------------
F(); ==> args =null ==>(args.Length) argumentnullexception
------解决方案--------------------
F();这个函数没有参数,访问null出错
------解决方案--------------------

你main放在class里,就不会错了,
namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            F();
            F(1);
            F(1, 2);
            F(1, 2, 3);
            F(new int[] { 1, 2, 3, 4 });
        }
        static void F(params int[] args)
        {
            Console.WriteLine("# of argument:{0}", args.Length);
            for (int i = 0; i < args.Length; i++)
                Console.WriteLine("\targs[{0}]={1}", i, args[i]);
            Console.ReadKey(); 
        }
    }
}


------解决方案--------------------
将主函数Main放到类里面