The example in this paper describes the method of C # for fast copying with pointers. Share to everyone for your reference. The implementation method is as follows:
fastcopy.cs//compile-time use:/unsafeusing system;class test{//unsafe keyword allows pointers to be used in the following//methods: Static unsafe void Copy (byte[] Sr c, int srcindex, byte[] DST, int dstindex, int count) {if (src = = NULL | | Srcindex < 0 | | DST = = NULL | | Dstindex < 0 | | Count < 0) {throw new ArgumentException (); } int srclen = src. Length; int Dstlen = DST. Length; if (Srclen-srcindex < count | | Dstlen-dstindex < count) {throw new ArgumentException (); }//The following fixed statement fixed//SRC objects and DST objects in memory position so that both objects//are not moved by garbage collection. Fixed (byte* PSRC = src, pDst = DST) {byte* PS = PSRC; byte* PD = pDst; Loop count in 4-byte blocks, one copy//integer (4 bytes): for (int n = 0; n < count/4; n++) {* ((int*) pd) = * ((i nt*) PS); PD + = 4; PS + = 4; }//Move all bytes not moved with a 4-byte block,//To complete replication: for (int n = 0; n < count% 4; n++) {*pd = *ps; pd++; ps++; }}}} static void Main (StrinG[] args) {byte[] a = new byte[100]; Byte[] B = new byte[100]; for (int i = 0; i < ++i) a[i] = (byte) i; Copy (A, 0, b, 0, 100); Console.WriteLine ("The first elements is:"); for (int i = 0; i < ++i) Console.Write (B[i] + ""); Console.WriteLine ("\ n"); }}
Hopefully this article will help you with your C # programming.