This foreach is intended to look through a list and when a match is found it returns the next element. Can it be improved?
public static string GetCommandLineArg(string arg)
{
var doreturn = false;
foreach (var item in Environment.GetCommandLineArgs())
if (doreturn)
return item;
else if (arg == item)
doreturn = true;
return null;
}
Run Code Online (Sandbox Code Playgroud)
The part I am concerned with is the returning of the next item.
内部if语句杂乱无章,似乎不需要使用额外的变量。是否有一个简单的函数调用或属性可以与a foreach一起使用以返回下一个项目?
fub*_*ubo 10
与Linq接触
public static string GetCommandLineArg(string arg)
{
return Environment.GetCommandLineArgs().SkipWhile(x => x != arg).Skip(1).FirstOrDefault();
}
Run Code Online (Sandbox Code Playgroud)
https://dotnetfiddle.net/zsIhtv