【发布时间】:2016-02-08 09:59:10
【问题描述】:
这可能是一个真正的初学者问题,但我一直在阅读这方面的内容,但我发现它很难理解。
这是来自msdn 页面的关于此主题的示例(只是稍微小一点)。
using System;
class SetByteDemo
{
// Display the array contents in hexadecimal.
public static void DisplayArray(Array arr, string name)
{
// Get the array element width; format the formatting string.
int elemWidth = Buffer.ByteLength(arr) / arr.Length;
string format = String.Format(" {{0:X{0}}}", 2 * elemWidth);
// Display the array elements from right to left.
Console.Write("{0,7}:", name);
for (int loopX = arr.Length - 1; loopX >= 0; loopX--)
Console.Write(format, arr.GetValue(loopX));
Console.WriteLine();
}
public static void Main()
{
// These are the arrays to be modified with SetByte.
short[] shorts = new short[2];
Console.WriteLine("Initial values of arrays:\n");
// Display the initial values of the arrays.
DisplayArray(shorts, "shorts");
// Copy two regions of source array to destination array,
// and two overlapped copies from source to source.
Console.WriteLine("\n" +
" Array values after setting byte 1 = 1 and byte 3 = 200\n");
Buffer.SetByte(shorts, 1, 1);
Buffer.SetByte(shorts, 3, 10);
// Display the arrays again.
DisplayArray(shorts, "shorts");
Console.ReadKey();
}
}
SetByte 应该很容易理解,但是如果我在执行SetByte 操作之前打印短裤数组,则数组看起来像这样
{short[2]}
[0]: 0
[1]: 0
做完第一个Buffer.SetByte(shorts, 1, 1);后数组就变成了
{short[2]}
[0]: 256
[1]: 0
设置Buffer.SetByte(shorts, 3, 10);后数组变成
{short[2]}
[0]: 256
[1]: 2560
最后,在示例中,他们从右到左打印数组:
0A00 0100
我不明白这是如何工作的,谁能给我一些关于这个的信息?
【问题讨论】: