Home > Mobile >  Extension method gives compilation error when can be accessed as static method
Extension method gives compilation error when can be accessed as static method

Time:06-16

I declared an extension method as follows

namespace ExtensionMethods
{
    public static class MyExtensions
    {
        const string Nil = "$$$NIL$$$";
        public static bool IsEmptyNullOrNil(this string str)
        {
            if (string.IsNullOrEmpty(str) || str == Nil)
            {
                return true;
            }
            return false;
        }
    }
}

And in my controller class, I included the namespace. But the program does not compile. But I could use the function as normal static method.

enter image description here

Any help will be appreciated.

CodePudding user response:

Your extension method can be accessed in the following ways

if (!employeeFilter.Name.IsEmptyNullOrNil())

OR

if (!MyExtensions.IsEmptyNullOrNil(employeeFilter.Name))
  • Related