Quickly convert an array of prompts to a string and vice versa?

With .NET 4.0, I sometimes have a Guid [] or 200 pointer that needs to be converted to a string [], and sometimes I have to do the opposite.

What is the fastest / smartest way to do this?

Thanks.

+4
source share
3 answers

An alternative to LINQ is Array.ConvertAll() in this case:

 Guid[] guidArray = new Guid[100]; ... string[] stringArray = Array.ConvertAll(guidArray, x => x.ToString()); guidArray = Array.ConvertAll(stringArray, x => Guid.Parse(x)); 

The performance of Runtime is probably the same as LINQ, although it is probably a little faster because it goes from and directly to the array, and not to the enumeration.

+20
source

Well, if you want to use LINQ, myList.Select(o => o.ToString()) will do this.

Otherwise, filling the List loop with the foreach contour is almost as short and will work with older versions of .Net.

+2
source

See if this helps:

 Guid[] guids = new Guid[200]; var guidStrings = guids.Select(g => g.ToString()); var revertedGuids = guidStrings.Select(g => Guid.Parse(g)); 
+2
source

All Articles