testingTester
testingTester

Reputation: 105

Trying to use optional parameters in c#

But I am receiving the error

Optional parameters must appear after all required parameters

Though my optional parameter is appearing after my required parameter? What am I doing wrong here?

public string UserAnswer(string optInput = null, [Optional] int num1)
{
        bool isOperatorValid;

        do
        {
            string answer = optInput ?? Console.ReadLine();
            isOperatorValid = true;

            switch (answer)
            {
                case "a":
                    Console.WriteLine($"Your result: {num1} + {num2} = " + (num1 + num2));
                    break;

                case "s":
                    Console.WriteLine($"Your result: {num1} - {num2} = " + (num1 - num2));
                    break;

                case "m":
                    Console.WriteLine($"Your result: {num1} * {num2} = " + (num1 * num2));
                    break;

                case "d":
                    Console.WriteLine($"Your result: {num1} / {num2} = " + (num1 / num2));
                    break;

                default:
                    Console.WriteLine("Invalid input please try again");
                    isOperatorValid = false;
                    break;
            };
        } while (!isOperatorValid);

        return optInput;
}

Upvotes: 2

Views: 13427

Answers (3)

Tyler
Tyler

Reputation: 25

Make your method header

public string UserAnswer(string optInput = null, int num1 = 0)

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499790

The problem is that you're using [Optional] instead of the C# language integration for optional parameters. As far as the language is concerned (in terms of the method declaration part), num1 is a required parameter, so it has to come before optInput. The compiler does know about OptionalAttribute when consuming methods, but not when declaring them.

Assuming you wanted both parameters to be optional, use the language integration for both:

public string UserAnswer(string optInput = null, int num1 = 0)

I would strongly advise against using OptionalAttribute explicitly - there's no reason to do so now there's language support, and it's more likely to cause confusion than to help when it comes to anyone else reading the code.

Upvotes: 17

user12447201
user12447201

Reputation:

Any parameter in a function signature that has an assignment is an optional parameter. They don't need to have the optional attribute.

public string UserAnswer(string optInput = null, int num1 = 0)

In this example both optInput and num1 are optional. Going off your commment

i want the num1 and num2s to be optional in the sense that they can be used if a value is entered for them otherwise use the console.readline

We can't do exactly this. But we can change our parameters in a way that makes this easier. We can make the integer nullable

public string UserAnswer(string optInput = null, int? num1 = null)

Then this way we can use the value only when it isn't null. This is useful when any valid integer is considered a valid input and we need a way to determine if the argument was provided or not.

Upvotes: 3

Related Questions