4

I have the class:

class Program
{
    static void Main(string[] args)
    {

    }

    public static int SetFlag_Old(this int i, int flag, bool set = true)
    {
        return (set) ? i | flag : ((i & flag) != 0) ? (i - flag) : i;

    }
}

And when I put this code into the main method above I do not get the option to call the extension method and I can't figure out why.

int i = 0;
i.

Even when I create a non-static method and insert that code I can't seem to call the extension methods. Am I missing something really simple?

swimfar
  • 147
  • 6
Exitos
  • 29,230
  • 38
  • 123
  • 178
  • 1
    That doesn't compile. Fixing the compilation error also fixes your problem – Rune FS Oct 18 '11 at 09:21
  • 3
    "error CS1106: Extension method must be defined in a non-generic static class" - it feels like the compiler is trying to tell me something... if only it would be more specific! – Marc Gravell Oct 18 '11 at 09:42
  • Hi @Marc, thanks for the sarcastic response. Actually I didnt realise it didnt build becasue I didnt get the usual errors tab. I opened visual studio (new machine) I didn't have the error list so I just saw the black on white writing and it looked 'OK'. I appreciate the responses though I was really rolling around laughing. – Exitos Oct 19 '11 at 10:19
  • @Pete2k it was meant to be more tongue-in-cheek than out-and-out sarcastic. But: the compiler message pane is almost always the first place to look for any failed build (I might be wrong, but I *thought* it displayed the errors pane automatically if the build failed; oh well). Anyway, no offence was intended. – Marc Gravell Oct 19 '11 at 10:26

4 Answers4

15

The extension method has to be in a static class:

public static class IntExtensions 
{
    public static int SetFlag_Old(this int i, int flag, bool set = true)
    {
        return (set) ? i | flag : ((i & flag) != 0) ? (i - flag) : i;
    }
}

http://msdn.microsoft.com/en-us/library/bb383977.aspx

Richard Dalton
  • 35,513
  • 6
  • 73
  • 91
  • Even if you fixes all the problems it maybe doesn't show up in Intellisense. Simply enter blindy your whole method name and after entering the opening bracket the intellisense will show you the parameters. – Oliver Oct 18 '11 at 09:39
  • Actually, it doesn't need to be in it's *own* class; simply, it must be in a `static` class; marking `Program` as `static` would achieve the same – Marc Gravell Oct 18 '11 at 09:41
  • @MarcGravell True, but I think it is better to have it in it's own class. I have updated my answer though. – Richard Dalton Oct 18 '11 at 09:43
1

Extension methods must be defined in a non-generic and static class. Program is not static.

See Extension methods must be defined in a non-generic static class

Community
  • 1
  • 1
Martin Peck
  • 11,440
  • 2
  • 42
  • 69
0

The code won't even compile. It will give the error Extension method must be defined in a non-generic static class. How much more information do you need? I'll re-write it for you:

Extension method must be defined in a non-generic static class

I'll add that you could make your Program class static. Considering its "entry" method (Main) is static I don't know why they didn't made it directly static in the code template.

xanatos
  • 109,618
  • 12
  • 197
  • 280
0

Class where extension method is located has to be static itself. Add static modifier to Program class.

Lloyd
  • 1,324
  • 7
  • 10