Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# string array getting only the first 10 values

Tags:

arrays

string

c#

I have a string array that has a list of values like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

I am trying only to get the first ten so my output looks like this and store it another string array.

1
2
3
4
5
6
7
8
9
10

it seems really easy i just can't figure it out

like image 700
user990951 Avatar asked Oct 22 '11 08:10

user990951


3 Answers

for (int i=0; i<Math.Min(10, array.Length); i++)
    Console.WriteLine(array[i]);

OR

foreach (int i in array.Take(10))        
    Console.WriteLine(array[i]);

EDIT: Based on your comment that you want it in a string array. Here is what you have to do

string[] numbers = array.Take(10).Select(i=>i.ToString()).ToArray();
like image 106
Muhammad Hasan Khan Avatar answered Sep 30 '22 05:09

Muhammad Hasan Khan


You can use Linq. You need to include the reference and the using directive:

using System.Linq;

theStringsArray.Take(10).ToArray();
like image 28
lontivero Avatar answered Sep 30 '22 05:09

lontivero


You can use

Array.Copy(SourceArray, DestinationArray, 10);

like image 24
Nasreddine Avatar answered Sep 30 '22 05:09

Nasreddine