C# 枚举 - 如何比较值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19537083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:13:02  来源:igfitidea点击:

C# Enum - How to Compare Value

c#enums

提问by Val Okafor

How can I compare the value of this enum

我如何比较这个枚举的值

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

I am trying to compare the value of this enum in an MVC4 controller like so:

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

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

I also tried this

我也试过这个

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

In each case I get an Object reference not set to an instance of an object.

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

采纳答案by Kamil Budziewski

use this

用这个

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

If you want to get int from your AccountType enum and compare it (don't know why) do this:

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

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

Objet reference not set to an instance of an objectexception is because your userProfile is nulland you are getting property of null. Check in debug why it's not set.

Objet reference not set to an instance of an object例外是因为您的 userProfile 为空,而您获得的属性为空。检查调试为什么没有设置。

EDIT (thanks to @Rik and @KonradMorawski) :

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

Maybe you can do some check before:

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

if(userProfile!=null)
{
}

or

或者

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

回答by Tafari

Comparision:

比较:

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

In case to prevent the NullPointerExceptionyou could add the following condition before comparing the AccountType:

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

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

or shorter version:

或更短的版本:

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

回答by Satpal

You can use Enum.Parselike, if it is string

你可以使用Enum.Parse喜欢,如果它是字符串

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

回答by Dherik

You can use extension methods to do the same thing with less code.

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

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;
    }
}

And to use:

并使用:

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

I would recommend to rename the AccountTypeto Account. It's not a name convention.

我建议将其重命名AccountTypeAccount. 这不是命名约定

回答by Ayberk

You should convert the string to an enumeration value before comparing.

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

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

Then

然后

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