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

142
Views
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 answers
Answer question

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 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!