Reputation: 19423
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?
Upvotes: 3
Views: 1284
Reputation: 3192
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);
}
}
}
Upvotes: 2
Reputation: 241769
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);
Upvotes: 3
Reputation: 13210
Using linq as it appears you are and from the top of my head:
string[] otherArgs = args.skip(1).ToArray();
Upvotes: 2