2 c#
任何人都可以解释为什么这段代码的输出只是'你好'以及这段代码的含义是什么?
( 0, characterArray, 0, characterArray.Length );
Run Code Online (Sandbox Code Playgroud)
输出显示:
字符数组是:hello
代码如下:
string string1 = "hello there";
char[] characterArray = new char[ 5 ];
string1.CopyTo( 0, characterArray, 0, characterArray.Length );
Console.Write( "\nThe character array is: " );
for ( int i = 0; i < characterArray.Length; i++ )
Console.Write( characterArray[ i ] );
Run Code Online (Sandbox Code Playgroud)
这是因为您的数组仅设置为5个字符.将它扩展为11,它将工作.
以下是Copyto的内容:
public void CopyTo(
int sourceIndex,
char[] destination,
int destinationIndex,
int count
)
Run Code Online (Sandbox Code Playgroud)
Parameters sourceIndex Type: System..::.Int32 A character position in this instance. destination Type: array[]()[] An array of Unicode characters. destinationIndex Type: System..::.Int32 An array element in destination. count Type: System..::.Int32 The number of characters in this instance to copy to destination.
摘自:http://msdn.microsoft.com/en-us/library/system.string.copyto.aspx