C#中的枚举数据类型问题 [英] Problem with enum datatype in C#

查看:132
本文介绍了C#中的枚举数据类型问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下程序没有编译

Hi,
Following program is not compiling

using System;

namespace enum2
{
    class Program
{
    private const string V = "\n";

    private enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };
    static void Main(string[] args)
    {

        Console.Write(value: Days.Sun + V);
        Console.Write(value: Days.Sun + V);
        Days.Sun = (int)  10;//error here
        Console.Write(value: Days.Mon + V);
        Console.ReadKey();
    }
}
}





错误是



Error is

left hand-side of an assignment must be variable, property or indexer





我试过的:



我试图施放值



What I have tried:

I have tried to cast the value

推荐答案

Days.Sun,for你的目的,是一个不变;枚举值;它只能出现在右侧。
Days.Sun, for your purposes, is a "constant"; an enum value; which can only appear on the RIGHT side.


Days.Sun =(int)10; //这里错误

想想你在这里想做什么......



天是枚举,太阳是枚举值之一。完全令人困惑......



你喜欢mabye做这样的事吗?



Days.Sun = (int) 10;//error here
Think about what you are trying to do here...

"Days" is the enum and "Sun" is one of the enumeration values... Completly confusing...

Do you like mabye to do something like this?

int day= (int)Days.Sun





无论如何你对枚举值'10'有什么期望?在0..6范围内?



好​​的,另一方面,如果你喜欢/需要为你的枚举建立一个特定的关系,你可以做这样的事情





Anyway what do you expect from the value '10' from an enum in the range 0..6?

Ok, on the other hand in case you like/need to have a specific relation for your enum you can do something like this

enum Days { Sun= 4, Mon= 5, Tue= 6, Wed= 7, Thu= 8, Fri=9, Sat=10 };


这篇关于C#中的枚举数据类型问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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