params 关键字载入空值的陷阱

class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            p.ShowInfo("cjijhsakj", 100, null);//参数赋null值,就会得不到效果
        }
        public void ShowInfo(params object[] parameters)
        {
            string strOutPut = string.Empty;
            for (int i = 0; i < parameters.Length; i++)
            {
                if (parameters[i] == null)//修复载入载入空值的问题
                {
                    strOutPut += "null" + "\r\n";
                }
                else
                {
                    strOutPut += parameters[i].ToString() + "\r\n";
                }
            }
            Console.Write("参数列表:\n{0}", strOutPut);
            Console.Read();
        }
    }

posted @ 2013-03-10 10:27  才高远志  阅读(170)  评论(0编辑  收藏  举报