Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 105683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:28:40+00:00 2026-05-11T01:28:40+00:00

I’ve been programming in Java for a while and just got thrown onto a

  • 0

I’ve been programming in Java for a while and just got thrown onto a project that’s written entirely in C#. I’m trying to come up to speed in C#, and noticed enums used in several places in my new project, but at first glance, C#’s enums seem to be more simplistic than the Java 1.5+ implementation. Can anyone enumerate the differences between C# and Java enums, and how to overcome the differences? (I don’t want to start a language flame war, I just want to know how to do some things in C# that I used to do in Java). For example, could someone post a C# counterpart to Sun’s famous Planet enum example?

public enum Planet {   MERCURY (3.303e+23, 2.4397e6),   VENUS   (4.869e+24, 6.0518e6),   EARTH   (5.976e+24, 6.37814e6),   MARS    (6.421e+23, 3.3972e6),   JUPITER (1.9e+27,   7.1492e7),   SATURN  (5.688e+26, 6.0268e7),   URANUS  (8.686e+25, 2.5559e7),   NEPTUNE (1.024e+26, 2.4746e7),   PLUTO   (1.27e+22,  1.137e6);    private final double mass;   // in kilograms   private final double radius; // in meters   Planet(double mass, double radius) {       this.mass = mass;       this.radius = radius;   }   public double mass()   { return mass; }   public double radius() { return radius; }    // universal gravitational constant  (m3 kg-1 s-2)   public static final double G = 6.67300E-11;    public double surfaceGravity() {       return G * mass / (radius * radius);   }   public double surfaceWeight(double otherMass) {       return otherMass * surfaceGravity();   } }  // Example usage (slight modification of Sun's example): public static void main(String[] args) {     Planet pEarth = Planet.EARTH;     double earthRadius = pEarth.radius(); // Just threw it in to show usage      // Argument passed in is earth Weight.  Calculate weight on each planet:     double earthWeight = Double.parseDouble(args[0]);     double mass = earthWeight/pEarth.surfaceGravity();     for (Planet p : Planet.values())        System.out.printf('Your weight on %s is %f%n',                          p, p.surfaceWeight(mass)); }  // Example output: $ java Planet 175 Your weight on MERCURY is 66.107583 Your weight on VENUS is 158.374842 [etc ...] 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:28:41+00:00Added an answer on May 11, 2026 at 1:28 am

    Enumerations in the CLR are simply named constants. The underlying type must be integral. In Java an enumeration is more like a named instance of a type. That type can be quite complex and – as your example shows – contain multiple fields of various types.

    To port the example to C# I would just change the enum to an immutable class and expose static readonly instances of that class:

    using System; using System.Collections.Generic;  namespace ConsoleApplication1 {     class Program     {         static void Main(string[] args)         {             Planet planetEarth = Planet.MERCURY;              double earthRadius = pEarth.Radius; // Just threw it in to show usage             double earthWeight = double.Parse('123');             double earthMass   = earthWeight / pEarth.SurfaceGravity();              foreach (Planet p in Planet.Values)                 Console.WriteLine($'Your weight on {p} is {p.SurfaceWeight(mass)}');              Console.ReadKey();         }     }      public class Planet     {         public static readonly Planet MERCURY = new Planet('Mercury', 3.303e+23, 2.4397e6);         public static readonly Planet VENUS   = new Planet('Venus', 4.869e+24, 6.0518e6);         public static readonly Planet EARTH   = new Planet('Earth', 5.976e+24, 6.37814e6);         public static readonly Planet MARS    = new Planet('Mars', 6.421e+23, 3.3972e6);         public static readonly Planet JUPITER = new Planet('Jupiter', 1.9e+27, 7.1492e7);         public static readonly Planet SATURN  = new Planet('Saturn', 5.688e+26, 6.0268e7);         public static readonly Planet URANUS  = new Planet('Uranus', 8.686e+25, 2.5559e7);         public static readonly Planet NEPTUNE = new Planet('Neptune', 1.024e+26, 2.4746e7);         public static readonly Planet PLUTO   = new Planet('Pluto', 1.27e+22, 1.137e6);          public static IEnumerable<Planet> Values         {             get             {                 yield return MERCURY;                 yield return VENUS;                 yield return EARTH;                 yield return MARS;                 yield return JUPITER;                 yield return SATURN;                 yield return URANUS;                 yield return NEPTUNE;                 yield return PLUTO;             }         }          public string Name   { get; private set; }         public double Mass   { get; private set; }         public double Radius { get; private set; }          Planet(string name, double mass, double radius) =>              (Name, Mass, Radius) = (name, mass, radius);          // Wniversal gravitational constant  (m3 kg-1 s-2)         public const double G = 6.67300E-11;         public double SurfaceGravity()            => G * mass / (radius * radius);         public double SurfaceWeight(double other) => other * SurfaceGravity();         public override string ToString()         => name;     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 114k
  • Answers 114k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I've used Greasemonkey frequently, but only in Firefox. Immediately I… May 11, 2026 at 10:14 pm
  • Editorial Team
    Editorial Team added an answer It's an out parameter, which means it must be set… May 11, 2026 at 10:14 pm
  • Editorial Team
    Editorial Team added an answer for (Component c : pane.getComponents()) { if (c instanceof JTextField)… May 11, 2026 at 10:14 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.