Asp.net B/S模式下使用Console.Write()

今天在学习C#的格式字符串,教程有如下代码

            double d= 13.45;
            int i = 45;
            Console.Write("The double is {0} and the int contains {1}",d,i);

    我是学asp.net, B/S模式下在类中使用了该代码没用报错,但是页面上并不能输出相应的字符。我知道在页面要输出字符串的话要用Response.Write(),那么是不是把Console.Write("The double is {0} and the int contains {1}",d,i); 改成 Response.Write("The double is {0} and the int contains {1}",d,i),感觉好像行。但是敲完这段代码一条小红线提示你,改方法错误,不能通过编译。很显然Response.Write()并没有相应的参数。

   查了一下资料只要多加一句代码就可用使用Console.Write() ;代码如下:

            double d= 13.45;
            int i = 45;
            Console.SetOut(Response.Output);
            Console.Write("The double is {0} and the int contains {1}",d,i);

   当然用Response.Write()也一样可以,其实Console.Write()方法只是把参数的完整列表传送给静态方法string.Format()。

Response.Write(string.Format("The double is {0} and the int contains {1}",d,i));

  

 

 

posted @ 2012-07-22 17:13  showblog  阅读(1374)  评论(0编辑  收藏  举报