String.Format vs +运算符 [英] String.Format vs + operator

查看:117
本文介绍了String.Format vs +运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪个更好,哪个更快,哪个更容易等... ?????

String.Format(" yadda {0} yadda {1}" ;,x,y)


" yadda" + x.ToString()+" yadda" + y.tostring();

我的代码有两种混合物,如果我要标准化

,我还会感到很好吗?

Thnanks,


JIM

解决方案

6月25日上午11点10分,詹姆斯 < nos ... @ hypercon.netwrote:


哪个更好,哪个更快,哪个更容易等...... ????? >

String.Format(" yadda {0} yadda {1}",x,y)


" yadda" + x.ToString()+" yadda" + y.tostring();


我的代码有两种混合物,如果我应该标准化

或者不是吗?



除了其他海报提到的内容外,请记住,如果您想要将代码国际化,那么+ "操作员方式是

非首发。


2007年6月25日星期一20:18:41 + 0200,尼古拉斯Paldino [.NET / C#MVP]< mv*@spam.guard.caspershouse.comwrote:


James,


使用+运算符会更快,因为它只是预先分配

字符串并将各种元素复制在一起。使用Format,格式化

字符串,这将需要一些开销。


但是,我认为你应该仔细考虑开销

与代码的可读性/可维护性相比,因为我认为在这个领域中格式化的呼叫要好得多。


当然,如果你执行了大量的这些操作,那么你可以通过移动到concat

运算符(+)来获得一些性能。



在某些情况下我同意,但在许多情况下,通过不调用ToString()可以大大提高可读性,在这种情况下OP''示例缩小downto


string s =" yadda" + x + yadda + y;


我认为它更具可读性(语法着色会使它更具可读性)比


string s = string。格式(yadda {0} yadda {1},x,y);

PS!如果字符串连接的任何部分包含字符串文字,ToString()也会在其他部分自动调用。


-

快乐编码!

Morten Wennevik [C#MVP]


好的,谢谢,


JIM

" Tom Spink" < ts **** @ gmail.com在消息中写道

新闻:uj ************** @ TK2MSFTNGP03.phx.gbl ...


James写道:


>哪个更好,哪个更快,哪个更容易等...? ????

String.Format(" yadda {0} yadda {1}",x,y)

" yadda" + x.ToString()+" yadda" + y.tostring();

我的代码有两种混合物,如果我应该标准化或者不标准化,我会感到很好吗?

Thnanks,

JIM



嗨Jim,


IIRC,连接速度更快,但你应该选择基础关于你是否实际上需要在连接字符串时获得速度(即你的应用程序性能是否很重要)然后决定是否应该牺牲
牺牲代码可读性略有提升。


-

Tom Spink

爱丁堡大学



Which is better, which is faster, which is easier etc... ?????

String.Format ( "yadda {0} yadda {1}", x, y )

"yadda" + x.ToString() + " yadda" + y.tostring();
My code has a mish mash of both and I am wondewring if I should standardize
or not ??
Thnanks,

JIM

解决方案

On Jun 25, 11:10 am, "James" <nos...@hypercon.netwrote:

Which is better, which is faster, which is easier etc... ?????

String.Format ( "yadda {0} yadda {1}", x, y )

"yadda" + x.ToString() + " yadda" + y.tostring();

My code has a mish mash of both and I am wondewring if I should standardize
or not ??

In addition to what other posters have mentioned, keep in mind that if
you ever want to internationalize your code, the "+" operator way is a
non-starter.


On Mon, 25 Jun 2007 20:18:41 +0200, Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.comwrote:

James,

Using the + operator will be faster, as it will just pre-allocate the
string and copy the various elements together. With Format, the format
string is parsed, and that is going to require some overhead.

However, I do think that you should carefully consider the overhead
versus the readability/maintainability of the code, as I think that the call
to Format is MUCH much better in this arena.

Of course, if you have a good number of these operations being
performed, then you could gain some performance by moving to the concat
operator (+).

In some cases I agree, but in many cases readability can be much improved by not calling ToString(), in which case the OP''s example narrows downto

string s = "yadda " + x + " yadda " + y;

which I would argue is more readable (syntax coloring will make it even more readable) than

string s = string.Format("yadda {0} yadda {1}", x, y);
PS! If any of the parts of a string concatenation contains a string literal, ToString() will automatically be called on other parts as well.

--
Happy coding!
Morten Wennevik [C# MVP]


Ok, thanks,

JIM
"Tom Spink" <ts****@gmail.comwrote in message
news:uj**************@TK2MSFTNGP03.phx.gbl...

James wrote:

>Which is better, which is faster, which is easier etc... ?????

String.Format ( "yadda {0} yadda {1}", x, y )

"yadda" + x.ToString() + " yadda" + y.tostring();
My code has a mish mash of both and I am wondewring if I should
standardize or not ??
Thnanks,

JIM


Hi Jim,

IIRC, concatenation is faster, but you should choose based on whether you
actually need to gain speed when concatenating strings (i.e. is your
application performance critical) and then decided whether you should
sacrifice code readability for a slight performance improvement.

--
Tom Spink
University of Edinburgh



这篇关于String.Format vs +运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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