重载仅返回类型的基础 [英] overloading on basis of return type only
问题描述
我有,我想返回列表℃的情况下,利用此功能
I have a situation where i want to return List<> from this function
public DataTable GetSubCategoriesBySubCatID(Guid SubCategoryID)
所以,我要的是
public List<SubCategories> GetSubCategoriesBySubCatID(Guid SubCategoryID)
我知道超载是不支持的返回类型的基础上而已,我只是DONOT希望复制在这两个功能相同的代码。
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
推荐答案
给他们不同的名称:
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)
这篇关于重载仅返回类型的基础的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!