Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

141
Visualizações
How would you convert List<T> to Vector<T> C#

I'm using the vectors C# library using System.Numerics; I need to convert List to a Vector

I've tried implicitly and explicitly converting the type but it does not have inbuilt compatibility. I have read the documentation looking for a method that can do do this conversion, but didn't find any suitable.

I know I could achieve this conversion using a simple for loop but doing this conversion many times within my program seems terribly inefficient and I'm wondering if anyone knows of a better way to get the same result.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Vector<T> has a constructor which takes a Span<T>, and CollectionsMarshal.AsSpan lets you get a Span<T> from a List<T>.

E.g.:

var list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8 };
var vector = new Vector<int>(CollectionsMarshal.AsSpan(list));

See on SharpLab.

This results in a single memory copy from the contents of the array backing the List<T>, to the memory backing the Vector<T>.

Heed the warnings in the docs for CollectionsMarshal.AsSpan: it returns a reference to the array which is currently backing the List<T>, but that will change if the list is updated. For just taking a snapshot for copying into a Vector<T>, however, it's fine.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda