Search code examples
c#.netasp.net-coreextension-methods.net-6.0

Extension method gives compilation error when can be accessed as static method


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.


Solution

  • Your extension method can be accessed in the following ways

    if (!employeeFilter.Name.IsEmptyNullOrNil())
    

    OR

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