Scott Hanselman

ASP.NET, Response.Write and Response.Output.Write - know the difference

January 03, 2004 Comment on this post [3] Posted in ASP.NET
Sponsored By

A fellow emailed me just now wanting to know the difference between Response.Write() and Response.Output.Write() in ASP.NET.  Well sir, I'm glad you asked, because it's damned interesting. :)  The short answer is that the latter gives you String.Format-style output and the former doesn't.  The long answer follows.

In ASP.NET the Response object is of type HttpResponse and when you say Response.Write you're really saying (basically) HttpContext.Current.Response.Write and calling one of the many overloaded Write methods of HttpResponse. 

Response.Write then calls .Write() on it's internal TextWriter object:

public void Write(object obj){ this._writer.Write(obj);}

HttpResponse also has a Property called Output that is of type, yes, TextWriter, so:

public TextWriter get_Output(){ return this._writer; }

Which means you can to the Response whatever a TextWriter will let you.  Now, TextWriters support a Write() method ala String.Format, so you can do this:

Response.Output.Write("Scott is {0} at {1:d}", "cool",DateTime.Now);

But internally, of course, this this is happening:

public virtual void Write(string format, params object[] arg)
{
   this.Write(string.Format(format, arg)); 
}

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
April 29, 2004 12:05
rerdffdfefd
ali
April 29, 2004 12:05
rerdffdfefd
ali
April 29, 2004 12:06
rerdffdfefd
ali

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.