我有一个接受两个参数的方法,现在我需要传递一个附加参数,但是调用此参数的所有其他代码可能没有传递该第三个参数。
下面是一个丑陋的hack,但是我只是不熟悉重载的过程,而且我们陷于.NET 2.0中,因此尽管工作正常,但我还是忍不住感觉到它的错误。
public static void AddPlanFunds(ParticipantResultsPlan planNode, Plan plan)
{
AddPlanFunds(planNode, plan, -1);
}
public static void AddPlanFunds(ParticipantResultsPlan planNode, Plan plan, int participantId)
{
planNode.PlanFunds = new CommonPlanFunds();
// Add single class funds
AddSingleClassFunds(planNode.PlanFunds, plan);
// Add portfolios
AddPortfolios(planNode.PlanFunds, plan,participantId);
}
我应该如何做过载?
谢谢!
最佳答案
您的代码是执行此操作的标准方法。这不是一个丑陋的hack。
但是,您应该考虑采用int?
并传递null
而不是-1
。
You can create optional parameters in C# 2 to be called from C# 4, using attributes。
关于c# - 有没有办法在2.0中执行C#4.0可选参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5008478/