4ndy
4ndy

Reputation: 600

Enum Syntax Error? Identifier Expected? C# .NET Core 2.0

I have done many Enum declarations before in .NET framework, but why is this not working in .NET Core 2.0?

public enum SomeOtherName
{
    Add,
    Subtract,
    Multiply,
    Divide
}

public static IEnumerable<string> Calculate(int num1, int num2, SomeOtherName operator)
    {

    }

VisualStudio2017 flags this as an error CS1001 Identifier expected and CS1003 Syntax error, ',' expected

Likewise, if I try something like

var op = Operator.Add;

in the method, I get the same error. Why?

Upvotes: 0

Views: 1118

Answers (1)

Frederik Carlier
Frederik Carlier

Reputation: 4776

That's because operator is a reserved keyword in C#. Can you change the name of your parameter so something else, such as operation instead?

public enum Operator
{
    Add,
    Subtract,
    Multiply,
    Divide
}

public static IEnumerable<string> Calculate(int num1, int num2, Operator operation)
{

}

Upvotes: 3

Related Questions