how to compare string with enum in C#

前端 未结 6 1837
自闭症患者
自闭症患者 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:53

    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(nameString, out myName))
    {
        switch (myName) { case John: ... }
    }
    

    Hope it helps someone :)

提交回复
热议问题