Im having a strange problem here , Im reading settings from a .txt file using the code which works like a charm .

private string[] splitArray;
        private void Form1_Load(object sender, EventArgs e)
        {
            string[] lines = File.ReadAllLines("settings.txt");
            foreach (string line in lines)
            {
                if (line == "" || line.StartsWith(" ") || line.StartsWith("/"))
                {
                    continue;
                }
                splitArray = line.Split('=');
                MessageBox.Show(splitArray[0]);
            }
        }

Whats strange is the value of splitArray[0] keeps changing meaning the items are being inserted at the start of the array not the end .

Is there any to make the contents of line.Split('='); be added to the end of the array ?

I would use generics here but I gave you three ways to accomplish your task depending on what framework you're using:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using System.Linq;

namespace daniweb
{
  public partial class frmMainForm : DevExpress.XtraEditors.XtraForm
  {
    public frmMainForm()
    {
      InitializeComponent();
    }

    #region method 1 - use generics, probably the best way
    //Use generics, probably the best way
    private void simpleButton1_Click(object sender, EventArgs e)
    {
      const string fName = @"C:\file.txt";
      string[] lines = File.ReadAllLines(fName);
      List<string> fields = new List<string>();
      foreach (string s in lines)
      {
        fields.AddRange(s.Split(new char[] { '=' }));
      }
      //If you want to put them back in an array, or just use the generic list
      string[] outputArray = fields.ToArray();
      System.Diagnostics.Debugger.Break();
    }
    #endregion

    #region method 2 - use an array with linq to concat() them
    //Use an array with linq to combine arrays
    private void simpleButton2_Click(object sender, EventArgs e)
    {
      const string fName = @"C:\file.txt";
      string[] lines = File.ReadAllLines(fName);

      string[] fields = new string[0];
      foreach (string s in lines)
      {
        fields = fields.Concat(s.Split(new char[] { '=' })).ToArray();
      }

      System.Diagnostics.Debugger.Break();
    }
    #endregion

    #region method 3 - resize the array manually
    private void simpleButton3_Click(object sender, EventArgs e)
    {
      const string fName = @"C:\file.txt";
      string[] lines = File.ReadAllLines(fName);

      string[] fields = new string[0];
      foreach (string s in lines)
      {
        string[] lineFields = s.Split(new char[] { '=' });
        fields = (string[])ResizeArray(fields, fields.Length + lineFields.Length);
        lineFields.CopyTo(fields, fields.Length-lineFields.Length);
      }

      System.Diagnostics.Debugger.Break();
    }

    public static System.Array ResizeArray(System.Array oldArray, int newSize)
    {
      int oldSize = oldArray.Length;
      System.Type elementType = oldArray.GetType().GetElementType();
      System.Array newArray = System.Array.CreateInstance(elementType, newSize);
      int preserveLength = System.Math.Min(oldSize, newSize);
      if (preserveLength > 0)
        System.Array.Copy(oldArray, newArray, preserveLength);
      return newArray;
    }
    #endregion


  }
}

Oh, and as for your original code you were replacing the array entirely with each iteration instead of appending to it. My examples are three ways to append values to an array or list. I would avoid arrays wherever it is sensible to do so for performance reasons.

This article has been dead for over six months. Start a new discussion instead.