在不更改byte [] numArray的情况下,即使字节保持完全相同,两个消息框也会显示不同的输出.我糊涂了.
第一个MessageBox的结果:stream:stream to =""version ="1.0"xmlns:stream ="http://etherx.jabber.org/streams">
第二个MessageBox的结果: F^ v
第三个MessageBox:"匹配"
MessageBox.Show(System.Text.Encoding.UTF8.GetString(numArray));
byte[] num1 = numArray;
byte[] encrypted = getEncryptedInit(numArray);
MessageBox.Show(System.Text.Encoding.UTF8.GetString(numArray));
byte[] num2 = numArray;
if (num1.SequenceEqual<byte>(num2) == true)
{
MessageBox.Show("Match");
}
Run Code Online (Sandbox Code Playgroud)
getEncryptedInit必须修改内容numArray.
既然num1和num2都指向numArray,当然它们是等价的.
请记住,数组是引用类型,所以当你说num1 = numArray,你只是将num1变量指向内存中numArray指向的相同位置.如果你真的想捕捉numArray特定时间点的样子,你必须复制它,而不是仅仅做一个简单的任务.
请考虑以下示例:
void DoStuff(byte[] bytes) {
for (int i = 0; i < bytes.Length; i++) {
bytes[i] = 42;
}
}
bool Main() {
// This allocates some space in memory, and stores 1,2,3,4 there.
byte[] numArray = new byte[] { 1, 2, 3, 4 };
// This points to the same space in memory allocated above.
byte[] num1 = numArray;
// This modifies what is stored in the memory allocated above.
DoStuff(numArray);
// This points to the same space in memory allocated above.
byte[] num2 = numArray;
return (num1 == num2 == numArray); // always true
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
101 次 |
| 最近记录: |