Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there some way/alternative to use System.Collections.Generic.List<T> with ref return values?

Tags:

c#

.net

ref

Now that C# supports ref return values, is there some way (or alternative) to use List<T> where T is a value type and get ref from the indexer? From the documentation it seems that it returns simply T, which means that there is no way to update a structure in place without copying it out of the list, updating, and copying back in.

Is there any way to use List<T> while getting a ref indexer, or some alternative data structure that does the same? I'm aware that it is possible to do this with arrays, but I'm looking for a dynamic growable array.

like image 944
Jakub Arnold Avatar asked Aug 13 '18 22:08

Jakub Arnold


1 Answers

Looking at the examples given in this documentation and this blog post, arrays support ref return values via indexer, but List<T> doesn't (I have tried).

So I don't think there is anyway to use ref return on the list indexer until it is implement in the framework.

like image 131
Selman Genç Avatar answered Oct 04 '22 05:10

Selman Genç