Search code examples
c#excelpackage

Overloading Extension Methods


Is it possible to overload Extension Methods?

I did something like this

    public static ExcelWorksheet CreateSheet(this ExcelPackage thisPackage, List<Document> list)
    {
        ExcelWorksheet worksheet = thisPackage.Workbook.Worksheets.Add("Documents");
        return worksheet;
    }
    public static ExcelWorksheet CreateSheet(this ExcelWorksheet thisPackage, List<Book> list)
    {
        ExcelWorksheet worksheet = thisPackage.Workbook.Worksheets.Add("Books");
        return worksheet;
    }

In this case, It recognizes only the first method as an extension method and Ignores the second method without any Compiler Warnings or Errors.


Solution

  • You aren't overloading here as the extension methods are on different objects, ExcelPackage and ExcelWorksheet. Overloading extension methods is acceptable though.