Home > Backend Development > C++ > C# vs. Java Enums: How Do I Add Functionality to C# Enums Like in Java?

C# vs. Java Enums: How Do I Add Functionality to C# Enums Like in Java?

Mary-Kate Olsen
Release: 2025-01-12 07:37:43
Original
725 people have browsed it

C# vs. Java Enums: How Do I Add Functionality to C# Enums Like in Java?

C# and Java Enumerations: Difference Analysis

For developers moving from Java to C#, it is critical to understand the differences between enumerations in the two languages. While C# enums look cleaner, they initially lack some of Java's advanced features.

Main Differences

  • Constructor parameters: Java enumerations can have constructor parameters, allowing initialization with additional data. C# enums have no constructors.
  • Methods and Properties: Java enumerations can contain methods and properties. C# enumerations cannot define methods or properties directly; they must be defined as extension methods.
  • Annotation equivalent: In Java, you can use annotations to associate metadata with an enumeration. In C#, custom attributes have similar functionality.

Bridging the differences

  • Extension methods: C# extension methods provide the functionality of Java enumeration methods and properties.
  • Custom Attributes: C# custom attributes can be used to store additional data next to the enumeration value.

Java Planet enumeration example in C#

The following C# code demonstrates an equivalent implementation of Sun's Planet enumeration, including extension methods and custom attributes:

<code class="language-csharp">using System;
using System.Reflection;

namespace PlanetEnum
{
    public static class Planets
    {
        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();
        }

        public const double G = 6.67300E-11;

        private static PlanetAttr GetAttr(Planet p)
        {
            return (PlanetAttr)Attribute.GetCustomAttribute(ForValue(p), typeof(PlanetAttr));
        }

        private static MemberInfo ForValue(Planet p)
        {
            return typeof(Planet).GetField(Enum.GetName(typeof(Planet), p));
        }
    }

    [AttributeUsage(AttributeTargets.Field)]
    public class PlanetAttr : Attribute
    {
        public PlanetAttr(double mass, double radius)
        {
            Mass = mass;
            Radius = radius;
        }

        public double Mass { get; private set; }
        public double Radius { get; private set; }
    }

    public enum Planet
    {
        [PlanetAttr(3.303e+23, 2.4397e6)] MERCURY,
        [PlanetAttr(4.869e+24, 6.0518e6)] VENUS,
        [PlanetAttr(5.976e+24, 6.37814e6)] EARTH,
        [PlanetAttr(6.421e+23, 3.3972e6)] MARS,
        [PlanetAttr(1.9e+27,   7.1492e7)] JUPITER,
        [PlanetAttr(5.688e+26, 6.0268e7)] SATURN,
        [PlanetAttr(8.686e+25, 2.5559e7)] URANUS,
        [PlanetAttr(1.024e+26, 2.4746e7)] NEPTUNE,
        [PlanetAttr(1.27e+22,  1.137e6)] PLUTO
    }
}</code>
Copy after login

This code demonstrates how to use extension methods and custom attributes to extend C# enums with additional functionality, effectively bridging the gap with Java enums.

The above is the detailed content of C# vs. Java Enums: How Do I Add Functionality to C# Enums Like in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template