DE CSharp Keyword extern - LIHACHTETAN/ClassicUO-Bad-Newbie-CSharp-Scripts-IDE GitHub Wiki
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
C#: extern
Handbuch C# · Praktischer Leitfaden
Deklariert ein Mitglied, das außerhalb des C#-Codes implementiert ist; typisch für natives Interop über DllImport. Externer Alias gibt einen externen Assembly-Alias an.
Einfaches Beispiel (Example.cs)
Speichern Sie dieses Beispiel separat als Example.cs. Führen Sie es unabhängig von den anderen Beispielen dieser Seite aus.
Example.cs:
// PROJECT-EXAMPLE
class NativeMethods
{
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
public static extern uint GetCurrentProcessId();
}
Nur eine Deklaration: Der Code wird als Bibliothek kompiliert; zum Ausführen ist ein Aufruf aus Main erforderlich.
Syntax
[DllImport(...)] public static extern ReturnType Method(...);
Diese beiden nativen Beispiele gelten für Windows. Bibliotheksname, ABI und Funktionsverfügbarkeit sind plattformabhängig; Die Kompilierung selbst prüft nicht, ob sie vorhanden sind.
Beispiel 1: Grundform
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(); }
Beispiel 2: eine weitere praktische Form
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(); }
Offizielle Quelle: Microsoft Learn.