110,571
社区成员
发帖
与我相关
我的任务
分享
public static void Test1()
{
string s = "123";
s += "456" + 1.ToString() + "789";
}
.method public hidebysig static void Test1() cil managed
{
// 代码大小 34 (0x22)
.maxstack 4
.locals init ([0] string s,
[1] int32 CS$0$0000)
IL_0000: nop
IL_0001: ldstr "123"
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: ldstr "456"
IL_000d: ldc.i4.1
IL_000e: stloc.1
IL_000f: ldloca.s CS$0$0000
IL_0011: call instance string [mscorlib]System.Int32::ToString()
IL_0016: ldstr "789"
IL_001b: call string [mscorlib]System.String::Concat(string,
string,
string,
string)
IL_0020: stloc.0
IL_0021: ret
} // end of method Class1::Test1
public static void Test2()
{
string s = "123";
s = string.Concat(s, "456", 1.ToString(), "789");
}
.method public hidebysig static void Test2() cil managed
{
// 代码大小 34 (0x22)
.maxstack 4
.locals init ([0] string s,
[1] int32 CS$0$0000)
IL_0000: nop
IL_0001: ldstr "123"
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: ldstr "456"
IL_000d: ldc.i4.1
IL_000e: stloc.1
IL_000f: ldloca.s CS$0$0000
IL_0011: call instance string [mscorlib]System.Int32::ToString()
IL_0016: ldstr "789"
IL_001b: call string [mscorlib]System.String::Concat(string,
string,
string,
string)
IL_0020: stloc.0
IL_0021: ret
} // end of method Class1::Test2
string s = '1' + '2' + "";
s = "" + '1' + '2';
[CLSCompliant(false)]
public static String Concat(Object arg0, Object arg1, Object arg2, Object arg3, __arglist)
{
Object[] objArgs;
int argCount;
ArgIterator args = new ArgIterator(__arglist);
。。。
return Concat(objArgs);
}
public static String Concat(params Object[] args) {
if (args==null) {
throw new ArgumentNullException("args");
}
。。。
return ConcatArray(sArgs, totalLength);
}
private static String ConcatArray(String[] values, int totalLength) {
String result = FastAllocateString(totalLength);
int currPos=0;
。。。
FillStringChecked(result, currPos, values[i]);
currPos+=values[i].Length;
}
return result;
}
unsafe private static void FillStringChecked(String dest, int destPos, String src)
{
int length = src.Length;
if (length > dest.Length - destPos) {
throw new IndexOutOfRangeException();
}
fixed(char *pDest = &dest.m_firstChar)
fixed (char *pSrc = &src.m_firstChar) {
wstrcpy(pDest + destPos, pSrc, length);
}
}
private static unsafe void wstrcpy(char *dmem, char *smem, int charCount)
{
if (charCount > 0) {
#if ALIGN_ACCESS
if ((((int)dmem | (int)smem) & 1) == 0) {
#endif
// First Align dmem to a pointer boundary
if (((int)dmem & 2) != 0)
{
dmem[0] = smem[0];
dmem += 1;
smem += 1;
charCount -= 1;
}
#if WIN64
。。。
}