Pregunta

I have a Java enumerator shortened to the following from the BioJava library:

public enum Element implements Serializable {

// most frequently used elements first
H(1, 1, 39, 1.10f, 0.32f, 1, 1, 1, 1, 1, 1.008f, 0, 1, 2.20f, ElementType.OTHER_NONMETAL),
C(6, 2, 0, 1.55f, 0.77f, 4, 4, 4, 4, 4, 12.011f, 2, -4, 2.55f, ElementType.OTHER_NONMETAL),
N(7, 2, 57, 1.40f, 0.75f, 5, 2, 5, 3, 4, 14.007f, 2, -3, 3.04f, ElementType.OTHER_NONMETAL),
O(8, 2, 65, 1.35f, 0.73f, 6, 1, 2, 2, 2, 16.000f, 2, -2, 3.44f, ElementType.OTHER_NONMETAL),
.....


private int atomicNumber;
private int period;
private float VDWRadius; // in Angstroms
private float covalentRadius; // in Angstroms
private int valenceElectronCount;
private int minimumValence;
private int maximumValence;
private int commonValence;
private int maximumCovalentValence;
private float atomicMass;
private int coreElectronCount;
private int oxidationState;
private float paulingElectronegativity;
private ElementType elementType;


private static final Map<String,Element> allElements ;

static {
    allElements = new HashMap<String,Element>();
    for (Element e : Element.values()){
        allElements.put(e.toString().toLowerCase(), e);
    }
}
private Element(int atomicNumber,
        int period,
        int hillOrder,
        float VDWRadius,
        float covalentRadius,
        int valenceElectronCount,
        int minimumValence,
        int maximumValence,
        int commonValence,
        int maximumCovalentValence,
        float atomicMass,
        int coreElectronCount,
        int oxidationState,
        float paulingElectronegativity,
        ElementType elementType) {

    this.atomicNumber = atomicNumber;
    this.period = period;
    //this.hillOrder = hillOrder;
    this.VDWRadius = VDWRadius;
    this.covalentRadius = covalentRadius;
    this.valenceElectronCount = valenceElectronCount;
    this.minimumValence = minimumValence;
    this.maximumValence = maximumValence;
    this.commonValence = commonValence;
    this.maximumCovalentValence = maximumCovalentValence;
    this.atomicMass = atomicMass;
    this.coreElectronCount = coreElectronCount;
    this.oxidationState = oxidationState;
    this.paulingElectronegativity = paulingElectronegativity;
    this.elementType = elementType;


}

I would like to simply port this structure to C#, where there is an enumeration of the elements with the basic information of the Element attached. How would I do this?

Preferably, the solution would be memory efficient.

¿Fue útil?

Solución

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:

public class Element {

    // most frequently used elements first
    public static readonly Element H = new Element(1, 1, 39, 1.10f, 0.32f, 1, 1, 1, 1, 1, 1.008f, 0, 1, 2.20f, ElementType.OTHER_NONMETAL);
    public static readonly Element C = new Element(6, 2, 0, 1.55f, 0.77f, 4, 4, 4, 4, 4, 12.011f, 2, -4, 2.55f, ElementType.OTHER_NONMETAL);
    public static readonly Element N = new Element(7, 2, 57, 1.40f, 0.75f, 5, 2, 5, 3, 4, 14.007f, 2, -3, 3.04f, ElementType.OTHER_NONMETAL);
    public static readonly Element O = new Element(8, 2, 65, 1.35f, 0.73f, 6, 1, 2, 2, 2, 16.000f, 2, -2, 3.44f, ElementType.OTHER_NONMETAL);



    private int atomicNumber;
    private int period;
    private float VDWRadius; // in Angstroms
    private float covalentRadius; // in Angstroms
    private int valenceElectronCount;
    private int minimumValence;
    private int maximumValence;
    private int commonValence;
    private int maximumCovalentValence;
    private float atomicMass;
    private int coreElectronCount;
    private int oxidationState;
    private float paulingElectronegativity;
    private ElementType elementType;


    private static readonly IDictionary<String,Element> allElements ;

    static Element(){
        allElements = new Dictionary<String,Element>();
        allElements.Add("h", H);
        allElements.Add("c", C);
        allElements.Add("n", N);
        allElements.Add("o", O);
    }
    private Element(int atomicNumber,
            int period,
            int hillOrder,
            float VDWRadius,
            float covalentRadius,
            int valenceElectronCount,
            int minimumValence,
            int maximumValence,
            int commonValence,
            int maximumCovalentValence,
            float atomicMass,
            int coreElectronCount,
            int oxidationState,
            float paulingElectronegativity,
            ElementType elementType) {

        this.atomicNumber = atomicNumber;
        this.period = period;
        //this.hillOrder = hillOrder;
        this.VDWRadius = VDWRadius;
        this.covalentRadius = covalentRadius;
        this.valenceElectronCount = valenceElectronCount;
        this.minimumValence = minimumValence;
        this.maximumValence = maximumValence;
        this.commonValence = commonValence;
        this.maximumCovalentValence = maximumCovalentValence;
        this.atomicMass = atomicMass;
        this.coreElectronCount = coreElectronCount;
        this.oxidationState = oxidationState;
        this.paulingElectronegativity = paulingElectronegativity;
        this.elementType = elementType;


    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top