Autor Beitrag
Ivy
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 228



BeitragVerfasst: Do 19.07.12 13:11 
Hallo,
ich möchte ein ganz einfaches String arrray in ein Integer Array konvertieren. Wie kann ich das implementieren?
ist es möglich es ohne eine for schleife umzusetzten. habe bisher nur beispiele mit schleife gefunden
IVY
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Do 19.07.12 13:40 
ausblenden C#-Quelltext
1:
2:
string[] strings = { "1""2""3" };
int[] ints = Array.ConvertAll(strings, x => Convert.ToInt32(x));


Edit - natürlich steckt aber in ConvertAll auch eine Schleife. Ohne ein 'mach etwas für jedes Element', also eine Schleife, geht es nicht.
Ivy Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 228



BeitragVerfasst: Do 19.07.12 13:44 
super vielen dank, das klappt :-)
Palladin007
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1282
Erhaltene Danke: 182

Windows 11 x64 Pro
C# (Visual Studio Preview)
BeitragVerfasst: Do 19.07.12 16:28 
Schau noch mal hier vorbei:

msdn.microsoft.com/d...ibrary/bb397687.aspx

Da wird das ganze erklärt und ich finde es sehr praktisch, das zu können, denn damit kann man so viele kleine sinnlose Methoden raus werfen und auch viele so nützliche Methoden im Framework überhaupt erst sinnvoll verwenden.
Yogu
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2598
Erhaltene Danke: 156

Ubuntu 13.04, Win 7
C# (VS 2013)
BeitragVerfasst: Do 19.07.12 21:17 
Wenn Performance nicht das oberste Kriterium ist, wäre folgende Version möglicherweise eleganter:

ausblenden C#-Quelltext
1:
int[] integers = strings.Select(x => int.Parse(x)).ToArray();					

Dann sieht man nämlich auf den ersten Blick, dass dort etwas geparst wird und möglicherweise eine FormatException geworfen wird.

Edit: Fehler korrigiert