Skip to content

Improve error for casting negative value to enum #32057

@hughbe

Description

@hughbe

Example:

using System;

public class Program
{
    public void T()
    {
        var value = (ConsoleColor)-1;
    }
}

We get two error messages:

Example.cs(7,22): error CS0119: 'ConsoleColor' is a type, which is not valid in the given context
Example.cs(7,21): error CS0075: To cast a negative value, you must enclose the value in parentheses.

Can we get rid of the first error if the second is present? It's fairly obvious what we're trying to do and I often get this error as I forgot to add the parens

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions