dotnet / dotnet/csharplang

allow references to structs to be stored in fields of ref structs

Open
#1,147 69 comments 157 reactions 1 assignee Claimed by @jaredpar View on GitHub
Bottom Up Work Feature Request Implemented Needs ECMA Spec Proposal Proposal champion
Dominant language
C#
Stars
12.7k
Forks
1.1k
Avg merge
11h 1m
Merged PRs (30d)
3

Description

Unity is a game engine that uses C# extensively.

We're in need of a way to have a struct that contains one or more pointers to other structs that we want to be able to read/write from/to. We do this with unsafe code today:
```C#
struct Color { float r,g,b; }

unsafe struct Group
{
Color* _color;
ref Color color => ref *_color;
}
```
But we would love to do this without unsafe code, and would like to suggest a feature where it's allowed to store references to structs in fields of ref structs, that can guarantee that these pointers don't escape onto the heap:
```C#
struct Color { float r,g,b; }

ref struct Group
{
ref Color color;
}
```

**EDIT** link to spec for this feature https://github.com/dotnet/csharplang/blob/main/proposals/low-level-struct-improvements.md

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.