Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 19-02-2010
Member
 
Join Date: Nov 2009
Posts: 36
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.
Reply With Quote
  #2  
Old 19-02-2010
Member
 
Join Date: Nov 2005
Posts: 1,323
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.
Reply With Quote
  #3  
Old 19-02-2010
Member
 
Join Date: Oct 2005
Posts: 2,389
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.
Reply With Quote
  #4  
Old 19-02-2010
Member
 
Join Date: May 2008
Posts: 2,383
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.
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #5  
Old 19-02-2010
Member
 
Join Date: Feb 2008
Posts: 1,848
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
Reply With Quote
  #6  
Old 19-02-2010
Member
 
Join Date: Jan 2008
Posts: 1,515
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);
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "which is better List<T>.ForEach or custom IEnumerable<T>?"
Thread Thread Starter Forum Replies Last Post
Is there any way to show list of global custom keyboard shortcuts in Windows 7 Maggie Q Operating Systems 3 04-12-2010 12:16 AM
Foreach statement of C sharp Ram Bharose Software Development 5 19-01-2010 08:42 AM
What is Custom List Daniel23 Windows Software 3 20-11-2009 03:57 AM
Javascript ForEach loop equivalent Ikshud Software Development 2 04-08-2009 07:36 PM
Problem with a foreach loop in PHP JiJi Software Development 2 20-11-2008 05:57 PM


All times are GMT +5.5. The time now is 06:55 AM.