none
Am I missing something? RRS feed

  • Question

  • Hi,


    I'm currently developing a control toolkit in VS2017 using the .NET 4.7 SDK. However I get the following message:-


    'IEnumerable<string>' does not contain a definition for 'ForEach' and no extension method 'ForEach' accepting a first argument of type 'IEnumerable<string>' could be found (are you missing a using directive or an assembly reference?)


    Am I missing a reference or something?


    Peter

    Friday, March 2, 2018 7:05 PM

Answers

  • ForEach is in System.Collections.Generic, do you have this as a using statement?

    using System.Collections.Generic;

    Assuming you are doing this or similar operation

    var test = new List<string>();
    test.ForEach(x => Console.WriteLine(x));


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by PeterWagner Saturday, March 3, 2018 7:03 AM
    Friday, March 2, 2018 7:16 PM
    Moderator

All replies

  • ForEach is in System.Collections.Generic, do you have this as a using statement?

    using System.Collections.Generic;

    Assuming you are doing this or similar operation

    var test = new List<string>();
    test.ForEach(x => Console.WriteLine(x));


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by PeterWagner Saturday, March 3, 2018 7:03 AM
    Friday, March 2, 2018 7:16 PM
    Moderator
  • There is no ForEach method on IEnumerable<T> or Enumerable (the extension class). If you want foreach then use the standard C# foreach statement. List<T> does have a Foreach method but it doesn't do anything that the C# foreach statement doesn't already.

    Some folks define a Foreach method in their private code for IEnumerable<T> but I wouldn't recommend it. The foreach statement is sufficient.

    If you're just now getting this error then you likely changed a type from List<string> to IEnumerable<string>. Simply replace the Foreach method call with a foreach statement and you should be fine.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, March 2, 2018 10:13 PM
    Moderator
  • Thanks, the original code that I'm working on didn't make this clear enough.
    Saturday, March 3, 2018 7:06 AM
  • I think I've  got it now thanks. The original code is using it on an argument passed into a method. Presumably I can fix it by adding:-

    files = new List<string>();

     
    Saturday, March 3, 2018 7:12 AM