Thursday, February 13, 2025

.NET or not .NET - ForEach

 .NET or not .NET - ForEach

Summary: Naive foreach loop has better performance than .NET foreach loop


.NET foreach loop: 
# Define an ArrayList in .NET
$dotnetfruits = [System.Collections.ArrayList]@("Apple", "Banana", "Cherry", "Date")
# Use foreach to iterate through each number in the ArrayList $dotnetfruits.foreach( { Write-Output $_ })

Naive foreach loop: 

# Define an array of strings

$fruits = @("Apple", "Banana", "Cherry", "Date")

# Using foreach to loop through each item in the array

$fruits.foreach({

    Write-Output $_

})

No comments:

Post a Comment

PowerShell: filter output with value

 #require PowerShell core  Install-PSResource FineFormat Install-Module -Name FineFormat -scope CurrentUser #The output show a lot of empty ...