C # Optional parameters / multiple required

Can I get the requirement for two optional parameters together? Here's an example:

public void ParamChoise(string A, bool B = false, string C = "Y")
    {
        // Stuff here
    }

      

If B is optional, I want the function to require C if B is true. From my logic, something like this:

public void ParamChoise(string A, (bool B = false, string C = "Y"))

      

Couldn't find anything for your search term. One possible way for me:

        /// <summary>
        /// If you want behaviour XXX to be enabled, please enter "C" for usage.
        /// </summary>
        /// <param name="A"></param>
        /// <param name="C"></param>
        public void ParamChoise(string A, string C = "Y")
        {
            // Stuff here
        }

      

Of course, I could give my function a comment like this, but writing a comment for an illogical given parameter is bad for me.

This case may be a bad example, but I'm sure it will happen again in the future.

Many thanks:).

EDIT FOUR:
Possible combination of parameters:

  • Only
  • A and (B and C)
  • NOT A and B
  • NOT A and C (because B gives a dot if C is required)
+3


source to share


3 answers


I think you want the old way - method overloading.

// b = true
public void ParamChoise(string A, string C)

// b = false
public void ParamChoise(string A)

      

They should call a private version of your original

private void ParamChoiseInternal(string A, bool B = false, string C = "")

      

You have to make sure that you give the 2 public methods a name that accurately conveys their meaning - this will help the programmer (perhaps you, I know) name the correct method for their current state.




After update

Same as above, the names reflect the requirement and you can still call your own private method. Overloads ensure your consistency

public void AOnly(string A)
public void AAndBAndC(string A, bool B, string C)

      

Fighting decoding these 2:

NOT A and B WITHOUT C !!!
NOT A AND C (because B gives a dot if C is required)

+6


source


You can use named arguments

public void ParamChoise(string A = "", bool B = false, string C = "Y") {...}

// call it
ParamChoise(A: "123");
ParamChoise(C: "123");
ParamChoise(C: "123", A: "123");
ParamChoise(B: true, C: "123", A: "123");

      



I found this extremely useful for methods with many optional parameters.

By explicitly specifying the parameter name, you can add new optional parameters anywhere later without destroying the existing code.

+1


source


From what I can gather, you want C to be optional WHEN b is false; As an exercise in method overloading, I think the closest you will get is:

void ParamChoise(string A, string C)
void ParamChoise(string A, bool B = true, string C = "Y")

      

You still have to decide in the calling file if Bool ... should be supplied, which makes it pointless, as you can now do:

void ParamChoiseBIsFalse(string A, string C)
void ParamChoiseBIsTrue(string A, string C = "Y")

      

You only have complex logic that cannot be translated directly into a simple overload. You either put this logic in the called party or the caller

+1


source







All Articles