Come si converte da una variabile IEnumerable in una [[] in variabile in C#?Converti IEnumerable <int> in int []
12
A
risposta
18
utilizzare il metodo .ToArray()
estensione se siete in grado di utilizzare System.Linq
Se siete in Net 2 allora si potrebbe appena strappare quanto System.Linq.Enumerable implementa il. ToArray
metodo di estensione (ho sollevato il codice qui quasi parola per parola - ne ha bisogno un Microsoft®?):
struct Buffer<TElement>
{
internal TElement[] items;
internal int count;
internal Buffer(IEnumerable<TElement> source)
{
TElement[] array = null;
int num = 0;
ICollection<TElement> collection = source as ICollection<TElement>;
if (collection != null)
{
num = collection.Count;
if (num > 0)
{
array = new TElement[num];
collection.CopyTo(array, 0);
}
}
else
{
foreach (TElement current in source)
{
if (array == null)
{
array = new TElement[4];
}
else
{
if (array.Length == num)
{
TElement[] array2 = new TElement[checked(num * 2)];
Array.Copy(array, 0, array2, 0, num);
array = array2;
}
}
array[num] = current;
num++;
}
}
this.items = array;
this.count = num;
}
public TElement[] ToArray()
{
if (this.count == 0)
{
return new TElement[0];
}
if (this.items.Length == this.count)
{
return this.items;
}
TElement[] array = new TElement[this.count];
Array.Copy(this.items, 0, array, 0, this.count);
return array;
}
}
Con questo si può semplicemente fare questo:
public int[] ToArray(IEnumerable<int> myEnumerable)
{
return new Buffer<int>(myEnumerable).ToArray();
}
3
IEnumerable<int> i = new List<int>{1,2,3};
var arr = i.ToArray();
14
chiamata ToArray
dopo una direttiva using per LINQ:
using System.Linq;
...
IEnumerable<int> enumerable = ...;
int[] array = enumerable.ToArray();
Richiede .NET 3.5 o versione successiva. Fateci sapere se siete su .NET 2.0.
1
IEnumerable to int[] - enumerable.Cast<int>().ToArray();
IEnumerable<int> to int[] - enumerable.ToArray();
1
IEnumerable<int> ints = new List<int>();
int[] arrayInts = ints.ToArray();
A condizione che si sta utilizzando LINQ :)
Problemi correlati
- 1. Elenco <int> a IEnumerable <IComparable>
- 2. Muliplying tutti i valori in IEnumerable <int>
- 3. Verificare se IEnumerable <object> è IEnumerable <int>
- 4. Converti IEnumerable <Task<T>> to IObservable <T>
- 5. Converti intervallo <Int> nell'intervallo <String.Index>
- 6. Flatten IEnumerable <IEnumerable <>>; comprensione generici
- 7. Converti int in CGFloat
- 8. Converti int in stringa?
- 9. Converti int [] in raddoppiare [] utilizzando Cast <T>?
- 10. Inizializza IEnumerable <int> come parametro opzionale
- 11. IEnumerable <IEnumerable <T>> per IEnumerable <T> utilizzando LINQ
- 12. Conversione da IEnumerable <IEnumerable <string>> per ICollection <T>
- 13. IEnumerable <T> in C#
- 14. MySQL: Converti INT in DATETIME
- 15. Converti int in double Python
- 16. Converti parole (stringa) in Int
- 17. Converti int in const wchar_t *
- 18. Converti stringa esadecimale in int
- 19. Haskell Converti intero in Int?
- 20. Lista Converti <int> alla lista stringa delimitata
- 21. Converti [IO Int] in IO [Int] in Haskell?
- 22. IEnumerable <> to IList <>
- 23. Perché IEnumerable <T> è definito come IEnumerable <out T>, non IEnumerable <T>
- 24. Converti sezione stringa in int in Rust
- 25. Converti numero in vista testo in int
- 26. Converti Int in UInt32 in Swift
- 27. Converti byte in Int/uint in C
- 28. Converti tipo a tipo IEnumerable interfaccia
- 29. inizializzazione IEnumerable <string> In C#
- 30. esportazione IEnumerable <T> in Excel
utilizzare il .ToArray() metodo di estensione. –
Per i downvoter: per te potrebbe essere ovvio, ma merita un DV? Le risposte alla maggior parte delle domande sono ovvie per qualcuno. – spender
@spender - Come nel gameshow del Regno Unito Chi vuol essere milionario? È semplice se conosci la risposta! Non è una cattiva domanda - è perfettamente ragionevole e 5 risposte dicono che è responsabile. Potrebbe però qualificarsi per un dupe. –