how to compare string with enum in C#

前端 未结 6 1834
自闭症患者
自闭症患者 2020-12-03 13:07
string strName = \"John\";
public enum Name { John,Peter }

private void DoSomething(string myname)
{
case1:
     if(myname.Equals(Name.John) //returns false
     {
         


        
6条回答
  •  执念已碎
    2020-12-03 13:55

    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

提交回复
热议问题