Skip to content

The error: CS0236 is reported incorrectly on a nullable enum property with same name as enum type.  #56348

@JoeRobich

Description

@JoeRobich

As reported by @PaulHazelton in dotnet/vscode-csharp#4763

The following code produces the CS0236 error incorrectly:

#nullable enable
namespace MyNamespace
{
	public enum MyEnum
	{
		VALUE1,
		VALUE2
	}

	public class MyClass
	{
		public MyEnum? MyEnum { get; set; } = MyEnum.VALUE1;	// <-- this line produces CS0236
		// If the question mark is omitted, there is no error reported.
	}
}

Without the question mark, the compiler correctly knows that MyEnum at (Ln 12, Col 46) is referring to the enum type, not the property. However, with the question mark the error is reported, the compiler seems to think that (Ln 12, Col 47) refers to the property MyEnum instead of the type.

I'm not sure if this issue is caused by the omnisharp-vscode extension, or by a compiler (and I'm not sure what compiler I'm using).

Note that this error is entirely avoidable by having the property name differ from the type name, however this is recommended by Microsoft

CONSIDER giving a property the same name as its type.
For example, the following property correctly gets and sets an enum value named Color, so the property is named Color:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions