DEV Community

Renaud Humbert-Labeaumaz
Renaud Humbert-Labeaumaz

Posted on • Originally published at rnowif.github.io on

2

C# for the Java Developer: Enums

Enums is a very common concept. It exists, of course, in Java and C# as well. However, Java and C# enums do not have the same capabilities. This blog post aims to show their differences.

In Java, enums are very much like regular classes: they can implement interfaces and have methods. However, they cannot inherit other classes or be explicitly instantiated. They can be viewed as final classes (or sealed classes in C#) that already inherit the virtual “enum” class, only have private constructor(s) and a set of pre-defined instances (the values of the enum).

For instance, let’s take the example of the HTTP status codes. In Java, it is possible to write this code:

enum HttpStatus implements Comparable<HttpStatus> {
  OK(200), SERVER_ERROR(500), NOT_FOUND(404);

  private final int code;

  HttpStatus(int code) {
      this.code = code;
  }

  // This is a regular instance method
  int code() {
      return code;
  }

  // This is the implementation of the Comparable interface
  @Override
  int compareTo(HttpStatus o) {
      // Http statuses will be sorted by status code
      return Integer.compare(code, o.code);
  }
}
Enter fullscreen mode Exit fullscreen mode
HttpStatus status = HttpStatus.OK;

// The 'code' method can be invoked like any other method
int code = status.code();
Enter fullscreen mode Exit fullscreen mode

In C#, enums are just integers in disguise. The previous snippet can be simulated in C# only because the code attribute happens to be an int. Otherwise, it would be very complex to have the same behaviour:

enum HttpStatus {
  // int value of the enum can be forced to a specific value
  OK = 200,
  NOT_FOUND = 404,
  SERVER_ERROR = 500
}
Enter fullscreen mode Exit fullscreen mode
HttpStatus status = HttpStatus.OK;

// The enum can be casted to an int to get its value
int code = (int) status;
Enter fullscreen mode Exit fullscreen mode

To sum up, Java enums are much more powerful than their C# counterparts. I often use these features when I write Java code and I think I would miss them if I had to write C# code on a daily basis.

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay