19

Possible Duplicate:
What’s the difference between Response.Write() and Response.Output.Write()?

How is it different from response.write() and response.output.write()? Please explain.

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
Surya sasidhar
  • 29,607
  • 57
  • 139
  • 219

5 Answers5

29

See this:

The difference between Response.Write() and Response.Output.Write() in ASP.NET. 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 do the Response whatever a TextWriter will let you. Now, TextWriters support a Write() method aka String.Format, so you can do this:

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

But internally, of course, this is happening:

public virtual void Write(string format, params object[] arg)
{ 
this.Write(string.Format(format, arg)); 
}
Dhay
  • 585
  • 7
  • 29
Graviton
  • 81,782
  • 146
  • 424
  • 602
  • 2
    Stumbled on this from Google - the original content of the link (reproduced in this answer) is from an '04 post on Hanselman's blog - http://www.hanselman.com/blog/ASPNETResponseWriteAndResponseOutputWriteKnowTheDifference.aspx – brichins Jan 08 '16 at 22:38
9

Here Response.Write():to display only string and you can not display any other data type values like int,date,etc.Conversion(from one data type to another) is not allowed. whereas Response .Output .Write(): you can display any type of data like int, date ,string etc.,by giving index values.

Here is example:

protected void Button1_Click(object sender, EventArgs e)
    {
       Response.Write ("hi good morning!"+"is it right?");//only strings are allowed        
       Response.Write("Scott is {0} at {1:d}", "cool", DateTime.Now);//this will give error(conversion is not allowed)
       Response.Output.Write("\nhi goood morning!");//works fine
       Response.Output.Write("Jai is {0} on {1:d}", "cool", DateTime.Now);//here the current date will be converted into string and displayed
    }
Coren
  • 5,517
  • 1
  • 21
  • 34
jai
  • 91
  • 1
  • 1
6

Response.write() is used to display the normal text and Response.output.write() is used to display the formated text.

Kyle Trauberman
  • 25,414
  • 13
  • 85
  • 121
Binod Kumar
  • 61
  • 1
  • 1
1

Nothing, they are synonymous (Response.Write is simply a shorter way to express the act of writing to the response output).

If you are curious, the implementation of HttpResponse.Write looks like this:

public void Write(string s)
{
    this._writer.Write(s);
}

And the implementation of HttpResponse.Output is this:

public TextWriter Output
{
    get
    {
        return this._writer;
    }
}

So as you can see, Response.Write and Response.Output.Write are truly synonymous expressions.

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
1

Response.write() don't give formatted output. The latter one allows you to write formatted output.

Response.write - it writes the text stream Response.output.write - it writes the HTTP Output Stream.

Tzury Bar Yochay
  • 8,798
  • 5
  • 49
  • 73