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 abcc
, the result of otherArgs
would be bc
not bcc
.
So how can I get a new array with all the elements from the second element onwards?
使用Skip
方法:
var otherArgs = args.Skip(1).ToArray();
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);
使用linq就像你看到的那样,从头到尾:
string[] otherArgs = args.skip(1).ToArray();
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);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.