how to compare string with enum in C#

C#.Net

C# Problem Overview


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 .Equals it is reference compare and when I use == it means value compare.

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??

C# Solutions


Solution 1 - C#

You can use the Enum.TryParse() method to convert a string to the equivalent enumerated value (assuming it exists):

Name myName;
if (Enum.TryParse(nameString, out myName))
{
    switch (myName) { case John: ... }
}

Solution 2 - C#

You can parse the string value and do enum comparisons.

Enum.TryParse: See 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 */
}

Solution 3 - C#

If you using .NET4 or later you can use Enum.TryParse. and Enum.Parse is available for .NET2 and later

// .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: ...
    }

Solution 4 - C#

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

Solution 5 - C#

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 :)

Solution 6 - C#

I think you're looking for the Enum.Parse() method.

if(myname.Equals(Enum.Parse(Name.John)) //returns false
 {

 }

Solution 7 - C#

A slightly more elegant solution would be an string extension method:

public static bool Equals(this string enumString, Name value)
{
    if(Enum.TryParse<Name>(enumString, out var v))
    {
        return value == v;
    }

    return false;
}

This way you can directly use .Equals() on the string as in the OPs first example.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGururajView Question on Stackoverflow
Solution 1 - C#dlevView Answer on Stackoverflow
Solution 2 - C#Paul FlemingView Answer on Stackoverflow
Solution 3 - C#RiaView Answer on Stackoverflow
Solution 4 - C#SindreView Answer on Stackoverflow
Solution 5 - C#ccoutinhoView Answer on Stackoverflow
Solution 6 - C#BradView Answer on Stackoverflow
Solution 7 - C#Hefaistos68View Answer on Stackoverflow