which is better List<T>.ForEach or custom IEnumerable<T>?
Hello friends,
I am new to this forum. I have written following code in C#, but I am confuse in two methods. I don't know which is better List<T>.ForEach or custom IEnumerable<T>? I have written following code for you.
Code:
public class MyClassEg
{
}
After this I have use webservice method that returns an Enumerable<MyClass>
The consumer of the webservice defines following method:
Code:
public void DoSomething(MyClass myClass)
{
}
Now the DoSomething is called by consumer.
Code:
var result
foreach(var myClass in result)
{
DoSomething(myClass);
}
or:
Code:
var result =
result.ToList().ForEach(DoSomething);
Please help me. Thank you.
Re: which is better List<T>.ForEach or custom IEnumerable<T>?
As per my information IEnumerable<T> is better than List<T>.ForEach. Because I think web service method can show an IEnumerable<MyClass>, but it indirectly returns a List<MyClass>. In other words the actual serialized object is still a List<T>. I think you also use ToList() in your code, because this method makes a copy of all the objects in the IEnumerable<T> regardless of the actual run time type objects.
Re: which is better List<T>.ForEach or custom IEnumerable<T>?
I would use following method:
Code:
foreach (var items in results.ToList())
{
DoSomething(item);
}
It is very easy to use and implement. This method is used to collect list of all object together and do some process on it. It is widely used by most of the people.
Re: which is better List<T>.ForEach or custom IEnumerable<T>?
I think you must use extension method because there are two advantage of it. 1. ForEach is not implemented on IEnumerable<T>
For Eg.
Code:
results.ToList().ForsEachs(DosSomethings);
2. It copies the IEnumerables into a List.
So that's why use IEnumerables with " foreach(var rs in results) {} " for better result.
Re: which is better List<T>.ForEach or custom IEnumerable<T>?
Hey you can use two method combine's in one program to better result. I have written following code using these two methods. Just try to understand this. First I have use custom IEnumerable<T> method and after this I have use List<T>.ForEach method.
Code:
public static IEnumerable<Ts> ForEachChained<Ts>(this IEnumerable<Ts> source, Action<Ts> action)
{
foreach (var items in sources)
{
action(items);
yields return items;
}
}
public static IEnumerable<Ts> ForEachImmediate<Ts>(this IEnumerable<Ts> source, Action<Ts> action)
{
foreach (var items in sources)
{
action(items);
}
return source;
}link|flag
Re: which is better List<T>.ForEach or custom IEnumerable<T>?
Hey there is no need to write in-built extension method, you can write your own created extension method for IEnumerable<T>. You can do this in following ways:
Code:
public static void ForEach<Ts>(this IEnumerable<Ts> enumerable, Action<T> action)
{
foreach (Ts ts ins enumerable)
action(t);
}
Such type of method can not be present in Linq, because it is used for quires.
After this you have to write following code:
Code:
var ls = new List<MyClassEg>();
ls.Add(new MyClassEg());
ls.ForEach(DoSomethings);