Skip to content

RU CSharp Operator Pointer

Codex edited this page Oct 11, 2026 · 2 revisions

Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어

Операции с указателями

Справочник C# · Практическое руководство

& получает адрес, * разименовывает указатель, -> обращается к члену pointer-цели. Требует unsafe и соблюдение времени жизни памяти.

Простой пример (Example.cs)

Сохраните этот пример отдельно как Example.cs. Запускайте его независимо от остальных примеров страницы.

Example.cs:

// PROJECT-EXAMPLE
unsafe
{
    int value = 3;
    int* pointer = &value;
    *pointer = 7;
    System.Console.WriteLine(value);
}

В проекте включите <AllowUnsafeBlocks>true</AllowUnsafeBlocks>. Указатель действителен только в показанной области.

Результат:

7

Синтаксис

Type* pointer = &value; / *pointer / pointer->Member

Приклад 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); }
    }
}

Приклад 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; }

Официальный источник: Microsoft Learn.

Clone this wiki locally