Skip to content

Latest commit

 

History

History
37 lines (26 loc) · 2.03 KB

unmanaged-types.md

File metadata and controls

37 lines (26 loc) · 2.03 KB
description title ms.date helpviewer_keywords
Learn about unmanaged types in C#
Unmanaged types
09/06/2019
unmanaged type [C#]

Unmanaged types (C# reference)

A type is an unmanaged type if it's any of the following types:

  • sbyte, byte, short, ushort, int, uint, long, ulong, nint, nuint, char, float, double, decimal, or bool
  • Any enum type
  • Any pointer type
  • A tuple whose members are all of an unmanaged type
  • Any user-defined struct type that contains fields of unmanaged types only.

You can use the unmanaged constraint to specify that a type parameter is a non-pointer, non-nullable unmanaged type.

A constructed struct type that contains fields of unmanaged types only is also unmanaged, as the following example shows:

[!code-csharpunmanaged constructed types]

A generic struct may be the source of both unmanaged and managed constructed types. The preceding example defines a generic struct Coords<T> and presents the examples of unmanaged constructed types. The example of a managed type is Coords<object>. It's managed because it has the fields of the object type, which is managed. If you want all constructed types to be unmanaged types, use the unmanaged constraint in the definition of a generic struct:

[!code-csharpunmanaged constraint in type definition]

C# language specification

For more information, see the Pointer types section of the C# language specification.

See also