如何在foreach循环中验证元素是否位于集合中的最后一个

本文关键字:于集合 集合 最后一个 是否 foreach 循环 元素 验证 | 更新日期: 2023-09-27 18:10:14

我有一个foreach循环,我需要验证元素是否在我迭代的集合中的最后一个。我试过的:

foreach (var character in list) // list is of type `string` 
{
    if (character == list.Last())
    {
    }
}

但是在这种情况下,如果我有"la la la", if语句将在第二个字符上执行。

问题:如何编写if语句,使其在访问序列的最后一个元素时执行?

如何在foreach循环中验证元素是否位于集合中的最后一个

我建议使用索引而不是对象引用进行迭代,即

for (int i = 0; i <= list.Count-1; i++)
{
    if (i == list.Count-1)
    {
        // so something special with last item
    }
}
foreach (var character in list) // list is of type `string` 
{
    if (character == list[list.Count - 1])
    {
    }
}

这是一个DEMO

作为替代方案,由于List实现了IEnumerable接口,您可以使用Enumerable.Last方法

返回序列的最后一个元素。

foreach (var character in list) // list is of type `string` 
{
    if (character == list.Last())
    {
    }
}

这是DEMO

由于列表实际上是一个字符串,因此需要将其转换为列表。

var elementList = list.Split(" ");

你可以找到最后一个元素。

var lastElement = elementList.LastOrDefault();

请检查使用IsNullOrEmpty来处理空列表的情况

如果您只想对最后一个字符执行操作,那么Mare Infinitus的代码应该可以完成此操作。

:

var elementList = list.Split(" ");
if (elementList.Last().Equals(character))
{
   // do something here
}

这个就可以了,不需要foreach。

但是,如果您想循环并对最后一个字符执行特定的操作,那么您可以使用标准的for循环。詹姆斯回答:
for (int i = 0; i <= list.Count-1; i++)
{
    if (i == list.Count-1)
    {
        // so something special with last item
    }
}

如果你发现自己经常做这种事情,你可以使用一个扩展方法,它可以让你询问序列中的一个元素是否是最后一项。

这篇文章最初是由Jon Skeet写的;他称之为"Smart Enumerable",我相信它是MoreLinq Linq扩展(也是由Jon Skeet)的一部分。

如果你使用这样的东西,你的代码看起来像这样:

foreach (var character in list.AsSmartEnumerable()) 
    if (character.IsLast)
        // Do something with character.Value

这是Jon Skeet实现的稍微修改的副本:

/// <summary>
/// Static class to make creation easier. If possible though, use the extension
/// method in SmartEnumerableExt.
/// </summary>
public static class SmartEnumerable
{
    /// <summary> method to make life easier.</summary>
    /// <typeparam name="T">Type of enumerable</typeparam>
    /// <param name="source">Source enumerable</param>
    /// <returns>A new SmartEnumerable of the appropriate type</returns>
    public static SmartEnumerable<T> Create<T>(IEnumerable<T> source)
    {
        return new SmartEnumerable<T>(source);
    }
}
/// <summary>Wrapper methods for SmartEnumerable[T].</summary>
public static class SmartEnumerableExt
{
    /// <summary>Extension method to make life easier.</summary>
    /// <typeparam name="T">Type of enumerable</typeparam>
    /// <param name="source">Source enumerable</param>
    /// <returns>A new SmartEnumerable of the appropriate type</returns>
    public static SmartEnumerable<T> AsSmartEnumerable<T>(this IEnumerable<T> source)
    {
        return new SmartEnumerable<T>(source);
    }
}
/// <summary>
/// Type chaining an IEnumerable&lt;T&gt; to allow the iterating code
/// to detect the first and last entries simply.
/// </summary>
/// <typeparam name="T">Type to iterate over</typeparam>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix", Justification="This is too general to end in 'Collection'")]
public class SmartEnumerable<T>: IEnumerable<SmartEnumerable<T>.Entry>
{
    /// <summary>Enumerable to which we proxy</summary>
    readonly IEnumerable<T> _enumerable;
    /// <summary>Constructor.</summary>
    /// <param name="enumerable">Collection to enumerate. Must not be null.</param>
    public SmartEnumerable(IEnumerable<T> enumerable)
    {
        if (enumerable==null)
        {
            throw new ArgumentNullException("enumerable");
        }
        this._enumerable = enumerable;
    }
    /// <summary>
    /// Returns an enumeration of Entry objects, each of which knows
    /// whether it is the first/last of the enumeration, as well as the
    /// current value.
    /// </summary>
    public IEnumerator<Entry> GetEnumerator()
    {
        using (IEnumerator<T> enumerator = _enumerable.GetEnumerator())
        {
            if (!enumerator.MoveNext())
            {
                yield break;
            }
            bool isFirst = true;
            bool isLast  = false;
            int  index   = 0;
            while (!isLast)
            {
                T current = enumerator.Current;
                isLast = !enumerator.MoveNext();
                yield return new Entry(isFirst, isLast, current, index++);
                isFirst = false;
            }
        }
    }
    /// <summary>Non-generic form of GetEnumerator.</summary>
    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
    /// <summary>
    /// Represents each entry returned within a collection,
    /// containing the value and whether it is the first and/or
    /// the last entry in the collection's. enumeration
    /// </summary>
    public class Entry
    {
        internal Entry(bool isFirst, bool isLast, T value, int index)
        {
            this._isFirst = isFirst;
            this._isLast  = isLast;
            this._value   = value;
            this._index   = index;
        }
        /// <summary>The value of the entry.</summary>
        public T Value
        {
            get
            {
                return _value;
            }
        }
        /// <summary>Whether or not this entry is first in the collection's enumeration.</summary>
        public bool IsFirst
        {
            get
            {
                return _isFirst;
            }
        }
        /// <summary>Whether or not this entry is last in the collection's enumeration.</summary>
        public bool IsLast
        {
            get
            {
                return _isLast;
            }
        }
        /// <summary>The 0-based index of this entry (i.e. how many entries have been returned before this one)</summary>
        public int Index
        {
            get
            {
                return _index;
            }
        }
        readonly bool _isFirst;
        readonly bool _isLast;
        readonly T    _value;
        readonly int  _index;
    }
}

如果只有IEnumerable,则必须手动触发枚举器。下面是一个示例扩展方法,可能会对您有所帮助:

public static class IEnumerableExtensions
{
    public static void Action<T>(this IEnumerable<T> source, Action<T> sequenceElement, Action<T> lastElement)
    {
        if (source == null)
            throw new ArgumentNullException("source");
        if (sequenceElement == null)
            throw new ArgumentNullException("sequenceElement");
        if (lastElement == null)
            throw new ArgumentNullException("lastElement");
        T element = default(T);
        using (var enumerator = source.GetEnumerator())
        {
            if (enumerator.MoveNext())
                element = enumerator.Current;
            while (enumerator.MoveNext())
            {
                sequenceElement(element);
                element = enumerator.Current;
            }
            lastElement(element);
        }
    }
}

你可以这样调用它:

var myElements = Enumerable.Range(1, 10);
myElements.Action(value => Console.WriteLine("Something out of the sequence: " + value),
                    value => Console.WriteLine("The last element: " + value));