Skip to content
Discussion options

You must be logged in to vote

Even though you only have the values A, B, and C defined, the value of any MyEnum instance could be an undefined value. This is why the Enum.IsDefined method exists.

using System;

MyEnum myenum = (MyEnum)(-1);
Console.WriteLine(myenum);
Console.WriteLine(myenum is MyEnum.A || myenum is MyEnum.B || myenum is MyEnum.C);
Console.WriteLine(Enum.IsDefined<MyEnum>(myenum));
Console.WriteLine(myenum switch
	{
		MyEnum.A => "a",
		MyEnum.B => "b",
		MyEnum.C => "c",
		_ => "undefined"
	});

public enum MyEnum
{
	A,
	B,
	C
}

Enum.IsDefined: https://docs.microsoft.com/en-us/dotnet/api/system.enum.isdefined

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@royberris
Comment options

Answer selected by royberris
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants