JA CSharp Keyword extern - LIHACHTETAN/ClassicUO-Bad-Newbie-CSharp-Scripts-IDE GitHub Wiki
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
C#: extern
C# コードの外で実装されたメンバーを宣言します。 DllImport を介したネイティブ相互運用では一般的です。 extern alias は、外部アセンブリのエイリアスを指定します。
簡単な例 (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(...);
これら 2 つのネイティブの例は 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 Learn。