Remove an element from an array I have a method which works perfectly. It removes an element from an array. Now i would like to have a clean and simple code.

private void removeUserFromGroup(baseClass[] member)
{
    try
    {
        this.ExHandling = null;
        searchPathMultipleObject groupSearchPath = new searchPathMultipleObject();
        groupSearchPath.Value = "CAMID(\":" + this.DataViewModel.GroupModel.SelectedGroup + "\")";

        propEnum[] props = { propEnum.defaultName, propEnum.searchPath, propEnum.members };

        // get the current group membership.
        group cognosGroup = (group)this.LogonModel.CBICMS.query(groupSearchPath, props, new sort[] { }, new queryOptions())[0];
        if (cognosGroup.members.value.Length &gt; 0)
        {
            for (int y = 0; y &lt; cognosGroup.members.value.Length; y++)
            {
                //check if member[y] is the one that need to be deleted
                if (cognosGroup.members.value[y].searchPath.value == member[0].searchPath.value)
                {
                    int lenght = cognosGroup.members.value.Length - 1;

                    baseClass[] newMembers = new baseClass[lenght];
                    int index = 0;
                    baseClass obj = null;
                    // go trough group
                    for (int i = 0; i &lt;= lenght; i++)
                    {
                        if (i != y)
                        {
                            //create user
                            obj = cognosGroup.members.value[i];
                            newMembers[index] = obj;
                            index++;
                        }
                        else
                        {
                            this._messageText = "*Successfully removed " + this.RemoveUsername.ToLower() + " from " + this.DataViewModel.GroupModel.SelectedGroup;
                        }
                    }
                    cognosGroup.members = new baseClassArrayProp();
                    cognosGroup.members.value = newMembers;
                    this.LogonModel.CBICMS.update(new baseClass[] { cognosGroup }, new updateOptions());
                    y--;
                    Log4NetLogger.Logger.Info("Successfully removed: " + this.RemoveUsername.ToLower() + " from " + this.DataViewModel.GroupModel.SelectedGroup);
                    return;
                }
                else
                {
                    this._messageText = "*" + this.RemoveUsername.ToLower() + " is not a user from " + this.DataViewModel.GroupModel.SelectedGroup;
                }
            }
        }
        else
        {
            Log4NetLogger.Logger.Error(this.DataViewModel.GroupModel.SelectedGroup + " is empty");
            return;
        }
    }
    catch (Exception ex)
    {
        Log4NetLogger.Logger.Error("Error: ", ex);
    }
}


I know it doesn't look good and it isn't coded as if the person who ends up maintaining my code is a violent psychopath who knows where I live. So now I want to have a "clean" code. Can anyone help me and give me some tips?

Thanks!
You should be able to simplify that whole inner loop with a simple Where extension:

If you have the equality operator implemented:

baseClass obj = cognosGroup.members.value[y];
cognosGroup.members.value = cognosGroup.members.value.Where(x =&gt; x != obj).ToArray()


Or use the index option:

cognosGroup.members.value = cognosGroup.members.value.Where((x,i) =&gt; i != y).ToArray()


Another option is to store the data in a List&lt;baseClass&gt; instead and use the Remove or RemoveAt extension.