views:

55

answers:

4

I originally had this code which I mistakenly thought would do what I wanted it to:

string firstArg = args[0];
string[] otherArgs = args.Except(new string[] { args[0] }).ToArray();

However, it seems that the .Except method removes duplicates. So if I was to pass through the arguments a b c c, the result of otherArgs would be b c not b c c.

So how can I get a new array with all the elements from the second element onwards?

+5  A: 

Use the Skip method:

var otherArgs = args.Skip(1).ToArray();
SLaks
Thanks for that - first thing I stumbled across after posting this question!
Damovisa
+2  A: 

Using linq as it appears you are and from the top of my head:

string[] otherArgs = args.skip(1).ToArray();
Michael Gattuso
+2  A: 

If you don't have a desination array in mind:

string[] otherArgs = args.Skip(1).ToArray();

If you do:

Array.Copy(args, 1, otherArgs, 0, args.Length - 1);
Jason
+1  A: 

You could also use the ConstrainedCopy method. Here's some example code:

static void Main(string[] args)
{
    string firstArg = args[0];
    Array otherArgs = new string[args.Length - 1];
    Array.ConstrainedCopy(args, 1, otherArgs, 0, args.Length - 1);

    foreach (string foo in otherArgs)
    {
        Console.WriteLine(foo);
    }
}

}

nithins