ES CSharp Operator Pointer - LIHACHTETAN/ClassicUO-Bad-Newbie-CSharp-Scripts-IDE GitHub Wiki
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
Operaciones con punteros
& obtiene la dirección, * cambia el nombre del puntero, -> accede al miembro del objetivo del puntero. Requiere cumplimiento de vida útil de memoria y no seguro.
Ejemplo sencillo (Example.cs)
Guarde este ejemplo por separado como Example.cs. Ejecútelo independientemente de los demás ejemplos de esta página.
Example.cs:
// PROJECT-EXAMPLE
unsafe
{
int value = 3;
int* pointer = &value;
*pointer = 7;
System.Console.WriteLine(value);
}
Habilite <AllowUnsafeBlocks>true</AllowUnsafeBlocks> en el proyecto. El puntero solo es válido dentro del ámbito mostrado.
Resultado:
7
Sintaxis
Type* pointer = &value; / *pointer / pointer->Member
Ejemplo 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
public static class Program
{
public static async Task Main(string[] args)
{
unsafe { int value = 3; int* pointer = &value; *pointer = 9; Console.WriteLine(value); }
}
}
Ejemplo 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
public static class Program
{
public static async Task Main(string[] args)
{
unsafe { Point value = new() { X = 2 }; Point* pointer = &value; pointer->X += 5; Console.WriteLine(value.X); }
}
}
public struct Point { public int X; }
Fuente oficial: Microsoft Aprender.