Introduction
These days, I prefer to split arrays with autosize using the latest methodology, and always being as elegant as possible.
Using the Code
My ideas about changing code (different from the original) are:
- Avoid unnecessary
if
statements in for
statements. - Give the possibility to extract the splitted arrays to work with.
- Avoid
null
values.
static void Main(string[] args)
{
String[] arrayString = new string[] { "1", "2", "3", "4", "5","6","7" };
List<string[]> splitted = new List<string[]>();
Action<string[],> Split = (bulkarray, size) =>
{
int i = -1;
int div = bulkarray.Length / size + (bulkarray.Length % size > 0 ? 1 : 0);
while (++i < div)
splitted.Add(bulkarray.Skip(size * i).Take(size).ToArray());
};
Split(arrayString, 3);
}
I hope you find it interesting, because at least if you are learning C#, in this example you can learn several things.
I'm Electronic Engineer, I did my end degree project at Astrophysical Institute and Tech Institute. I'm HP Procurve AIS and ASE ,Microsoft 3.5 MCTS
I live in Canary Islands ,developing customized solutions
Deeply involved in Xamarin Forms LOB (including Azure Cloud with offline support, custom controls, dependencies) projects, WP8.1 & W10 projects, WPF modern styled projects. Portable libraries like portablePDF, portableOneDrive, portableReports and portablePrinting (using Google Printing API).
Web and apps showcase at:
Expediteapps
Take a look to my blog
Blog