Skip to content

KO CSharp Keyword extern

Codex edited this page Oct 11, 2026 · 2 revisions

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

C#: extern

핸드북 C# · 실용 가이드

C# 코드 외부에서 구현된 멤버를 선언합니다. DllImport을 통한 기본 상호 운용성의 경우 일반적입니다. 외부 별칭은 외부 어셈블리 별칭을 지정합니다.

간단한 예제 (Example.cs)

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

Example.cs:

// PROJECT-EXAMPLE
class NativeMethods
{
    [System.Runtime.InteropServices.DllImport("kernel32.dll")]
    public static extern uint GetCurrentProcessId();
}

선언만 포함합니다. 이 코드는 라이브러리로 컴파일되며 실행하려면 Main에서 호출해야 합니다.

통사론

[DllImport(...)] public static extern ReturnType Method(...);

이 두 가지 기본 예제는 Windows용입니다. 라이브러리 이름, ABI 및 기능 가용성은 플랫폼에 따라 다릅니다. 컴파일 자체에서는 해당 항목의 존재를 확인하지 않습니다.

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

공식 출처: Microsoft 학습.

Clone this wiki locally