为什么在 C# 中使用 String.Concat()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1787696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why use String.Concat() in C#?
提问by chobo2
I been wondering this for a while. Why use String.Concat()
instead of using the +
operator. I understand the String.Format
since it a voids using the +
operator and make your code looker nicer.
我一直在想这个问题。为什么使用String.Concat()
而不是使用+
运算符。我理解String.Format
它,因为它使用+
操作符使你的代码看起来更好。
Like for example:
例如:
string one = "bob";
string two = "jim";
string three = one + two;
string three = String.Concat(one, two);
采纳答案by Guffa
As long as the two operands are strings, there is no difference at all between using the +
operator and the String.Concat
method. The code using the +
operator actually compiles into a String.Concat
call.
只要两个操作数都是字符串,使用+
操作符和使用String.Concat
方法就没有任何区别。使用+
运算符的代码实际上编译为String.Concat
调用。
Use the one that best represents your intention with the code.
使用最能代表您的代码意图的那个。
回答by tster
Because you can use the version that takes two objects ;)
因为您可以使用带有两个对象的版本;)
回答by Suroot
To be honest; some people prefer it. I know I would rather use Concat as the + really isn't very intuitive. Especially just looking at the code deep down; it's hard to tell if those are numbers you're adding or two strings your concating or two points that you're doing vector addition with. :)
老实说; 有些人更喜欢它。我知道我宁愿使用 Concat,因为 + 确实不是很直观。尤其是深入查看代码;很难判断这些是您要添加的数字还是您的连接的两个字符串或您正在使用向量加法的两个点。:)
回答by Jeff
I believe it's the same as string a + string b. String type in C# being immutabletherefore the preferred way of string manipulation is through StringBuilder. As Guffa has concluded "Use the one that best represents your intention with the code." Of course for simple usage where performance isn't too much of a concern use + or concat.
我相信它与字符串 a + 字符串 b 相同。C# 中的字符串类型是不可变的,因此字符串操作的首选方式是通过 StringBuilder。正如 Guffa 得出的结论,“使用最能代表您的代码意图的那个。” 当然,对于性能不太重要的简单用法,请使用 + 或 concat。
回答by Kyle Hodgson
Portability (yours).
便携性(你的)。
In other languages, + might not concatenate as you expect when presented with two strings. It might do something altogether unexpected. Or, in some loosely typed languages if you pit the + between an integer or numeric scalar variable and a string, it might not throw a compiler warning, try to convert the strung to a numeric, and add them.
在其他语言中,当显示两个字符串时,+ 可能不会像您期望的那样连接。它可能会做一些完全出乎意料的事情。或者,在一些松散类型的语言中,如果你在整数或数字标量变量和字符串之间插入 +,它可能不会抛出编译器警告,尝试将字符串转换为数字,然后添加它们。
In comparison, .concat() is quite obvious and readable compared to + in some cases.
相比之下,在某些情况下, .concat() 与 + 相比非常明显和可读。
回答by Illuminati
I myself have been having this same question and this question triggered me to investigate into it a bit,
我自己也有同样的问题,这个问题促使我对此进行了一些调查,
I created the following class
我创建了以下课程
public class Class1
{
string str = "One" + "Team";
string str2 = string.Concat("One", "Team");
}
And below is corresponding IL code for that.
下面是相应的 IL 代码。
.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 40 (0x28)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldstr "OneTeam"
IL_0006: stfld string StringConcat.Class1::str
IL_000b: ldarg.0
IL_000c: ldstr "One"
IL_0011: ldstr "Team"
IL_0016: call string [mscorlib]System.String::Concat(string, string)
IL_001b: stfld string StringConcat.Class1::str2
IL_0020: ldarg.0
IL_0021: call instance void [mscorlib]System.Object::.ctor()
IL_0026: nop
IL_0027: ret
// end of method Class1::.ctor
}
For me it definitely looks like string.Concat
is having more steps than overloaded + operator. But I know for sure inside the System.String
class there will be similar set of operations happening for overloaded + operator as well. Thoughts?
对我来说,它看起来肯定string.Concat
比重载 + 运算符有更多的步骤。但我确信在System.String
类内部也会有类似的操作集发生在重载 + 运算符上。想法?
回答by Jon Skeet
I use +
when I know how many strings are going to be concatenated - but what if you just have an array? In that case you wouldn't know how many times to apply +
, so you'd haveto call a method (or loop yourself, which is horrible).
我用+
的时候我知道有多少串将要连接起来-但如果你只是有一个数组?在这种情况下,您将不知道应用多少次+
,因此您必须调用一个方法(或自己循环,这太可怕了)。
I can't remember calling string.Concat
very often though - it's a definite rarity.
我不记得string.Concat
经常打电话- 这绝对是罕见的。
As guffa says, +
compiles down into calls to string.Concat
anyway - it's worth being aware that string
doesn't actually havea + operator, which can be a cause of confusion if you ever try to use it in reflection!
正如古法所说,无论如何+
编译成调用string.Concat
- 值得注意的是,string
它实际上没有+ 运算符,如果您尝试在反射中使用它,这可能会导致混淆!
One benefit of +
though is that if all the arguments are constant expressions, the compiler will perform the concatenation for you, so you don't need to do it at execution time. That sort of tiny performance benefit isn't going to be significant in most code, but it's always nice when the code I find most readable alsohas a performance advantage :)
+
虽然的一个好处是,如果所有参数都是常量表达式,编译器将为您执行连接,因此您无需在执行时执行此操作。这种微小的性能优势在大多数代码中并不重要,但是当我发现最易读的代码也具有性能优势时,它总是很好:)
回答by user5816960
The + operator is a mathematical operator. Therefore before concatenating string using that operator, the compiler will have to decide whether to use it as a math operator for addition or use it for string concatenation.
+ 运算符是数学运算符。因此,在使用该运算符连接字符串之前,编译器必须决定是将其用作数学运算符进行加法还是将其用于字符串连接。
For example: if you are concatenating a+b+c+d+e+f, the compiler will make the decision 5 times as there are 5 + operator.
例如:如果您连接 a+b+c+d+e+f,编译器将做出 5 次决策,因为有 5 个 + 运算符。
Performancewise it`s not recommended.
性能方面不推荐。
However, if you have only one concatenation to do, i don`t think there is much difference in terms of performance while using + or the Concat() method
但是,如果您只有一个连接要做,我认为使用 + 或 Concat() 方法在性能方面没有太大差异