Fastest Conversion of Array of Guid to String and Back?
With .NET 4.0, I sometimes have a Guid[] or 200 items that all need conversion to string[], and sometimes I have to do the reverse.
What is the fastest/smartest way to do that?
Than开发者_开发问答ks.
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));
Runtime performance is probably the same as LINQ, although it is probably a tiny bit faster since it's going from and to array directly instead of an enumeration first.
Well, if you wanted to use LINQ, myList.Select(o => o.ToString())
will do it.
Otherwise, filling a List
with a foreach
loop is almost as succinct, and will work with older versions of .Net.
See if it helps:
Guid[] guids = new Guid[200];
var guidStrings = guids.Select(g => g.ToString());
var revertedGuids = guidStrings.Select(g => Guid.Parse(g));
精彩评论