您如何看待这段代码? [英] What do you think of this code?

查看:73
本文介绍了您如何看待这段代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个项目中找到了这个代码:


StringBuilder sb = new StringBuilder();

sb.Append(" SELECT * FROM SomeTable" )

。附近(WHERE SomeField = SomeValue)

。附近(订购等等等)


起初我以为它使用的是使用声明,但事实并非如此。它工作

因为sb.Append返回自己的实例。我有兴趣听到

大家都在想什么。


-

Michael Culley

I found this code in a project:

StringBuilder sb = new StringBuilder();
sb.Append("SELECT * FROM SomeTable")
.Append("WHERE SomeField = SomeValue")
.Append("ORDER BY etc etc etc")

at first I thought it was using a using statement but it''s not. It works
because sb.Append returns an instance of itself. I''d be interested to hear
what everyone thinks of it.

--
Michael Culley

推荐答案

Michael Culley< mc ***** @ NOSPAMoptushome.com.au>写道:
Michael Culley <mc*****@NOSPAMoptushome.com.au> wrote:
我在一个项目中找到了这个代码:

StringBuilder sb = new StringBuilder();
sb.Append(" SELECT * FROM SomeTable")
.Append(WHERE SomeField = SomeValue)
.Append(ORDER BY etc etc等)

起初我以为它使用的是使用声明但是不是。它起作用
因为sb.Append返回自己的实例。我很想听听每个人对它的看法。
I found this code in a project:

StringBuilder sb = new StringBuilder();
sb.Append("SELECT * FROM SomeTable")
.Append("WHERE SomeField = SomeValue")
.Append("ORDER BY etc etc etc")

at first I thought it was using a using statement but it''s not. It works
because sb.Append returns an instance of itself. I''d be interested to hear
what everyone thinks of it.




假设下一行使用ToString,开发人员本来就是<更好的写作:


string x =" SELECT * FROM SomeTable" +

" WHERE SomeField = SomeValue" +

" ORDER BY等等等等;;


这实际上比使用StringBuilder快得多,因为:


1)如果该值实际上是一个常量,那么它确实非常快。

连接字符串常量由C#编译器连接,

不在运行时。

2)如果不是,它最终会调用String.Concat,它能够分配足够的空间来开始 -

StringBuilder版本将至少涉及一次重新分配。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



Assuming the next line uses "ToString", the developer would have been
better writing:

string x = "SELECT * FROM SomeTable "+
"WHERE SomeField=SomeValue "+
"ORDER BY etc etc etc";

That would actually be much faster than using StringBuilder, as:

1) If the value is actually a constant, it will be very fast indeed.
Concatenated string constants are concatenated by the C# compiler,
not at runtime.
2) If it''s not, it ends up as one call to String.Concat, which is able
to allocate the right amount of space to start with - the
StringBuilder version will involve at least one reallocation.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too





没关系,Append方法返回相同的实例,所以你不是

创建一个新对象


现在我假设您在Append()方法中替换参数,如果没有

它有一个开销,因为您不需要使用StringBuilder。它只需要更简单:


string s =" SELECT * FROM SomeTable WHERE SomeField = SomeValue ....." ;;

干杯,

-

Ignacio Machin,
ignacio.machin at dot.state。 fl.us

佛罗里达州交通局

" Michael Culley" < MC ***** @ NOSPAMoptushome.com.au>在留言中写道

新闻:%2 *************** @ TK2MSFTNGP14.phx.gbl ...
Hi,

It''s ok , the Append method return the same instance so you are not
creating a new object

Now I assume that you replace the parameter in the Append() method, if not
it has an overhead, as you don''t need to use StringBuilder for that. it
would be simpler to just do:

string s= "SELECT * FROM SomeTable WHERE SomeField = SomeValue .....";
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Michael Culley" <mc*****@NOSPAMoptushome.com.au> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
我发现了这个项目中的代码:

StringBuilder sb = new StringBuilder();
sb.Append(" SELECT * FROM SomeTable")
.Append(&WHERE SomeField = SomeValue" ;)
.Append(ORDER BY等等)

起初我以为它使用的是使用声明,但事实并非如此。它起作用
因为sb.Append返回自己的实例。我很想听听每个人对它的看法。

-
Michael Culley
I found this code in a project:

StringBuilder sb = new StringBuilder();
sb.Append("SELECT * FROM SomeTable")
.Append("WHERE SomeField = SomeValue")
.Append("ORDER BY etc etc etc")

at first I thought it was using a using statement but it''s not. It works
because sb.Append returns an instance of itself. I''d be interested to hear
what everyone thinks of it.

--
Michael Culley



" Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道

新闻:MP ************************ @ msnews.microsoft.c om ...
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
[concatenation]实际上比使用StringBuilder快得多,
因为:

2)...它最终成为对String的一次调用。 Concat,能够分配适当数量的空间 -
StringBuilder版本将至少涉及一次重新分配。
[concatenation] would actually be much faster than using StringBuilder,
as:

2) ... it ends up as one call to String.Concat, which is able
to allocate the right amount of space to start with - the
StringBuilder version will involve at least one reallocation.




虽然,如果StringBuilder构造的缓冲区大于初始大小为16字节的缓冲区大于
,那么它也可以避免分配。我提到这一点是因为将初始大小和/或
字符串传递给StringBuilder ctor可能是最常被忽略的

优化之一,至少在代码I'中'见过。你经常知道

最终结果会有多大,或者你至少知道

的平均值,它几乎总是超过16个字节。默认情况下,

StringBuilder将从16开始,然后跳转到32,64,128等。在

OP'的设计示例中,或许是一个起始大小512,取决于SQL语句的预期长度,可能会更好,并且通常会将任何新分配保留为零或一个而不是半个

打。


--BOB



Although, if the StringBuilder had been constructed with a buffer larger
than the default initial size of 16 bytes in the first place, it would also
avoid allocation. I mention this because passing an initial size and/or
string to the StringBuilder ctor is perhaps one of the most often-overlooked
optimizations, at least in the code I''ve seen. Quite often you know about
how big the final result will be, or you at least have an idea of the
average, and it''s almost always way more than 16 bytes. By default,
StringBuilder will start with 16, then jump to 32, 64, 128, etc. In the
OP''s contrived example, a starting size of perhaps 512, depending on the
expected length of the SQL statement, would probably be better and would
usually keep any new allocations to zero or one rather than perhaps a half
dozen.

--Bob


这篇关于您如何看待这段代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆