FR CSharp Keyword extern - LIHACHTETAN/ClassicUO-Bad-Newbie-CSharp-Scripts-IDE GitHub Wiki
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
C#: extern
Déclare un membre implémenté en dehors du code C# ; typique pour l'interopérabilité native via DllImport. extern alias spécifie un alias d’assembly externe.
Exemple simple (Example.cs)
Enregistrez cet exemple séparément dans Example.cs. Exécutez-le indépendamment des autres exemples de cette page.
Example.cs:
// PROJECT-EXAMPLE
class NativeMethods
{
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
public static extern uint GetCurrentProcessId();
}
Déclaration uniquement : ce code se compile comme bibliothèque ; appelez-le depuis Main pour l’exécuter.
Syntaxe
[DllImport(...)] public static extern ReturnType Method(...);
Ces deux exemples natifs sont pour Windows. Le nom de la bibliothèque, ABI et la disponibilité des fonctionnalités dépendent de la plate-forme ; la compilation elle-même ne vérifie pas leur présence.
Exemple 1 : formulaire de base
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(); }
Exemple 2 : une autre forme pratique
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(); }
Source officielle : Microsoft Apprendre.