与泛型列表的foreach,利用价值型检测时,第一次迭代 [英] foreach with generic List, detecting first iteration when using value type

查看:80
本文介绍了与泛型列表的foreach,利用价值型检测时,第一次迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的foreach 通过一个普通的名单我常常想做些什么为列表中的第一个元素不同ING:

 列表<对象> OBJ文件=新的List<对象> 
{
新的对象(),
新的对象(),
新的对象(),
新的对象()
};

的foreach(对象o在OBJ文件)
{
如果(O == objs.First())
{
System.Diagnostics.Debug。的WriteLine(第一个对象 - 做一些特别的东西);
}
,否则
{
System.Diagnostics.Debug.WriteLine(对象做别的事情);
}
}

这将输出:

 
第一个对象 - 做一些特别的
对象做别的
对象做别的
对象做别的

这是所有罚款和花花公子。



但是,如果我的泛型列表是值类型的,这种方法会失败。

 列表< INT>整数=新的List< INT> {0,0,0,0}; 
的foreach(INT我整型)
{
如果(我== ints.First())
{
System.Diagnostics.Debug.WriteLine(第一INT - 做一些特别的东西);
}
,否则
{
System.Diagnostics.Debug.WriteLine(INT做别的事情);
}
}

这将输出:

 
首先INT - 做一些特别的
首先INT - 做一些特别的
首先INT - 做一些特别的
首先INT - 做点什么特殊的

现在我知道我可以重新编写这个添加布尔标志变量或传统循环,但我想知道是否有任何方式,以找出是否foreach循环是其循环的第一次迭代。


< DIV CLASS =h2_lin>解决方案

那么,你可以使用显式迭代编写的:

 使用(VAR ITER = ints.GetEnumerator()){
如果(iter.MoveNext()){
//做第一与iter.Current

,而(ITER .MoveNext()){
//做一些与数据的其余部分iter.Current
}
}
}

的布尔标志选项(使用的foreach )可能是比较容易,但...这就是我!(几乎)总是做



另一个选择是LINQ:

 如果(ints.Any()){
变种第一= ints.First();
//做第一
}

的foreach(VAR项ints.Skip(1)){东西
//与他们无关的其他东西
}

以上的缺点是,它试图看看列表3倍。 ..因为我们知道这是一个列表,这是好的 - 但如果我们拥有的是一个的IEnumerable< T> ,它只会是明智的,一旦迭代它(因为源可能不被重新可读)。


When foreaching through a generic list I often want to do something different for the first element in the list:

List<object> objs = new List<object>
{
    new Object(),
    new Object(),
    new Object(),
    new Object()
};

foreach (object o in objs)
{
    if (o == objs.First())
    {
        System.Diagnostics.Debug.WriteLine("First object - do something special");
    }
    else
    {
        System.Diagnostics.Debug.WriteLine("object Do something else");
    }
}

This will output:

    First object - do something special
    object Do something else
    object Do something else
    object Do something else

This is all fine and dandy.

However if my generic list is of a value type, this approach will fail.

List<int> ints = new List<int> { 0, 0, 0, 0 };
foreach (int i in ints)
{
    if (i == ints.First())
    {
        System.Diagnostics.Debug.WriteLine("First int - do something special");
    }
    else
    {
        System.Diagnostics.Debug.WriteLine("int Do something else");
    }
}

This will output:

    First int - do something special
    First int - do something special
    First int - do something special
    First int - do something special

Now I know I could recode this to add a boolean flag variable or traditional for loop, but I am wondering if there's any way to find out if a foreach loop is on the first iteration of its looping.

解决方案

Well, you could code it using explicit iteration:

using(var iter = ints.GetEnumerator()) {
  if(iter.MoveNext()) {
     // do "first" with iter.Current

     while(iter.MoveNext()) {
       // do something with the rest of the data with iter.Current
     }
  }
}

The bool flag option (with foreach) is probably easier though... that is what I (almost) always do!

Another option would be LINQ:

if(ints.Any()) {
  var first = ints.First();
  // do something with first
}

foreach(var item in ints.Skip(1)) {
  // do something with the rest of them
}

The downside of the above is that it tries to look at the list 3 times... since we know it is a list, that is fine - but if all we had was an IEnumerable<T>, it would only be sensible to iterate it once (since the source might not be re-readable).

这篇关于与泛型列表的foreach,利用价值型检测时,第一次迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆