c#中方法的默认参数

Default Parameters for method in c#

我有以下方法签名,我想为我的一个参数提供默认值,但我不想为另一个参数提供任何默认值 leadSourceStatus

protected PromotionCatalogResponseRootObject GetVideoPromotionCatalog(PromotionCatalogTypes catalogType = PromotionCatalogTypes.RESIDENTIAL, LeadSourceStatus leadSourceStatus)

但是当我尝试这样做时,出现错误

Optional parameters must appear after all required parameters

处理这个问题的最佳方法是什么?

最好的处理方法是按照它告诉你的去做,并将可选参数放在最后:

protected PromotionCatalogResponseRootObject GetVideoPromotionCatalog(LeadSourceStatus leadSourceStatus, PromotionCatalogTypes catalogType = PromotionCatalogTypes.RESIDENTIAL)

只需将可选参数放在最后,如错误消息所述

protected PromotionCatalogResponseRootObject GetVideoPromotionCatalog(LeadSourceStatus leadSourceStatus, PromotionCatalogTypes catalogType = PromotionCatalogTypes.RESIDENTIAL)