Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

extern: practical reference

extern: practical reference: extern declares an implementation outside C# code or an alias for an external reference.

Version: C# language; verified against C# 14 reference

Syntax

extern /* grammar-specific form */

extern declares an implementation outside C# code or an alias for an external reference.

Use extern with DllImport when the runtime creates the P/Invoke stub; source-generated P/Invoke uses LibraryImport on a static partial method, not extern.

A wrong native signature can corrupt the process instead of merely throwing an exception.

The library or symbol may be absent on a platform, and marshaling needs explicit types.

The complete program actually uses extern; its exact expected stdout is True.

Examples

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;

Console.WriteLine(Native.GetProcessId() > 0);
static partial class Native { [DllImport("libc", EntryPoint = "getpid")] public static extern int GetProcessId(); }

Result

True

The complete program actually uses `extern`; its exact expected stdout is `True`.

Sources