C#中实现可变参数实例

2019-12-26 12:12:11于丽

            
            foo(1, 2, "one", "two", p);
            Console.WriteLine();
            foo("one", "two", p, 1, 2 );
        }


        public static void foo(params object[] list)
        {
            foreach(object o in list)
            {
                if (o.GetType() == typeof(int))
                {
                    Console.WriteLine(o);
                }
                else if (o.GetType() == typeof(string))
                {
                    Console.WriteLine(o);
                }
                else if (o.GetType() == typeof(Program))
                {
                    Console.WriteLine(((Program)o).value);
                }
            }
        }
    }
}