Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace Array.ConvertAll in NetCore 1.0

Tags:

c#

.net-core

My current code is using Array.ConvertAll, which I need to migrate to net core 1.0. How to migrate it to work in Net core.

Can we use foreach statement with custom conversion code to handle the conversion? But I don't know how to do it.

Any help is appreciated.

like image 570
S Tinu Avatar asked Sep 30 '16 22:09

S Tinu


2 Answers

Instead of

int[] array1 = ...
string[] array2 = Array.ConvertAll(array1, element => element.ToString());

You could use Linq:

int[] array1 = ...
string[] array2 = array1.Select(element => element.ToString()).ToArray();
like image 59
Julian Avatar answered Oct 16 '22 02:10

Julian


Only if you upgrade to latest .NET Core 1.0 official release (forget about all previous testing bits), you can use this method in System.Runtime package,

https://learn.microsoft.com/en-us/dotnet/core/api/system.array#System_Array_ConvertAll__2___0___System_Converter___0___1__

like image 20
Lex Li Avatar answered Oct 16 '22 01:10

Lex Li