【问题标题】:C# Enum - How to Compare ValueC# 枚举 - 如何比较值
【发布时间】:2013-10-23 08:50:32
【问题描述】:

如何比较这个枚举的值

public enum AccountType
{
    Retailer = 1,
    Customer = 2,
    Manager = 3,
    Employee = 4
}

我正在尝试在 MVC4 控制器中比较此枚举的值,如下所示:

if (userProfile.AccountType.ToString() == "Retailer")
{
    return RedirectToAction("Create", "Retailer");
}
return RedirectToAction("Index", "Home");

我也试过了

if (userProfile.AccountType.Equals(1))
{
    return RedirectToAction("Create", "Retailer");
}
return RedirectToAction("Index", "Home");

在每种情况下,我都会得到一个未设置为对象实例的对象引用。

【问题讨论】:

  • 您确定 userProfile 不为空吗?
  • 为什么是枚举值的数字?如果您将枚举用作位标志,则枚举往往只需要数字等效项,并且在这种情况下可能会被删除。
  • 枚举可以在不转换为字符串的情况下进行比较,异常也可能来自实例 userProfile
  • @Stony 看起来像,我正在查看为什么 userProfile 为空
  • @ValOkafor 据您所知,我添加了代码以防止 userProfilenull时运行 if 语句>

标签: c# enums


【解决方案1】:

使用这个

if (userProfile.AccountType == AccountType.Retailer)
{
     ...
}

如果您想从 AccountType 枚举中获取 int 并进行比较(不知道为什么),请执行以下操作:

if((int)userProfile.AccountType == 1)
{ 
     ...
}

Objet reference not set to an instance of an object 异常是因为您的 userProfile 是 null 并且您获得了 null 的属性。检查为什么它没有设置。

编辑(感谢@Rik 和@KonradMorawski):

也许你可以先做一些检查:

if(userProfile!=null)
{
}

if(userProfile==null)
{
   throw new ArgumentNullException(nameof(userProfile)); // or any other exception
}

【讨论】:

  • 如果userProfile.AccountType 字段(属性)是AccountType 类型,那么它不能为空,因为枚举在C# 中不能为空。我宁愿打赌userProfile 本身是空的。
  • 可能是userProfile为null,因为AccountType是枚举,因此是值类型,不能为null(除非明确设为Nullable)
  • 如果我想比较两个枚举。它会抛出一些异常吗?如果是,那么如何处理它会有什么异常
  • 比较枚举值时出现异常。我可以使用 .Equals 来比较而不是“==”来摆脱异常。这是我得到的异常。无法比较数组中的两个元素。 System.Collections.Generic.GenericArraySortHelper1.BinarySearch(T[] array, Int32 index, Int32 length, T value, IComparer1 比较器)在 System.Array.BinarySearch[T](T[] 数组,Int32 索引,Int32 长度,T 值,IComparer`1 比较器)在 System.Array.BinarySearch[T ] 在 System.RuntimeType.GetEnumName(Object value) 在 System.Enum.InternalFormat(RuntimeType eT, Object value)
【解决方案2】:

你可以使用Enum.Parse,如果是字符串的话

AccountType account = (AccountType)Enum.Parse(typeof(AccountType), "Retailer")

【讨论】:

  • 虽然技术上可行,但出于速度、本地化和其他问题,不建议这样做。
【解决方案3】:

比较:

if (userProfile.AccountType == AccountType.Retailer)
{
    //your code
}

为了防止 NullPointerException,您可以在比较 AccountType 之前添加以下条件:

if(userProfile != null)
{
    if (userProfile.AccountType == AccountType.Retailer)
    {
       //your code
    }
}

或更短的版本:

if (userProfile !=null && userProfile.AccountType == AccountType.Retailer)
{
    //your code
}

【讨论】:

  • 谢谢,这有效,但我仍然得到一个空引用,所以我逐步完成我的登录逻辑
  • @ValOkafor 检查我的解释
  • @ValOkafor 正如 wudzik 所说,您可能指的是未初始化的 userProfile,因此请确保在检查条件之前将其初始化或添加以下代码(添加到答案中)。
【解决方案4】:

您可以使用扩展方法用更少的代码做同样的事情。

public enum AccountType
{
    Retailer = 1,
    Customer = 2,
    Manager = 3,
    Employee = 4
}

static class AccountTypeMethods
{
    public static bool IsRetailer(this AccountType ac)
    {
        return ac == AccountType.Retailer;
    }
}

并使用:

if (userProfile.AccountType.isRetailer())
{
    //your code
}

我建议将AccountType 重命名为AccountIt's not a name convention.

【讨论】:

    【解决方案5】:

    您应该在比较之前将字符串转换为枚举值。

    Enum.TryParse("Retailer", out AccountType accountType);
    

    然后

    if (userProfile?.AccountType == accountType)
    {
        //your code
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-14
      • 1970-01-01
      • 2012-05-04
      • 1970-01-01
      • 2011-03-03
      相关资源
      最近更新 更多