optional parameters must appear after all required parameters in c# [duplicate]

萝らか妹 提交于 2019-12-18 09:42:34

问题


Method 1

public List<IndentItems> GetIndentsByStatus(string projectAddress, string jobAddress, string currentStatus,string ddlevent)    
{    
    List<IndentItems> indentItems =null;
    indentItems = GetIndentFilledInfo(filterdReports, false,null ,ddlevent);
    return indentItems;    
}

Method 2

public List<IndentItems> GetIndentFilledInfo(List<SurveyFeedback> surveyFeedbacks, bool hasupdate, string indentType = null,string ddlevent)    
{
}

From Method1 I'm calling the second method and in method2 based on EventID I will get data. But it was showing Compiler Error Message:

CS1737: Optional parameters must appear after all required parameters.


回答1:


You need to move your optional parameters to the end of the parameter list:

from MSDN:

Optional parameters are defined at the end of the parameter list, after any required parameters. If the caller provides an argument for any one of a succession of optional parameters, it must provide arguments for all preceding optional parameters. Comma-separated gaps in the argument list are not supported. For example, in the following code, instance method ExampleMethod is defined with one required and two optional parameters.

public List<IndentItems> GetIndentFilledInfo(
        List<SurveyFeedback> surveyFeedbacks, 
        bool hasupdate,
        string ddlevent,
        string indentType = null)

More Read Here




回答2:


optional params should be after all of you method params:

public List<IndentItems> GetIndentFilledInfo(
    List<SurveyFeedback> surveyFeedbacks, 
    bool hasupdate, 
    string ddlevent,
    string indentType = null)    
{
    // Codes here
}

MSDN



来源:https://stackoverflow.com/questions/27316789/optional-parameters-must-appear-after-all-required-parameters-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!