using System;
using System.Linq;
namespace StringArrayToIntegerArray
{
class Program
{
static void Main(string[] args)
{
string[] _StringUserId = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
//Previous approach.
int[] _IintUserId = new int[_StringUserId.Length];
int iterator = 0;
foreach (string item in _StringUserId)
{
int.TryParse(item, out _IintUserId[iterator]);
iterator++;
}
foreach (int item in _IintUserId)
{
Console.WriteLine(item);
}
Console.WriteLine(Environment.NewLine);
//New approach.
_IintUserId = new int[_StringUserId.Length];
_IintUserId = _StringUserId.Select(UserId => int.Parse(UserId)).ToArray();
foreach (int item in _IintUserId)
{
Console.WriteLine(item);
}
Console.Read();
}
}
}
No comments:
Post a Comment