'Can't parse protobuf enum from int

I'm working on ASP.NET CORE 3.1 with protos3 and I need to parse an int to an enum on protouf. When I use Enum.TryParse(int, out MyEnum myEnum), it succeeds even if the value doesn't exists. For example:

Proto file:

syntax = "proto3";
package MyProtos;

enum MyEnum { A = 0; B = 1; C = 2; }

I run the following code:

int x = 4;
if(Enum.TryParse<MyEnum>(x, out var myEnum))
    Console.WriteLine($"MyEnum = {myEnum}")
x = 1
if(Enum.TryParse<MyEnum>(x, out myEnum))
    Console.WriteLine($"MyEnum = {myEnum}")

The Output is:

MyEnum = 4
MyEnum = B

Expected Output is:

MyEnum = B


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source