zana zana - 2 months ago 24
C# Question

how to convert double array into integer in C#?

I am using C# and I have a List like this

List<double[]>
, I want convert into integer and store all elements in another list like this
List<int[]>
.

How can I do this?

mok mok
Answer

There is a heap of ways to do this, for example you can use linq like this:

List<int> integers = new List<int>();
List<double> doubles = new List<double>();
for (int i = 0; i < 10; i++)
    doubles.Add(i + new Random().NextDouble());
foreach (var d in doubles)
{
    Console.WriteLine(d);
}
Console.WriteLine("------------------------");
integers = doubles.Select(d => (int) d).ToList(); // EVERYTHING IS DONE HERE
foreach (var i in integers)
{
    Console.WriteLine(i);
}

Or you can simply loop through the list or use an iterator and cast them explicitly.

Comments