overloading on basis of return type only

2019-02-14 10:56发布

问题:

I have a situation where i want to return List<> from this function

public DataTable GetSubCategoriesBySubCatID(Guid SubCategoryID)

So what i want is

public List<SubCategories> GetSubCategoriesBySubCatID(Guid SubCategoryID)

I know overloading is not supported on the basis of return type only,I just donot want to duplicate same code in both functions.

Whats the best way to achieve this without impacting the references which hold true for first function

回答1:

Give them different names:

public DataTable GetSubCategoryTableBySubCatID(Guid subCatID)

public List<SubCategory> GetSubCategoryListBySubCatID(Guid subCatID)

Aside from anything else, that will make it clearer which method you're interested in when you're reading the calling code.

If those should be implemented in a common way, write a private method containing the common core, and call it from both of the public methods. For example, you might use a delegate to do the "I've found a result, add it to your collection" part, or use an iterator block:

// "action" will be called on each sub-category
private void FindSubCategoriesBySubCatID(Guid subCatID,
                                         Action<SubCategory> action)

private IEnumerable<SubCategory> FindSubCategoriesBySubCatID(Guid subCatID)


回答2:

Use generics like below.

    public T GetSubCategoriesBySubCatID<T>(Guid SubCategoryID)
    {
        T value = ...;
        return value;
    }


回答3:

I would define

public IEnumerable<SubCategories> GetSubCategoriesBySubCatID(Guid SubCategoryID);

The implementing class of this method is free to use any collection or container that implements IEnumerable{SubCategories}



回答4:

Can be done and this is how

As many folks have already explained that return type overloading is not supported by C#. Infact it is supported by CTS. However using interface and if absolutely required by scenarios in your application We can mimic return type method overloading using explicit interface implementation

We can define two interfaces having same method signature but different return types for e.g.

Interface I1
{
    DataTable GetSubCategoriesBySubCatID(Guid SubCategoryID);
}

Interface I2
{
    List<SubCategories> GetSubCategoriesBySubCatID(Guid SubCategoryID);
}

We define our class which will implement both the interfaces

public class CategoryFinder:I1, I2
{
     public DataTable GetSubCategoriesBySubCatID(Guid SubCategoryID) //Implicitly implementing interface 
     {   
          //processing
          return _dt; 
     }


     List<SubCategories> I2.GetSubCategoriesBySubCatID(Guid SubCategoryID) //explicit implementing interface
     {
           //processing
           return _list<>
     }
}

Since CategoryFinder class implements both conflicting GetSubCategoriesBySubCatID we will have to explicitly typecast the interface as follows

CategoryFinder cf = new CategoryFinder();
cf.GetSubCategoriesBySubCatID(Guid SubCategoryID); // will call the implicit return type which is data table
((I1)cf).GetSubCategoriesBySubCatID(Guid SubCategoryID); // will return **I1** implementation i.e datatable
((I2)cf).GetSubCategoriesBySubCatID(Guid SubCategoryID); // will return **I2** implementation i.e list