Response.Write()和Response.Output.Write()之间有什么区别?

可能重复:
Response.Write()和Response.Output.Write()之间有什么区别?

它与response.write()和response.output.write()的区别如何解释,谢谢你。

看到这个 :

ASP.NET中的Response.Write()Response.Output.Write()之间的区别。 简短的回答是后者为您提供了String.Format-style输出而前者没有。 答案如下。

在ASP.NET中, Response对象的类型为HttpResponse ,当你说Response.Write你真的说(基本上) HttpContext.Current.Response.Write并调用HttpResponse的许多重载的Write方法之一。

Response.Write然后在它的内部TextWriter对象上调用.Write()

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

HttpResponse还有一个名为Output的属性,类型为yes,是的, TextWriter ,所以:

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

这意味着无论TextWriter会让你做什么,你都可以做出Response 。 现在,TextWriters支持Write()方法,也就是String.Format ,所以你可以这样做:

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

但在内部,当然,这种情况正在发生:

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

这里Response.Write():只显示字符串,你不能显示任何其他数据类型值,如int,date等。不允许转换(从一种数据类型到另一种数据类型)。 而Response .Output .Write():你可以通过给出索引值来显示任何类型的数据,如int,date,string等。

这是一个例子:

 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 } 

Response.write()用于显示普通文本, Response.output.write()用于显示格式化文本。

没什么,它们是同义词( Response.Write只是表达写入响应输出的一种较短的方式)。

如果你很好奇, HttpResponse.Write的实现如下:

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

HttpResponse.Output的实现是这样的:

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

如您所见, Response.WriteResponse.Output.Write是真正的同义表达式。

Response.write()不提供格式化输出。 后者允许您编写格式化输出。

Response.write – 它写入文本流Response.output.write – 它写入HTTP输出流。