错误的C#枚举值结果如何? [英] Wrong C# Enum value result?

查看:161
本文介绍了错误的C#枚举值结果如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我使用以下代码:

 命名空间 wpfEmun 
{
enum Seizoen {lente,zomer,herfst,winter,autumn = herfst,spring = lente ,夏天= zomer};

public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click( object sender,EventArgs e)
{
Seizoen seizoen = Seizoen.autumn;
int seizoengetal =( int )seizoen;
MessageBox.Show( string .Format( Seizoen nr {0}是{1},seizoengetal,seizoen .ToString()));

}









如果我调试并添加一个手表到seizoen然后我看到herfst,结果是好的。

但是,在消息框中显示文本秋天。

简而言之:var seizoen包含herfst,但显示秋天,它应该是 herfst





Alex

解决方案

这是完美预期的结果。我在文章中详细描述了你观察到的现象,我还创建了一些有趣的解决方案: 枚举类型不枚举!解决.NET和语言限制



-SA


< blockquote>你应该反过来试试:现在你做 英文单词 = 荷兰语单词 ,但你应该做 荷兰语单词 = 英文单词

  enum  Seizoen {春,夏,秋,冬,lente = spring,zomer = summer,herfst = autumn}; 



首先,将英文单词添加到枚举中,然后将荷兰语单词添加到其中。


Hi,

I use the following code:

namespace wpfEmun
{
    enum Seizoen { lente , zomer, herfst, winter, autumn = herfst, spring = lente, summer = zomer };

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Seizoen seizoen = Seizoen.autumn;
            int seizoengetal = (int)seizoen;
            MessageBox.Show(string.Format("Seizoen nr {0} is {1}", seizoengetal, seizoen .ToString()));

        }





If I debug and add a watch to seizoen then I see "herfst", and the result is ok.
BUT, in the message box the text "autumn" is displayed.
So in short: var seizoen contains "herfst" but shows "autumn" and it should be "herfst".


Alex

解决方案

This is the perfectly expected result. I described the phenomenon you observed in detail in my article where I also created some interesting work-around solution: Enumeration Types do not Enumerate! Working around .NET and Language Limitations.

—SA


You should try it the other way around: now you do English word = Dutch word, but you should do Dutch word = English word:

enum Seizoen { spring, summer, autumn, winter, lente = spring, zomer = summer, herfst = autumn };


First, add the English words to the enum and then add the Dutch words to it.


这篇关于错误的C#枚举值结果如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆