Performance IEnumerable<T>.ToList().ForEach() over loop [duplicate] - c#

This question already has answers here:
foreach vs someList.ForEach(){}
(13 answers)
Closed 9 years ago.
Is there any performance difference between these two statements?
IEnumerable<T>.ToList().ForEach(x => Console.WriteLine(x));
and
foreach (var obj in IEnumerable<T>)
Console.WriteLine(obj)

In the first example, you will
Create a list by enumerating the source
Enumerate through the new list and call Console.WriteLine for each element.
In the second example, you will
Enumerate through the source and call Console.WriteLine for each element.
There are two performance penalities to the first over the second:
The creation of the new List object
The double enumeration: over the original source, and then the list

Related

How to compare and remove items from lists in C# [duplicate]

This question already has answers here:
LINQ compare two lists and remove
(4 answers)
Closed 8 months ago.
I am new to c# development. I want to compare and remove list items that already exist in another list. Is there any built in method to do that instead of doing it using loop?
The List object has a RemoveAll method which can remove all elements of the list which match the predicate.
https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.removeall?view=net-6.0
You can also use the except method, answered here: Remove items from one list in another
var list1 = { /* first list */ };
var list2 = { /* second list to remove from first */ };
list1 = list1.Except(list2).ToList();

How to iterate over all elements in array(or list) with LINQ? [duplicate]

This question already has answers here:
Linq style "For Each" [duplicate]
(6 answers)
Closed 3 years ago.
I am new to LINQ and i want to switch onto it from traditional loops
I have an array of GameObjects and I just want to iterate over all of them and set one of their component enabled, and I wanna know if there is 1 line elegant way with LINQ to replace foreach or for loop's version
gameObjects.ToList().ForEach(x => {do Stuff with list});
Some reading on forEach in C# however. Could be interesting: https://blogs.msdn.microsoft.com/ericwhite/2009/04/08/why-i-dont-use-the-foreach-extension-method/

Linq - How can I run each item through a function and then pick the item with the highest function result [duplicate]

This question already has answers here:
How to perform .Max() on a property of all objects in a collection and return the object with maximum value [duplicate]
(9 answers)
Closed 5 years ago.
What I want to do is basically
var max = things.Select(t => ExpensiveFunc(t)).Max();
var ThingWithMaxResult = things.Where(t => ExpensiveFunc(t) == max).First();
But I don't want to have to run the ExpensiveFunc twice on each element.
I am learning LINQ so I would like to know the LINQ way of doing this. Otherwise I would normally create an array of things and results, then just pick the array with the highest result.
You can order by the function call (descending) and take the first one:
var max = things.OrderByDescending(ExpensiveFunc).First();

How to write Hashset values to the console? [duplicate]

This question already has answers here:
printing all contents of array in C#
(13 answers)
Closed 7 years ago.
How do I write the values of a hashset to the console? I do not want to use a foreach loop.
HashSet<string> hashSet=new HashSet<string>();
hashSet.Add("Employee");
hashSet.Add("Employee1");
Console.WriteLine(hashSet.Select(x=>x));
Output
System.Linq.Enumerable
Expected output
Employee,Employee1
You can use String.Join:
Console.WriteLine(String.Join(",", hashSet));
Try this:
hashSet.ToList<String>().ForEach(x => Console.WriteLine(x));
This will iterate over the hashSet and call Console.WriteLine on each item

Check a list has set of values [duplicate]

This question already has answers here:
Determine if a sequence contains all elements of another sequence using Linq [duplicate]
(4 answers)
Closed 8 years ago.
I have two sting lists
List<string> list1=new List(){"1","2","3"};
List<string> list2=new List(){"1","2"};
What will be the easiest way to check if list1 contains the values in list2.
How about
list1.Except(list2).Any();
Try using
[listName].Except(SecondListName).Any();
This should work.

Categories