Table of Contents
C# vs Java Enums: A Comparison
Key Differences
Overcoming the Differences
Planet Enum Example
Home Java javaTutorial C# vs Java Enums: Which One Offers More Functionality?

C# vs Java Enums: Which One Offers More Functionality?

Nov 01, 2024 pm 03:25 PM

 C# vs Java Enums: Which One Offers More Functionality?

C# vs Java Enums: A Comparison

Java enums offer a powerful way to define constants with additional functionality, such as constructors, methods, and instance variables. While C# enums may seem simpler at first glance, they have drawbacks compared to Java's robust implementation.

Key Differences

Feature Java C#
Constructors Supported Not supported
Methods Supported Requires extension methods
Instance variables Supported Requires custom attributes or dictionaries
Values Immutable Mutable

Overcoming the Differences

Extension Methods: To add functionality similar to Java enums, C# allows defining extension methods on enums. For example, the following code provides methods to calculate surface gravity and surface weight:

<code class="csharp">public static class Planets
{
    public static double GetSurfaceGravity(this Planet p) { /* ... */ }
    public static double GetSurfaceWeight(this Planet p, double otherMass) { /* ... */ }
}</code>
Copy after login

Custom Attributes: Custom attributes can be used to store additional information on enums. Here is a sample implementation:

<code class="csharp">[PlanetAttr(3.303e+23, 2.4397e6)]
public enum Planet
{
    MERCURY,
    VENUS,
    EARTH,
    // ...
}

public class PlanetAttr : Attribute
{
    public double Mass { get; private set; }
    public double Radius { get; private set; }

    public PlanetAttr(double mass, double radius)
    {
        Mass = mass;
        Radius = radius;
    }
}</code>
Copy after login

By accessing the custom attribute on an enum value, you can retrieve the associated properties.

Planet Enum Example

Here is a C# equivalent to Sun's Planet enum using extension methods and custom attributes:

<code class="csharp">public enum Planet
{
    [PlanetAttr(3.303e+23, 2.4397e6)] MERCURY,
    [PlanetAttr(4.869e+24, 6.0518e6)] VENUS,
    [PlanetAttr(5.976e+24, 6.37814e6)] EARTH,
    // ...
}

public static class Planets
{
    public const double G = 6.67300E-11;

    public static double GetSurfaceGravity(this Planet p)
    {
        PlanetAttr attr = GetAttr(p);
        return G * attr.Mass / (attr.Radius * attr.Radius);
    }

    public static double GetSurfaceWeight(this Planet p, double otherMass)
    {
        return otherMass * p.GetSurfaceGravity();
    }

    private static PlanetAttr GetAttr(Planet p)
    {
        FieldInfo field = typeof(Planet).GetField(Enum.GetName(typeof(Planet), p));
        return field.GetCustomAttribute&lt;PlanetAttr&gt;();
    }
}</code>
Copy after login

This code defines the Planet enum with custom attributes for mass and radius. The Planets class provides extension methods for calculating surface gravity and weight, which are accessible through the enums themselves.

The above is the detailed content of C# vs Java Enums: Which One Offers More Functionality?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte Mar 07, 2025 pm 06:09 PM

Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte

How does Java's classloading mechanism work, including different classloaders and their delegation models? How does Java's classloading mechanism work, including different classloaders and their delegation models? Mar 17, 2025 pm 05:35 PM

How does Java's classloading mechanism work, including different classloaders and their delegation models?

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? Mar 17, 2025 pm 05:43 PM

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?

Iceberg: The Future of Data Lake Tables Iceberg: The Future of Data Lake Tables Mar 07, 2025 pm 06:31 PM

Iceberg: The Future of Data Lake Tables

How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? Mar 17, 2025 pm 05:46 PM

How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?

Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed Mar 07, 2025 pm 05:52 PM

Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed

Node.js 20: Key Performance Boosts and New Features Node.js 20: Key Performance Boosts and New Features Mar 07, 2025 pm 06:12 PM

Node.js 20: Key Performance Boosts and New Features

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? Mar 17, 2025 pm 05:44 PM

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?

See all articles