Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

unmanaged: practical reference

unmanaged: practical reference: The unmanaged constraint admits value types without managed references; the keyword also describes a function-pointer ABI.

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

Syntax

unmanaged /* grammar-specific form */

The unmanaged constraint admits value types without managed references; the keyword also describes a function-pointer ABI.

Use it in generic binary code and interop that require a reference-free representation.

unmanaged alone does not guarantee an identical cross-platform layout without StructLayout.

Nullable and reference-containing types fail the constraint, while size depends on architecture.

The complete program actually uses unmanaged; its exact expected stdout is 4.

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(Size<int>()); static int Size<T>() where T : unmanaged => Unsafe.SizeOf<T>();

Result

4

The complete program actually uses `unmanaged`; its exact expected stdout is `4`.

Sources