本文整理汇总了C#中System.Collections.Generic.ForEach方法的典型用法代码示例。如果您正苦于以下问题:C# System.Collections.Generic.ForEach方法的具体用法?C# System.Collections.Generic.ForEach怎么用?C# System.Collections.Generic.ForEach使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.Generic
的用法示例。
在下文中一共展示了System.Collections.Generic.ForEach方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ForEachArray
public void ForEachArray()
{
var arr = new[] { 1, 2, 3, 4 };
int count = 0;
arr.ForEach(item => count += item);
Assert.AreEqual(10, count);
}
示例2: ForEach_WithCollectionOf10_Iterates10Times
public void ForEach_WithCollectionOf10_Iterates10Times()
{
var totalIterations = 0;
var listOfNumbers = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
listOfNumbers.ForEach(number => totalIterations++);
Assert.That(totalIterations, Is.EqualTo(10));
}
示例3: ForEach
public void ForEach()
{
int sum = 0;
IEnumerable<int> numbers = new [] {1, 2, 3, 5, 8};
numbers.ForEach(n => sum += n);
Assert.That(sum, Is.EqualTo(19));
}
示例4: ForEach_WithCollectionOf1_Iterates1Time
public void ForEach_WithCollectionOf1_Iterates1Time()
{
var totalIterations = 0;
var listOfNumbers = new[] { 1 };
listOfNumbers.ForEach(number => totalIterations++);
Assert.That(totalIterations, Is.EqualTo(1));
}
示例5: ForEach_should_enumerate_the_collection
public void ForEach_should_enumerate_the_collection()
{
int sum = 0;
var values = new[] { 1, 1, 1 };
values.ForEach(x => sum += x);
sum.Should().Be(3);
}
示例6: ForEach_with_function_executes_function_over_every_element
public void ForEach_with_function_executes_function_over_every_element()
{
var executeWith = new List<int>();
var elements = new[] {1, 2, 3, 4};
elements.ForEach(executeWith.Add);
Assert.That(executeWith, Is.EquivalentTo(elements));
}
示例7: WhenForEachExecuted_ThenCallsActionForEach
public void WhenForEachExecuted_ThenCallsActionForEach()
{
var strings = new[] { "foo", "bar" };
var called = new List<string>();
strings.ForEach(s => called.Add(s));
Assert.Equal(strings, called);
}
示例8: ForEach_WithCollection_IteratesEachItem
public void ForEach_WithCollection_IteratesEachItem()
{
// arrange
IEnumerable<int> numbers = new[]{5, 4, 3};
string concatenated = string.Empty;
// act
numbers.ForEach(number => concatenated += number);
// assert
Assert.AreEqual("543", concatenated);
}
示例9: ForEach_WithIEnumerable_PerformsAction
public void ForEach_WithIEnumerable_PerformsAction()
{
// Arrange
var values = new[] { "William", "Sarah", "Abigail", "Jessica" };
//Act
var result = string.Empty;
values.ForEach(x => result += x);
// Assert
Assert.That(result, Is.EqualTo(string.Join(string.Empty, values)));
}
示例10: Should_CallActionForEachItem_WhenForEachIsInvoked
public void Should_CallActionForEachItem_WhenForEachIsInvoked()
{
// arrange
var items = new[] {1, 2, 3, 4, 5};
var visitedItems = new List<int>();
// act
items.ForEach(visitedItems.Add);
// assert
visitedItems.Should().BeEquivalentTo(items);
}
示例11: SaveOrLoad
public void SaveOrLoad(Serializer serializer)
{
if (serializer.IsLoading)
{
Images.Clear();
}
var objectEntries = new[]
{
LoadAndSaveEntry.Create(reader => reader.ReadUInt32(), writer => writer.WriteUInt32(0), 8),
LoadAndSaveEntry.Create(reader => reader.ReadUInt32(), writer => writer.WriteUInt32(0), 8),
LoadAndSaveEntry.Create(reader =>
{
Walk = new Point(reader.ReadInt16(), reader.ReadInt16());
}, writer =>
{
writer.WriteInt16(Walk.X);
writer.WriteInt16(Walk.Y);
}, 8),
LoadAndSaveEntry.Create(reader => Number = reader.ReadUInt16(), writer => writer.Write(Number), 8),
LoadAndSaveEntry.Create(reader =>
{
Position = new Point(reader.ReadInt16(), reader.ReadInt16());
}, writer =>
{
writer.WriteInt16(Position.X);
writer.WriteInt16(Position.Y);
}, 8),
LoadAndSaveEntry.Create(reader => Width = reader.ReadUInt16(), writer => writer.WriteUInt16(Width), 8),
LoadAndSaveEntry.Create(reader => Height = reader.ReadUInt16(), writer => writer.WriteUInt16(Height), 8),
LoadAndSaveEntry.Create(reader => ActorDir = reader.ReadByte(), writer => writer.WriteByte(ActorDir), 8),
LoadAndSaveEntry.Create(reader => ParentState = reader.ReadByte(), writer => writer.WriteByte(ParentState), 8),
LoadAndSaveEntry.Create(reader => Parent = reader.ReadByte(), writer => writer.WriteByte(Parent), 8),
LoadAndSaveEntry.Create(reader => State = reader.ReadByte(), writer => writer.WriteByte(State), 8),
LoadAndSaveEntry.Create(reader => reader.ReadByte(), writer => writer.WriteByte(0), 8),
LoadAndSaveEntry.Create(reader => Flags = (DrawBitmaps)reader.ReadByte(), writer => writer.WriteByte((byte)Flags), 46),
};
objectEntries.ForEach(e => e.Execute(serializer));
}