Skip to content

KO CSharp Keyword safe

Codex edited this page Oct 11, 2026 · 2 revisions

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

C#: safe

핸드북 C# · 실용 가이드

업데이트된 메모리 안전 모델의 미리보기 수정자: 작성자는 호출자의 안전하지 않은 컨텍스트를 요구하지 않는 것으로 선언을 명시적으로 표시합니다.

간단한 예제 (Example.cs)

이 예제를 Example.cs로 따로 저장하고 이 페이지의 다른 예제와 독립적으로 실행하세요.

Example.cs:

// PROJECT-EXAMPLE
class ManagedValue { public int Count { get; set; } }

이 짧은 예제는 C# 14를 사용합니다. 여기에서 지원되지 않는 미리 보기 구문 safe를 구현하지 않습니다.

통사론

public static safe extern int Method();

C# 15 / .NET 11의 미리보기입니다. 공식 규칙에서는 LangVersion=미리보기를 요구하고 해당 애플리케이션에 대해 업데이트된 메모리 안전 규칙을 별도로 활성화해야 합니다. 현재 C# 14에서는 이러한 선언을 지원하지 않습니다.

상태: 미리보기; 현재 구성 C# 14 / .NET 10는 이러한 예를 지원하지 않습니다.

예시 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)
    {
        Console.WriteLine(Native.GetCurrentProcessId());
    }
}
public static class Native { [DllImport("kernel32.dll")] public static safe extern uint GetCurrentProcessId(); }

예 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)
    {
        Union value = new() { Integer = 1065353216 };
        Console.WriteLine(value.Real);
    }
}
[StructLayout(LayoutKind.Explicit)] public struct Union { [FieldOffset(0)] public safe int Integer; [FieldOffset(0)] public safe float Real; }

공식 출처: Microsoft 학습.

Clone this wiki locally