logo search
CSharp_Prog_Guide

Использование указателей для копирования массива байтов

В следующем примере указатели используются для копирования байт из одного массива в другой.

В данном примере применяется ключевое слово unsafe, которое позволяет использовать указатели в методе Copy. Оператор fixed используется для объявления указателей исходного и конечного массива. Это закрепляет расположение исходного и конечного массива в памяти, чтобы они не перемещались при сборке мусора. Закрепление этих блоков памяти отменяется, когда завершается обработка блока fixed. Поскольку функция копирования в данном примере использует ключевое слово unsafe, она должна быть скомпилирована с параметром компилятора /unsafe.

Пример

------

static void Main()

{

byte[] a = new byte[100];

byte[] b = new byte[100];

for (int i = 0; i < 100; ++i)

{

a[i] = (byte)i;

}

Copy(a, 0, b, 0, 100);

System.Console.WriteLine("The first 10 elements are:");

for (int i = 0; i < 10; ++i)

{

System.Console.Write(b[i] + " ");

}

System.Console.WriteLine("\n");

}

}

The first 10 elements are:

0 1 2 3 4 5 6 7 8 9

-----

XML Documentation Comments

In Visual C# you can create documentation for your code by including XML tags in special comment fields in the source code directly before the code block they refer to. For example:

/// <summary>

/// This class performs an important function.

/// </summary>

public class MyClass{}

When you compile with /doc the compiler will search for all XML tags in the source code and create an XML documentation file.

Note:

The XML doc comments are not metadata; they are not included in the compiled assembly and therefore they are not accessible through reflection.

Recommended Tags for Documentation Comments

The C# compiler will process documentation comments in your code to an XML file. Processing the XML file to create documentation is a detail that has to be implemented at your site.

Tags are processed on code constructs such as types and type members.

Note:

Documentation comments cannot be applied to a namespace.

The compiler will process any tag that is valid XML.