Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检查“是否”;for loop";是在C中的第一次还是最后一次迭代中#_C#_For Loop - Fatal编程技术网

C# 检查“是否”;for loop";是在C中的第一次还是最后一次迭代中#

C# 检查“是否”;for loop";是在C中的第一次还是最后一次迭代中#,c#,for-loop,C#,For Loop,我的列表中有一个for循环,我想在第一次迭代和最后一次迭代中做一些不同的事情。我发现这个问题是关于每个循环的foreach。 我如何在for循环中实现目标 string str; for (int i = 0; i < myList.Count; i++) { //Do somthin with the first iteration str = "/" + i; //Do somthin with the last iteration } string-s

我的列表中有一个
for循环
,我想在第一次迭代和最后一次迭代中做一些不同的事情。我发现这个问题是关于每个循环的
foreach
。 我如何在
for循环
中实现目标

string str;
for (int i = 0; i < myList.Count; i++)
{
     //Do somthin with the first iteration
     str = "/" + i;
     //Do somthin with the last iteration
}
string-str;
for(int i=0;i
我想知道是否还有其他办法:

for (int i = 0; i < myList.Count; i++)
{
    if (i == 0)
    {
        //Do somthin with the first iteration
    }
    str = "/" + i;
    if (i == myList.Count-1)
    {
        //Do somthin with the last iteration
    }
}
for(int i=0;i
如果您希望完全避免for循环中的条件(根据您提供的详细信息,这似乎是这样的),那么您应该在第一个和最后一个项目上执行您喜欢的任何逻辑。然后,可以构造for循环,使其忽略可枚举中的第一个和最后一个元素(将
i
初始化为1,并将条件更改为
i

if(myList!=null&&myList.Count>=2)
{
YourFirstFunction(myList[0]);
for(int i=1;i
YourNFunction
替换为您希望分别应用于第一个索引、索引间索引和最后一个索引的任何逻辑


请注意,我已经检查了myList是否有两个或更多项——我认为这种逻辑没有任何意义,除非,至少,第一个索引和最后一个索引不相同。考虑到您还计划对介于两者之间的项执行某些操作,您可能希望将其更改为3,以确保始终有一个明确的开始、中间和结束。

如果您希望完全避免for循环中的条件(根据您提供的详细信息,这看起来是这样的),您应该在第一项和最后一项上执行您喜欢的任何逻辑。然后,可以构造for循环,使其忽略可枚举中的第一个和最后一个元素(将
i
初始化为1,并将条件更改为
i

if(myList!=null&&myList.Count>=2)
{
YourFirstFunction(myList[0]);
for(int i=1;i
YourNFunction
替换为您希望分别应用于第一个索引、索引间索引和最后一个索引的任何逻辑


请注意,我已经检查了myList是否有两个或更多项——我认为这种逻辑没有任何意义,除非,至少,第一个索引和最后一个索引不相同。考虑到您还计划对介于两者之间的项目执行某些操作,您可能希望将其更改为3,以确保始终有一个明确的开始、中间和结束。

只需对第一个和最后一个项目执行某些操作,然后循环其他项目:

if (myList != null && myList.Any())
{
    // Do something with the first item here  
    var str = "** START **" + myList.First();

    for (int i = 1; i < myList.Count - 1; i++)
    {
        str += "/" + i;
    }

    //Do something with the last item here 
    if (myList.Count > 1) str += myList.Last() + " ** END **";
}
if(myList!=null&&myList.Any())
{
//对这里的第一项做点什么
var str=“**START**”+myList.First();
for(int i=1;i1)str+=myList.Last()+“**END**”;
}

只需对第一个和最后一个项目执行一些操作,然后循环执行其余项目:

if (myList != null && myList.Any())
{
    // Do something with the first item here  
    var str = "** START **" + myList.First();

    for (int i = 1; i < myList.Count - 1; i++)
    {
        str += "/" + i;
    }

    //Do something with the last item here 
    if (myList.Count > 1) str += myList.Last() + " ** END **";
}
if(myList!=null&&myList.Any())
{
//对这里的第一项做点什么
var str=“**START**”+myList.First();
for(int i=1;i1)str+=myList.Last()+“**END**”;
}

您可以从1开始循环,并在外部执行第一次迭代处理。大概是这样的:

if(myList != null && myList.Count > 0){
// Process first and last element here using myList[0] and myList[myList.Count -1]

}

for(int i = 1; i <myList.Count - 1;i++){
// process the rest
}
if(myList!=null&&myList.Count>0){
//在此处使用myList[0]和myList[myList.Count-1]处理第一个和最后一个元素
}

对于(int i=1;i,您可以从1开始循环,并在外部执行第一次迭代处理。类似如下:

if(myList != null && myList.Count > 0){
// Process first and last element here using myList[0] and myList[myList.Count -1]

}

for(int i = 1; i <myList.Count - 1;i++){
// process the rest
}
if(myList!=null&&myList.Count>0){
//在此处使用myList[0]和myList[myList.Count-1]处理第一个和最后一个元素
}

对于(int i=1;i i suupose您可以使用Enumerable.Range用于此目的。您当前的解决方案有什么问题?@TGH我想知道是否有实际的方法来检查迭代。i suupose您可以使用Enumerable.Range用于此目的。您当前的解决方案有什么问题?@TGH我想知道是否有实际的方法来检查迭代。我想知道e最后一项也要检查\n我希望最后一项也要检查\