Repository navigation
JA CSharp Keyword extern
Codex edited this page Oct 11, 2026
·
2 revisions
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
C# コードの外で実装されたメンバーを宣言します。 DllImport を介したネイティブ相互運用では一般的です。 extern alias は、外部アセンブリのエイリアスを指定します。
この例を 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(...);これら 2 つのネイティブの例は Windows 用です。ライブラリ名、ABI、および機能の利用可能性はプラットフォームに依存します。コンパイル自体はそれらの存在をチェックしません。
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(); }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 Learn。
ClassicUO — Bad Newbie & C# Scripts IDE