C # Advanced Programming 75 days----C # using pointers

Source: Internet
Author: User

syntax for using pointers in C #

Suppose you want to use pointers in C # , First configure the project :

Do you see the attributes? Click :

Do you see that consent-insecure code? selected

The operation of the pointer and address is then placed in the unsafe statement block . Use unsafe keyword is telling the compiler that the code here is unsafe .

use of unsafe keyword :

(1) Before the function , Modify the function , indicating that the function of the inside or the shape of the function involves a pointer operation :

unsafe static void FastCopy (byte[] src, byte[] DST, int count)

{

Unsafe Context:can use pointers here.

}

The orientation of the unsafe context extends from the list of references to the end of the method , So the pointer is used as the function's parameter to use the unsafekeyword.

(2) Placing pointers on unsafe blocks declared by unsafe

unsafe{

Unsafe Context:can Use pointers here

}

Case :

Using System;

Using System.Collections.Generic;

Using System.Linq;

Using System.Text;

Using System.Threading.Tasks;

using pointers in namespace Csharp

{

Class Program

{

static void Main (string[] args)

{

int i = 1;

Unsafe

{

Increment (&i);

}

Console.WriteLine (i+ "\ n");

demonstrates how to manipulate strings with pointers

string s = "Code Project is cool";

Console.WriteLine ("The original string:");

Console.WriteLine ("{0}\n", s);

Char[] B = new char[100];

copies the characters of the specified bibliography to a specified position in a Unicode character array from the specified position in this instance

S.copyto (0, B, 0, 20);

Console.WriteLine ("The Encode string:");

Unsafe

{

Fixed (char* p = b)

{

Nencodedecode (P);

}

}

for (int t = 0; t < t++)

{

Console.WriteLine (B[t]);

}

Console.WriteLine ("\ n");

Console.WriteLine ("The Decoded string:");

Unsafe

{

Fixed (char* p = b)

{

Nencodedecode (P);

}

}

for (int t = 0; t < t++)

Console.Write (B[t]);

Console.WriteLine ();

Console.readkey ();

}

Unsafe public static void Increment (int* p)

{

*p = *p + 1;

}

<summary>

An operation that encodes a string through an XOR operation . Suppose you put a string into this

function , The string is encoded , Assuming that the encoded character is fed into the function ,

This string will

decoding

</summary>

<param name= "S" ></param>

Unsafe public static void Nencodedecode (char* s)

{

int W;

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

{

w = (int) * (s + i);

w = w ^ 5;// bitwise XOR

* (s + i) = (char) W;

}

}

}

}

When the fixed is used, the following is necessary to introduce it :

Fixed statements can only be in the context of today's insecure ,c# compiler just agree fixed Span style= "font-family: Arial" > The pointer to a managed variable is assigned cannot be changed in fixed The pointer initialized in the statement

The fixed statement prevents the garbage collector from locating the movable variable . when you use it before a statement or function fixed when , you are telling . NET Platform's garbage collector , before this statement or function is finished running , Do not reclaim the memory space that it occupies .

C # Advanced Programming 75 days----C # using pointers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.