如何在C#中比较字符串和枚举
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11508865/
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
how to compare string with enum in C#
提问by Gururaj
string strName = "John";
public enum Name { John,Peter }
private void DoSomething(string myname)
{
case1:
if(myname.Equals(Name.John) //returns false
{
}
case2:
if(myname == Name.John) //compilation error
{
}
case3:
if(myname.Equals(Name.John.ToString()) //returns true (correct comparision)
{
}
}
when I use .Equalsit is reference compare and when I use ==it means value compare.
当我使用.Equals它时是参考比较,当我使用==它时意味着值比较。
Is there a better code instead of converting the enum value to ToString()for comparison? because it destroys the purpose of value type enum and also, ToString()on enum is deprecated??
是否有更好的代码而不是将枚举值转换为ToString()进行比较?因为它破坏了值类型枚举的目的,而且,ToString()不推荐使用枚举??
回答by Paul Fleming
You can parse the string value and do enum comparisons.
您可以解析字符串值并进行枚举比较。
Enum.TryParse: See http://msdn.microsoft.com/en-us/library/dd783499.aspx
Enum.TryParse:见http://msdn.microsoft.com/en-us/library/dd783499.aspx
Name result;
if (Enum.TryParse(myname, out result))
{
switch (result)
{
case Name.John:
/* do 'John' logic */
break;
default:
/* unexpected/unspecialized enum value, do general logic */
break;
}
}
else
{
/* invalid enum value, handle */
}
If you are just comparing a single value:
如果您只是比较单个值:
Name result;
if (Enum.TryParse(myname, out result) && result == Name.John)
{
/* do 'John' logic */
}
else
{
/* do non-'John' logic */
}
回答by Sindre
One solution could be to get the type of the enum, and then the types name.
一种解决方案是获取枚举的类型,然后获取类型名称。
myname.Equals(Enum.GetName(typeof(Name)))
http://msdn.microsoft.com/en-us/library/system.enum.getname.aspx
http://msdn.microsoft.com/en-us/library/system.enum.getname.aspx
回答by dlev
You can use the Enum.TryParse()method to convert a string to the equivalent enumerated value (assuming it exists):
您可以使用该Enum.TryParse()方法将字符串转换为等效的枚举值(假设它存在):
Name myName;
if (Enum.TryParse(nameString, out myName))
{
switch (myName) { case John: ... }
}
回答by Brad
I think you're looking for the Enum.Parse()method.
我认为你正在寻找Enum.Parse()方法。
if(myname.Equals(Enum.Parse(Name.John)) //returns false
{
}
回答by Ria
If you using .NET4 or later you can use Enum.TryParse. and Enum.Parseis available for .NET2 and later
如果您使用 .NET4 或更高版本,则可以使用Enum.TryParse. 并且Enum.Parse可用于 .NET2 及更高版本
// .NET2 and later
try
{
switch (Enum.Parse(typeof(Names), myName))
{
case John: ...
case Peter: ...
}
}
// .NET4 and later
Name name;
if (Enum.TryParse(myName, out name))
switch (name)
{
case John: ...
case Peter: ...
}
回答by ccoutinho
For some reason, the given solutions didn't workout for me. I had to do in a slighly different way:
出于某种原因,给定的解决方案不适合我。我不得不以稍微不同的方式做:
Name myName;
if (Enum.TryParse<Name>(nameString, out myName))
{
switch (myName) { case John: ... }
}
Hope it helps someone :)
希望它可以帮助某人:)

