gpt4 book ai didi

c# - 将 enumType 值与 int 进行比较

转载 作者:行者123 更新时间:2023-11-30 19:21:25 25 4
gpt4 key购买 nike

我正在开发 C# 应用程序并且我有以下枚举:

public enum TourismItemType : int
{
Destination = 1,
PointOfInterest = 2,
Content = 3
}

我还有一个 int 变量,我想检查该变量以了解它是否等于 TourismItemType.Destination,如下所示:

int tourismType;
if (int.TryParse(NavigationContext.QueryString.Values.First(), out tourismType))
{
switch (tourismType)
{
case TourismItemType.Destination:
ShowDestinationInfo();
break;
case TourismItemType.PointOfInterest:
ShowPointOfInterestInfo();
break;
}
}

但是它会抛出一个错误。

我该怎么做?

谢谢。

最佳答案

tourismType 转换为您的枚举类型,因为没有从 int 的隐式转换。

switch ((TourismItemType)tourismType)
//...

关于c# - 将 enumType 值与 int 进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3361699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com