我有一个foreach循环,我需要验证该元素是否是我正在迭代的集合中的最后一个。我尝试了什么:
foreach (var character in list) // list is of type `string`
{
if (character == list.Last())
{
}
}
但是在这种情况下,如果我有"la la la"
if
语句将在第二个字符上执行。
问题:如何编写if
语句,以便在访问序列的最后一个元素时执行它?
答案 0 :(得分:11)
我建议使用索引而不是对象引用进行迭代,即
for (int i = 0; i <= list.Count-1; i++)
{
if (i == list.Count-1)
{
// so something special with last item
}
}
答案 1 :(得分:3)
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
。
答案 2 :(得分:0)
由于您的列表实际上是一个字符串,您需要将其转换为列表。
var elementList = list.Split(" ");
然后,您可以找到最后一个元素。
var lastElement = elementList.LastOrDefault();
只需使用IsNullOrEmpty检查处理空列表的情况。
答案 3 :(得分:0)
如果您发现自己经常这样做,可以使用扩展方法,让您询问序列中的元素是否是最后一项。
这最初由Jon Skeet撰写; he called it "Smart Enumerable",我相信它是MoreLinq Linq Extensions (也是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<T> 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;
}
}
答案 4 :(得分:0)
如果您只想对最后一个字符执行操作,那么Mare Infinitus'代码应该可以解决问题。
怎么样:
var elementList = list.Split(" ");
if (elementList.Last().Equals(character))
{
// do something here
}
这应该做到,不需要foreach。
但是,如果要循环并对最后一个字符执行特定的操作,则可以使用标准for
循环。 James'回答:
for (int i = 0; i <= list.Count-1; i++)
{
if (i == list.Count-1)
{
// so something special with last item
}
}
答案 5 :(得分:0)
如果您只有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));