CLSS.ExtensionMethods.IEnumerable.ForEach 1.1.1

dotnet add package CLSS.ExtensionMethods.IEnumerable.ForEach --version 1.1.1
                    
NuGet\Install-Package CLSS.ExtensionMethods.IEnumerable.ForEach -Version 1.1.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CLSS.ExtensionMethods.IEnumerable.ForEach" Version="1.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CLSS.ExtensionMethods.IEnumerable.ForEach" Version="1.1.1" />
                    
Directory.Packages.props
<PackageReference Include="CLSS.ExtensionMethods.IEnumerable.ForEach" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CLSS.ExtensionMethods.IEnumerable.ForEach --version 1.1.1
                    
#r "nuget: CLSS.ExtensionMethods.IEnumerable.ForEach, 1.1.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CLSS.ExtensionMethods.IEnumerable.ForEach@1.1.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CLSS.ExtensionMethods.IEnumerable.ForEach&version=1.1.1
                    
Install as a Cake Addin
#tool nuget:?package=CLSS.ExtensionMethods.IEnumerable.ForEach&version=1.1.1
                    
Install as a Cake Tool

CLSS.ExtensionMethods.IEnumerable.ForEach

Problem

ForEach is a convenient method for List<T>. It provides a syntax similar to JavaScript's forEach array method. However, by default, this method:

  • Is not available to other IEnumerable<T> collections, even though by nature they can be iterated through a foreach loop.
  • Returns void and therefore not very friendly to a functional-style call chain.
  • Does not accept impure methods.
  • Does not support passing through each element's index and the original collection like JavaScript's forEach array method.

Solution

This package provides ForEach for all IEnumerable<T> collections to address all of the above issues.

Using ForEach with HashSet:

using CLSS;

var uniqueIDs = new HashSet<int>() { 2, 4, 15, 22 };
uniqueIDs.ForEach(id => Console.WriteLine($"id: {id}"));

ForEach returns the source collection to continue to be used in a functional-style call chain:

using CLSS;
using Newtonsoft.Json.Linq;
using System.Linq;

var itemNodes = JArray.Parse(rawJSON).Properties()
  .Select(p => p.Value)
  .Cast<JObject>()
  .ForEach(itemNode => UpdateItemLabel(itemNode))
  .ToList();

Using ForEach with impure methods:

using CLSS;
using UnityEngine;

// In Unity's Scripting API, AssetBundle.LoadAssetAsync returns an
// AssetBundleRequest.
assetNames.ForEach(AssetBundle.LoadAssetAsync);

Using ForEach with JavaScript-style optional arguments:

using CLSS;

(new float[] { 9.5f, 23.6f, 5.0f, 14.1f })
  .ForEach<float[], float>((n, i, srcNumbers) =>
  {
    float sumWithNeighbours = n;
    if (i > 0) sumWithNeighbours += srcNumbers[i - 1];
    if (i < srcNumbers.Length - 1) sumWithNeighbours += srcNumbers[i + 1];
    Console.WriteLine(sumWithNeighbours);
  });

The exact return type of ForEach will be determined by the invocation syntax. Below is how return type rules apply:

using CLSS;

var numbers = new float[] { 0.0f, 1.0f, 2.0f };

// Implicit invocation and implicit lambda expression return IEnumerable<float>
numbers.ForEach(n => { }); // returns IEnumerable<float>
// Explicit invocation and implicit lambda expression return original type
numbers.ForEach<float[], float>(n => { }); // returns float[];
// Implicit invocation and explicit lambda expression return original type
numbers.ForEach((float n) => { }); // returns float[];
// Implicit invocation and delegate instance return original type
Action<float> ProcessNumber = n => { };
numbers.ForEach(ProcessNumber}); // returns float[];
// Implicit invocation and method reference return IEnumerable<float>
static void ProcessNumber(float n) { }
numbers.ForEach(ProcessNumber}); // returns IEnumerable<float>;
// Explicit invocation and method reference return original type
static void ProcessNumber(float n) { }
numbers.ForEach<float[], float>(ProcessNumber}); // returns float[];

Note that using lambdas in lieu of writing plain foreach loops will incur additional memory allocations. ForEach extension methods - just as with List<T>.ForEach - are meant to be syntactic sugar only. Only use it in performance-critical code paths if you can afford the added overhead.

This package is a part of the C# Language Syntactic Sugar suite.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp1.0 was computed.  netcoreapp1.1 was computed.  netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.0 is compatible.  netstandard1.1 was computed.  netstandard1.2 was computed.  netstandard1.3 was computed.  netstandard1.4 was computed.  netstandard1.5 was computed.  netstandard1.6 was computed.  netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net45 was computed.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen30 was computed.  tizen40 was computed.  tizen60 was computed. 
Universal Windows Platform uap was computed.  uap10.0 was computed. 
Windows Phone wp8 was computed.  wp81 was computed.  wpa81 was computed. 
Windows Store netcore was computed.  netcore45 was computed.  netcore451 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.1 2,305 11/16/2022
1.1.0 2,327 8/4/2022
1.0.0 2,285 8/1/2022

- Added language-sensitive syntax highlighting in README file.