Results 1 to 6 of 6

Thread: which is better List<T>.ForEach or custom IEnumerable<T>?

  1. #1
    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.

  2. #2
    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.

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    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.

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    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.

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    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

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    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);

Similar Threads

  1. Replies: 3
    Last Post: 04-12-2010, 12:16 AM
  2. Foreach statement of C sharp
    By Ram Bharose in forum Software Development
    Replies: 5
    Last Post: 19-01-2010, 08:42 AM
  3. What is Custom List
    By Daniel23 in forum Windows Software
    Replies: 3
    Last Post: 20-11-2009, 03:57 AM
  4. Javascript ForEach loop equivalent
    By Ikshud in forum Software Development
    Replies: 2
    Last Post: 04-08-2009, 07:36 PM
  5. Problem with a foreach loop in PHP
    By JiJi in forum Software Development
    Replies: 2
    Last Post: 20-11-2008, 05:57 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,248,601.48210 seconds with 16 queries