11 Replies - 2035 Views - Last Post: 08 September 2012 - 06:43 AM Rate Topic: -----

#1 ray1234   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 07-September 12

question on list<t>.ForEach method

Posted 07 September 2012 - 01:06 PM

// racers is  list<racer>  type

racers.ForEach(Console.Writeline);




from this code, how is console.writeline excepted as a parameter for the ForEach Method when the parameter type is Action<t> ?

also why was console.writeline in the above statement excepted without parenthesis? what does console.writeline mean without parenthesis?

thanks

ray
Is This A Good Question/Topic? 0
  • +

Replies To: question on list<t>.ForEach method

#2 darek9576   User is offline

  • D.I.C Lover

Reputation: 203
  • View blog
  • Posts: 1,731
  • Joined: 13-March 10

Re: question on list<t>.ForEach method

Posted 07 September 2012 - 01:14 PM

Are you trying to print each racer?
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2620
  • View blog
  • Posts: 4,175
  • Joined: 21-June 11

Re: question on list<t>.ForEach method

Posted 07 September 2012 - 01:14 PM

Action<T> is a delegate type. It represents void methods that take one argument of type T (or a supertype thereof). Console.WriteLine is a void method that takes an argument of type Object. Since Object is a supertype of Racer (and T in this case is Racer since, as you said, racers is a List<Racer>), Console.WriteLine can be used as an argument to ForEach.

Quote

also why was console.writeline in the above statement excepted without parenthesis?


Because we're passing the method WriteLine as an argument to ForEach - not the result of calling the method.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6534
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: question on list<t>.ForEach method

Posted 07 September 2012 - 01:14 PM

.

This post has been edited by tlhIn`toq: 07 September 2012 - 01:32 PM
Reason for edit:: Comment withdrawn

Was This Post Helpful? 0
  • +
  • -

#5 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2620
  • View blog
  • Posts: 4,175
  • Joined: 21-June 11

Re: question on list<t>.ForEach method

Posted 07 September 2012 - 01:27 PM

View PosttlhIn`toq, on 07 September 2012 - 10:14 PM, said:

That line of code is pretty much gibberish.


No, it's not.

Quote

foreach creates a loop.


Yes, that's true (though entirely irrelevant). However List<T>.ForEach is a method of the List<T> class, which takes an Action<T> as its argument.

Quote

And you can't send a command such as Console.WriteLine as a parameter.


Of course you can pass methods as arguments. That's why delegate types exist.
Was This Post Helpful? 1
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • Posts: 21,457
  • Joined: 05-May 12

Re: question on list<t>.ForEach method

Posted 07 September 2012 - 01:31 PM

The List<T>.ForEach() above is from the LINQ extension methods. It is different from foreach keyword.

Yes, you can just give it a function name. For example:
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
    class Dallasite
    {
        public string Name { get; set; }

        public override string ToString()
        {
            return Name;
        }
    }

    class Program
    {
        static void DoDebbie(Dallasite dallasite)
        {
            Console.WriteLine("Debbie does {0}.", dallasite.Name);
        }

        static void Main(string[] args)
        {
            List<Dallasite> dallasites = new List<Dallasite>();
            dallasites.Add(new Dallasite() { Name = "Rick" });
            dallasites.Add(new Dallasite() { Name = "Mr. Greenfield" });
            dallasites.Add(new Dallasite() { Name = "Mr. Hardwick" });

            Console.WriteLine("Dallasites:");
            dallasites.ForEach(Console.WriteLine);

            Console.WriteLine("Debbie does Dallas:");
            dallasites.ForEach(DoDebbie);
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#7 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2620
  • View blog
  • Posts: 4,175
  • Joined: 21-June 11

Re: question on list<t>.ForEach method

Posted 07 September 2012 - 01:34 PM

View PostSkydiver, on 07 September 2012 - 10:31 PM, said:

The List<T>.ForEach() above is from the LINQ extension methods. It is different from foreach keyword.


Actually it's defined directly on the List<T> class (and only on the List<T> class - it's not defined for other Enumerables) - not as an extension method. (Yes, I'm in smart ass mode today.)

This post has been edited by sepp2k: 07 September 2012 - 01:35 PM

Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • Posts: 21,457
  • Joined: 05-May 12

Re: question on list<t>.ForEach method

Posted 07 September 2012 - 01:41 PM

LOL! We need more smartasses. It keeps everyone on their toes. I usually play the smartass Saturday to Tuesday. :)
Was This Post Helpful? 0
  • +
  • -

#9 ray1234   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 07-September 12

Re: question on list<t>.ForEach method

Posted 07 September 2012 - 02:47 PM

View Postsepp2k, on 07 September 2012 - 01:14 PM, said:

Action<T> is a delegate type. It represents void methods that take one argument of type T (or a supertype thereof). Console.WriteLine is a void method that takes an argument of type Object. Since Object is a supertype of Racer (and T in this case is Racer since, as you said, racers is a List<Racer>), Console.WriteLine can be used as an argument to ForEach.

Quote

also why was console.writeline in the above statement excepted without parenthesis?


Because we're passing the method WriteLine as an argument to ForEach - not the result of calling the method.


thanks, query solved :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5103
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: question on list<t>.ForEach method

Posted 07 September 2012 - 03:18 PM

I've wondered why the ForEach method was only defined on List. I don't know any good reason it's not defined for IEnumerables. It's a pretty easy extension method to add though.

class Program
{
    static void Main(string[] args)
    {
        Enumerable.Range(1, 10).ForEach(Console.WriteLine);
        Console.ReadKey();
    }
}

public static class Extensions
{
    public static void ForEach<T>(this IEnumerable<T> collection, Action<T> predicate)
    {
        foreach (var i in collection)
        {
            predicate(i);
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#11 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: question on list<t>.ForEach method

Posted 07 September 2012 - 11:32 PM

In line 14 of your code why did you use 'var' rather than 'T'?
Was This Post Helpful? 0
  • +
  • -

#12 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5103
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: question on list<t>.ForEach method

Posted 08 September 2012 - 06:43 AM

Force of habit. The style that I and my coworkers follow is to use var whenever possible. Traditionalists don't like it, and I didn't like var either when I first started using 3.5. But to be honest, I've found using it like that to be really convenient, and we've really never had an issue with readability.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1