开发者

C# Enums returning wrong int value

开发者 https://www.devze.com 2023-04-12 09:50 出处:网络
public enum Question { A= 02, 开发者_运维问答 B= 13, C= 04, D= 15, E= 06, } but when i use int something = (int)Question.A;
public enum Question
{
    A= 02,
   开发者_运维问答 B= 13,
    C= 04,
    D= 15,
    E= 06,
}

but when i use

int something = (int)Question.A;

i get 2 instead of 02. How can i get 02?


Integers are not strings. It sounds like you want to format a string with at least two digits, which you can do with:

string text = value.ToString("00"); // For example; there are other ways

It's important to distinguish between the text representation of a value, and the information which is actually stored in a value.

For example, consider:

int base10 = 255;
int base16 = 0xff;

Here both variables have the same value. They were initialized using different forms of numeric literal, but they're the same value. Both could be formatted in decimal, hex, binary, whatever you want - but the values themselves are indistinguishable.


02 is a string representation of 2 with a precending zero. If you need to output this value somewhere try to use custom string format:

String.Format("{0:00}", (int)Question.A)

or

((int)Question.A).ToString("00")

For more details regarding this format string see on MSDN "The "0" Custom Specifier"


Integers are numbers - just like with money, $1 equals $1.00 and $000001. If you want to display a number in a certain way, you can use:

string somethingReadyForDisplay = something.ToString("D2");

Which converts the number into a string containing "02".


2 is exactly the same as 02, and 002 and 0002 etc.


Check this post : Enum With String Values In C# by this you can satisfy the needs easily realated to string

You can also try this option

 public enum Test : int {
        [StringValue("02")]
        Foo = 1,
        [StringValue("14")]
        Something = 2       
 } 

new custom attribute class, the source is below:

/// <summary>
/// This attribute is used to represent a string value
/// for a value in an enum.
/// </summary>
public class StringValueAttribute : Attribute {

    #region Properties

    /// <summary>
    /// Holds the stringvalue for a value in an enum.
    /// </summary>
    public string StringValue { get; protected set; }

    #endregion

    #region Constructor

    /// <summary>
    /// Constructor used to init a StringValue Attribute
    /// </summary>
    /// <param name="value"></param>
    public StringValueAttribute(string value) {
        this.StringValue = value;
    }

    #endregion

}

created a new Extension Method which I will use to get the string value for an enums value:

    /// <summary>
    /// Will get the string value for a given enums value, this will
    /// only work if you assign the StringValue attribute to
    /// the items in your enum.
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static string GetStringValue(this Enum value) {
        // Get the type
        Type type = value.GetType();

        // Get fieldinfo for this type
        FieldInfo fieldInfo = type.GetField(value.ToString());

        // Get the stringvalue attributes
        StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes(
            typeof(StringValueAttribute), false) as StringValueAttribute[];

        // Return the first if there was a match.
        return attribs.Length > 0 ? attribs[0].StringValue : null;
    }

finally read value by

Test t = Test.Foo;
string val = t.GetStringValue();

- or even -

string val = Test.Foo.GetStringValue();


02( the index ) stored as integer so you can't get it as 02 !!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号