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