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
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.
We define our class which will implement both the interfaces
Since CategoryFinder class implements both conflicting GetSubCategoriesBySubCatID we will have to explicitly typecast the interface as follows
I would define
The implementing class of this method is free to use any collection or container that implements IEnumerable{SubCategories}
Give them different names:
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:
Use generics like below.