One approach is overloaded methods
public ObservableCollection<ERCErrors> ErrorCollectionWithValue (string ErrorDode, int MulCopyNo, int param1) { //do some thing with param1 } public ObservableCollection<ERCErrors> ErrorCollectionWithValue (string ErrorDode, int MulCopyNo, int param1,int param2) { //do some thing with param1 and param3 } public ObservableCollection<ERCErrors> ErrorCollectionWithValue (string ErrorDode, int MulCopyNo, int param1, int param2, int param3) { //do some thing with param1, param2 and param3 }
then they would all be valid
var err = ErrorCollectionWithValue("text", 10, 1); var err = ErrorCollectionWithValue("text", 10, 1,2); var err = ErrorCollectionWithValue("text", 10, 1,2,3);
Another approach is to use optional parameters. You only need one method, not 3 in the first approach.
public ObservableCollection<ERCErrors> ErrorCollectionWithValue (string ErrorDode, int MulCopyNo, int param1 = 0, int param2 = 0, optional int param3 = 0) { }
they are still valid
var err = ErrorCollectionWithValue("text", 10, 1); //defaults param2 and param3 to 0 var err = ErrorCollectionWithValue("text", 10, 1,2); //defaults param3 to 0 var err = ErrorCollectionWithValue("text", 10, 1,2,3);
To skip any of the optional parameters, you need to use named parameters and this , which is only supported in C # 4.0 and above
var err = ErrorCollectionWithValue("text", param3: 5);
In the other two approaches, you cannot skip the order of parameters.