Compiler Error CS0446
Collapse the table of content
Expand the table of content

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.

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() { }

Community Additions

© 2015 Microsoft