I can't get my extension method to work (C #)

Any ideas? I marked it as static, but it does not work!

class ExtensionMethods
{
    public static int Add(this int number, int increment)
    {
        return number + increment;
    } 
}
+5
source share
2 answers

There is no static in the class. Did the compiler have to tell you this?

public static class ExtensionMethods
+19
source

I think it needs to be defined in a static class:

namespace MyNameSpace
{
    public static class ExtensionMethods
    {
        public static int Add(this int number, int increment)
        {
            return number + increment;
        } 
    }
}

You must also include using MyNameSpace;in the code file in which you want to use them if it is not in the same namespace

+11
source

All Articles