dotnet / dotnet/docs

Blittable type check doesn't work for bool

Open
#39,654 1 comment 4 reactions 0 assignees View on GitHub
:watch: Not Triaged dotnet-fundamentals/svc
Dominant language
No language data
Stars
4.8k
Forks
6.1k
Avg merge
15h 21m
Merged PRs (30d)
370

Description

### Type of issue

Code doesn't work

### Description

The [Blittable types when runtime marshalling is enabled](https://learn.microsoft.com/en-us/dotnet/standard/native-interop/best-practices#blittable-types-when-runtime-marshalling-is-enabled) section of the documentation contains the following instructions for how to check whether a type is blittable or not:

> You can see if a type is blittable or contains blittable contents by attempting to create a pinned GCHandle. If the type isn't a string or considered blittable, GCHandle.Alloc will throw an ArgumentException.

The following code throws `System.ArgumentException: Object contains references` for me as expected:

```csharp
using System.Runtime.InteropServices;
GCHandle.Alloc(new List(), GCHandleType.Pinned).Free();
```

However the following code does not throw an exception for me, even though `bool` is supposed to be non-blittable:

```csharp
using System.Runtime.InteropServices;
GCHandle.Alloc(new bool(), GCHandleType.Pinned).Free();
```

1. If there is a problem with my code, then please add some sample code to the documentation to show to correct way to create a pinned GCHandle.
2. If `bool` is actually blittable in this context, then please add details to the documentation explaining under when `bool` is blittable and when it is not.
3. If this is a bug in the .NET SDK (I am using 8.0.101) then please let me know whether this issue suffices as a bug report, or whether I should report it somewhere else as well.

### Page URL

https://learn.microsoft.com/en-us/dotnet/standard/native-interop/best-practices#blittable-types-when-runtime-marshalling-is-enabled

### Content source URL

https://github.com/dotnet/docs/blob/main/docs/standard/native-interop/best-practices.md

### Document Version Independent Id

e58f3d16-8083-0efe-0057-e21dbebb262f

### Article author

@jkoritzinsky

### Metadata

* ID: 6ea501d3-d963-c5dc-342d-ee82c92d1850
* Service: **dotnet-fundamentals**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.