Search code examples
c#functionmethodsdefault-parameters

Default Parameters for method in c#


I have the following method signature where I want to give a default value to one of my parameters but I dont want to give any default value to the other parameter leadSourceStatus

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

But when I try this, I get error

Optional parameters must appear after all required parameters

What will be the best way to deal with this?


Solution

  • The best way to deal with it is to do what it told you to do, and put the optional param at the end:

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