I have been working with C# for 10 years and last 4 years I started to use JS in the front-end into deep.
I want to compare some methods related to arrays and collection in JS and C# (using LINQ). I want to show you how to choose the right method depending on your requirements in JS or C#.
forEach (js) vs ForEach (C# not LINQ):
This method is used to execute a function for each element in the array or collection. You can use it to update each element depending on conditions or for getting specific values.
Both methods are not pure function which means the original collection is affected or updated. This method is not included in LINQ (it’s on C# collections directly) but it’s important to mention it.
//JS demo
const array1 = [1, 2, 3, 4, 5];
array1.forEach(element => console.log(element));
//C# demo
var listOfNumbers = new int[] {0,1,2,3,4,5};
listOfNumbers.ToList().ForEach(p => Console.WriteLine(p));
filter (js) vs Where (LINQ):
This method is used to make a filter by a function depending on a condition.
Both methods are pure functions and return new collections including the record that matches the condition.
//JS demo
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
//C# demo
var listOfNumbers = new int[] {0,1,2,3,4,5};
var evenNumbers = listOfNumbers.ToList().Where(p => p%2 == 0);
reduce (js) vs Aggregate (LINQ):
This method executes a function for each element to return only one value.
Both methods are pure functions and return a new single value with no affectations in the original collection.
//JS demo
const array1 = [1, 2, 3, 4, 5];
const reducer = array1.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
//C# demo
var listOfNumbers = new int[] {1,2,3,4,5};
var sumTotal = listOfNumbers.Aggregate(0,(total, currentItem)=> total+ currentItem);
sort (js) vs OrderBy (LINQ):
This method sorts the elements in the collection depending on a function or a parameter.
in JS this method is not a pure function and this method updates the original collection. However, in C# this method is a pure function returning a new collection and can sort easily depending on the property selected. Also, you can use the method ‘Then’ to sort the new collection by other properties.
//JS demo
const months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
//C# demo
var listOfNumbers = new int[] {1,2,3,4,5};
var numberSorted = listOfNumbers.OrderBy(p=> p);
//var listOfUsers = users.OrderBy(p=> p.Name).Then(p=> p.LastName);
concact (js) vs Concact (LINQ):
This method is used to merge into collections into one array.
Both methods are pure functions and return a new collection including the records from the original collection.
//JS demo
const array1 = [1,2,3,4,5];
const array2 = [6,7,8,9,10];
const array3 = array1.concat(array2);
//C# demo
var listOfNumbers = new int[] {1,2,3,4,5};
var listOfNumbers2 = new int[] {6,7,8,9,10};
var allNumbers = listOfNumbers.Concat(listOfNumbers2);
push (js) vs Append (LINQ):
This method is used to add a new element to add the end of the list. Both methods update the original collection.
//JS demo
var array1 = [1,2,3,4,5];
array1.push(6);
//C# demo
var listOfNumbers = new int[] {1,2,3,4,5};
listOfNumbers.Append(6);
find (js) vs FirstOrDefault (LINQ):
This returns the first item which satisfies the criteria specified. These methods don’t affect or update the original collection (pure functions).
//JS demo
var array1 = [1,2,3,4,5];
var item1 = array1.find(p=> p===1);
//C# demo
var listOfNumbers = new int[] {1,2,3,4,5};
var item1 = listOfNumbers.FirstOrDefault(p=> p==1);
includes (js) vs Any (LINQ):
This method returns a boolean whether an array contains an element in JS. In LINQ, the method ‘Any’ returns a boolean whether at least an element satisfies the condition specified. These methods don’t affect or update the original
//JS demo
var array1 = [1,2,3,4,5];
var result = array1.includes(1);
//C# demo
var listOfNumbers = new int[] {1,2,3,4,5};
var result = listOfNumbers.Any(p=> p==1);
reverse (js) vs Reverse (LINQ):
This method in JS reverses an array by modifying the original array and also returning a new one reversed. In LINQ this method is a pure function and returns a new array reversed with no affections on the original collection.
//JS demo
var array1 = [1,2,3,4,5];
var newArray = array1.reverse(); //array1 is reversed
//C# demo
var listOfNumbers = new int[] {1,2,3,4,5};
var newListOfNumbers = listOfNumbers.Reverse();
Another interesting one to compare with is the function* against IEnumerable. Both are generators and use the yield keyword.