C#.NET中的枚举的目的是什么? [英] What is the purpose of enum in C# .NET?

查看:103
本文介绍了C#.NET中的枚举的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请给我这个问题的答案.

Please give me answer for this question.

推荐答案

^ ]是MSDN关于enum的说法是c#.

何时使用枚举 [
This [^]is what MSDN says about enum is c#.

When to use Enumerations[^]

According to this article:
''The benefits of using enumerations include:

Reduces errors caused by transposing or mistyping numbers.

Makes it easy to change values in the future.

Makes code easier to read, which means it is less likely that errors will creep into it.

Ensures forward compatibility. With enumerations, your code is less likely to fail if in the future someone changes the values corresponding to the member names.''


在此处查找:

http://www.csharp-station.com/Tutorials/Lesson17.aspx [ ^ ]
Look it up here:

http://www.csharp-station.com/Tutorials/Lesson17.aspx[^]


请原谅如果我的答案有点题外话,而且对您来说太先进了,请问我.我仍然认为这可能非常有用.

我想参考我的CodeProject文章,该文章显示了许多开发人员都不知道的一些好的简单枚举技术.但更有趣的是主要话题.我可能会感到惊讶,但是C#(以及C/C ++)枚举类型不是某些其他语言中的真实"枚举类型!

原因非常令人信服:它们不枚举!用.NET术语表示这意味着它们没有实现使用foraech枚举所需的接口IEnumerable.请注意,其他一些语言也固有地执行此操作.

本文概述了解决方法,但主要结果是围绕任何现有类型构建了枚举器.此外,它还可以防止与主要主题相关的一些常见谬论.

请参阅:枚举类型不枚举!解决.NET和语言限制 [ ^ ].

祝你好运,
—SA
Please forgive me if my Answer is a bit off-topic and many a bit too advanced to you. I still think it could be very useful.

I want to reference my CodeProject article which shows some good and simple enumeration techniques many developers do not know. But more intriguing is the main topic. I may come as surprise, but C# (as well as C/C++) enumeration types are not "real" enumeration types as in some other languages!

The reason is very convincing: they do not enumerate! In .NET terms it means they do not implement the interface IEnumerable needed to enumerate using foraech. Note that some other languages do it inherently.

The article overview the ways to work around, but the main result is the enumerator build around any existing type. Also, it protects from some common fallacies related to main topic.

Please see: Enumeration Types do not Enumerate! Working around .NET and Language Limitations[^].

Good luck,
—SA


这篇关于C#.NET中的枚举的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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