Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

286
Views
¿Cómo debo crear un ImmutableArray a partir de un Span/ReadOnlySpan?

Supongamos que tengo un método como el siguiente:

 unsafe void Convert(byte* ptr, int length) { var span = new Span<byte>(ptr, length); var arr = ImmutableArray.CreateRange(span); // error: cannot convert from 'System.Span<byte>' to 'System.Collections.Generic.IEnumerable<byte>' Use(arr); }

El método CreateRange del ejemplo anterior es la CreateRange<T>(IEnumerable<T>) . Dado que Span no implementa ninguna interfaz, tiene sentido que no podamos realizar esta llamada. Pero en realidad estoy un poco perplejo en cuanto a qué hacer en su lugar. ¿Hay alguna forma idiomática de crear un ImmutableArray a partir de un Span con una copia/asignación mínima? (es decir, usar Span<T>.ToArray() y luego ImmutableArray<T>.ToImmutableArray() no sería ideal)

También agradecería cualquier sugerencia que implique usar el puntero + longitud directamente de alguna manera.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Hay un problema abierto (a partir de junio de 2021) sobre cómo agregar API para esto.

Hasta entonces, el problema sugiere usar un constructor como este:

 static ImmutableArray<T> ImmutableArrayFromSpan<T>(ReadOnlySpan<T> span) { var builder = ImmutableArray.CreateBuilder<T>(span.Length); foreach (var i in span) { builder.Add(i); } return builder.MoveToImmutable(); }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!