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 9235569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:04:50+00:00 2026-06-18T07:04:50+00:00

I’m developing scientific software that needs access to the periodic table of elements. An

  • 0

I’m developing scientific software that needs access to the periodic table of elements. An Element comprises of a set of Isotopes which have a few readonly properties (e.g. mass, abundance, atomic number, etc.). There are over 100 elements, and when factoring in their isotopes, there are well over 1000 isotopes. To populate all these objects at run time, I currently have an XML file (Build Action: Content)* containing all the elemental data that I parse in during the static constructor of the Element class:

public class Element {

    private static readonly Dictionary<string, Element> _elements;

    static Element()
    {
        _elements = new Dictionary<string, Element>();            
        LoadElements("Resources/Elements.xml");  // 461 KB file    
    }

    static LoadElements(string resource) {
         // code for construction of elements objects and population of the 
         // _elements dictionary.
    }      

    private Element(blah) { \\ instance constructor }

}

This works, but there is a overhead in parsing in the file and I lose some flexibility in designing the Element class. The alternative is to hard-code each Isotope and Element into the static constructor. The advantage of the later is I would be able to add static readonly property for each Element (a useful feature):

 public class Element {

    private static readonly Dictionary<string, Element> _elements;

    public static readonly Element Carbon = {get; private set;}
    public static readonly Element Hydrogen = {get; private set;}

    static Element()
    {
        _elements = new Dictionary<string, Element>();  

        Carbon = AddElement("Carbon", 6);  
        Carbon.AddIsotope(12, 12.0000000, 0.9893);
        Carbon.AddIsotope(13, 13.0033548378, 0.0107);

        Hydrogen = AddElement("Hydrogen", 1);
        //Repeat this pattern for all the elements...
    }

    static Element AddElement(string name, double atomicNumber) 
    {
          Element element = new Element(name, atomicNumber);
          _elements.Add(name, element);
          return element;
    }

    private Element(string name, double atomicNumber) {
         // Not Important, just setting readonly properties
    }

    private void AddIsotope(int massNumber, double mass, double abundance) {
         // Not Important;
    }

}

However, this seems like a lot of hard-coded data to include in a class.cs file. So I am torn, on one hand it makes sense on a data management level to have the elemental data stored in an external file which is read in. But on the other hand, because all the data is really a bunch of constant/static readonly objects, this additional parsing work seems timely, unfruitful, and limits the API design. What is the correct way for creating all these objects?

*Note: the Build Action is set to Content for the case if the client wants to modify the values of the elements for whatever reason. This isn’t a necessity and could be changed to an embedded resource.

  • 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. Editorial Team
    Editorial Team
    2026-06-18T07:04:51+00:00Added an answer on June 18, 2026 at 7:04 am

    I would consider putting the values in an embedded file, but possibly having an enum of the elements. (Probably not the isotopes, but provide an easy way of specifying an isotope from the elements.)

    That way:

    • You can still have a strongly-typed API, and not rely on magic strings etc in user code
    • You can still make it easy to change the data later should you really wish to (and possibly supply a way of reading the data from an external source)
    • You probably make it easier to work with the data itself, as an XML file rather than C#

    Don’t worry about the parsing work – given that you only need to do it once, I find it hard to believe that it would be significant in terms of performance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a

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.