Click here to Skip to main content
15,890,882 members
Articles / Programming Languages / C#
Tip/Trick

How to Split an Array into Multiple Arrays

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
31 May 2012CPOL 15.1K   2  
This is an alternative for "How to split an array into multiple arrays"

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:

  1. Avoid unnecessary if statements in for statements.
  2. Give the possibility to extract the splitted arrays to work with.
  3. Avoid null values.
C#
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Expediteapps
Spain Spain
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

Comments and Discussions

 
-- There are no messages in this forum --