Compiler Error CS0446
Foreach cannot operate on a 'Method or Delegate'. Did you intend to invoke the 'Method or Delegate'?
This error is caused by specifying a method without parentheses or an anonymous method without parentheses in the part of the foreach
statement where you would normally put a collection class. Note that it is valid, though unusual, to put a method call in that location, if the method returns a collection class.
Example
The following code will generate CS0446.
// CS0446.cs
using System;
class Tester
{
static void Main()
{
int[] intArray = new int[5];
foreach (int i in M) { } // CS0446
}
static void M() { }
}
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for