[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2431
  • Last Modified:

List<MyClass>.Reverse() ---> foreach statement cannot operate on variables of type 'void' because 'void' does not contain a public definition for 'GetEnumerator'

Hi There,

.NET 2.0+

List<MyClass> selectedList = new List<MyClass>();

foreach (MyClass item in selectedList)
{
    // This compiles.
}

foreach (MyClass item in selectedList.Reverse())
{
    // Error: foreach statement cannot operate on variables of type 'void' because 'void' does not contain a public definition for 'GetEnumerator'
}

How do I fix this from a layman point of view?

thanks
0
Valimai
Asked:
Valimai
  • 2
1 Solution
 
JimBrandleyCommented:
List.Reverse() is a method invocation that has a return value of void; i.e. It operates on the list, but doesn't return a new List. If you chage it to:
selectedList.Reverse();
foreach (MyClass item in selectedList)
{
     // do stuff here
}

It will compile and run.

Jim
0
 
ValimaiAuthor Commented:
Hi Jim, thanks a lot.
0
 
JimBrandleyCommented:
My pleasure. Good luck.

Jim
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now