Can Enum return string instead of integer value?

Spoiler: Answer is NO!

Current implementation C# Enum is very primitive; it can only return integer value for the selected enumerator option. If your project requirement needs something like returning a string for Enum, you have to come up with your own solution.

You cannot declare enum like this:

1
2
3
4
5
6
7
// enum type can't have string value
public enum WorkerType
{
  Employee = "WT001",
  Contractor = "WT003",
  Resigned = "WT099"
}

One solution usually popular is to declare Description attribute to each enumerators (for example, [Description("WT003")] for Contractor) and use it to store a respective string value. This is more of a hack work to as intend of Description attribute is to provide description for enumerators for documentation purpose only. With that, you then also require to use reflection to pull the string from Description attribute. This is not a clean solution.

I think the best way to implement the solution is through creating a dedicated class instead and provide all implementation underneath to make it behave like an enum.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class WorkerType
{
  private WorkerType(string value)
  {
    Value = value;
  }

  public string Value { get; }

  public static WorkerType Employee => new WorkerType("WT001");
  public static WorkerType Contractor => new WorkerType("WT003");
  public static WorkerType Resigned => new WorkerType("WT099");
}

class Program
{
  static void Main(string[] args)
  {
    WorkerType workerType = WorkerType.Contractor;
    Console.WriteLine($"Contractor Value: {workerType.Value}");
  }
}

We set the Constructor of the class as private which will keeping this class initiated anywhere in the project. All this class is exposing is defined members and Value property in which are interested in.

We can then further extend this class by overriding Equals and implementing == and != operators. Complete implementation will look like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;

namespace ConsoleApp
{
  public class WorkerType
  {
    private WorkerType(string value)
    {
      Value = value;
    }

    public string Value { get; }

    public static WorkerType Employee => new WorkerType("WT001");
    public static WorkerType Contractor => new WorkerType("WT003");
    public static WorkerType Resigned => new WorkerType("WT099");

    public override bool Equals(Object obj)
    {
      return obj is WorkerType && this.Value == ((WorkerType)obj).Value;
    }

    public static bool operator ==(WorkerType x, WorkerType y)
    {
      return x.Value == y.Value;
    }

    public static bool operator !=(WorkerType x, WorkerType y)
    {
      return !(x == y);
    }
  }

  class Program
  {
    static void Main(string[] args)
    {
      WorkerType workerType = WorkerType.Contractor;
      Console.WriteLine($"Contractor Value: {workerType.Value}");

      if (workerType.Equals(WorkerType.Contractor))
      {
        Console.WriteLine("And it matches using Equals method.");
      }

      if (workerType == WorkerType.Contractor)
      {
        Console.WriteLine("Yea! It also matches using == Operator.");
      }

      if (workerType != WorkerType.Resigned)
      {
        Console.WriteLine("And also with != Operator.");
      }

      Console.ReadKey();
    }
  }
}
Works like Enum!

Once we establish the pattern using the above implementation, it’s just a matter of replicating this pattern for other complex enums which has this string return value requirements. I hope this help!

0 Comments