Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I call an extension method?

Tags:

c#

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?

like image 963
Exitos Avatar asked Dec 05 '22 19:12

Exitos


1 Answers

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

like image 51
Richard Dalton Avatar answered Dec 25 '22 13:12

Richard Dalton