C#枚举vs常量vs #defines [英] C# enums vs Constnats vs #defines

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

问题描述

大家好,

暴露了我对OO编程的某些细节的无知.我来自硬件世界(Assembler,C!),却错过了纯PC世界中对某些事物的需求.我一直使用#define进行定义,我在某些C ++中看到了const,并且可以很清楚地看到其中的区别,任何人都可以解释或指出一些参考. eNums似乎只是将常量分组在一起的一种方法?....

Hi All,

Betraying my ignorance of some of the finer points OO programming. I come from the Hardware World (Assembler, C!) and have missed the need for certain things in the purely PC world. I have always used #define for definitions I have seen the const in some C++ and could quite see the difference can any one either explain or point me to some reference. eNums just seem to be a way of grouping constants together?....

推荐答案

常量将引用一个永不更改的值,您可能需要引用该值通常在程序中.例如:

< lang pre ="cs">
const long SpeedOfLight = 299792458;


枚举几乎是常数,只是更整洁.它还可以执行各种列表.假设您有一种可以治愈疾病的方法,它看起来可能像这样:

A constant will refer to a value that never changes, that you may need to reference often in a program. For example:

<lang pre="cs">
const long SpeedOfLight= 299792458;


Enums are pretty much constants, just neater. It also acts a list of sorts. Say you have a method that returns the cure to a disease, it can look like this:

public enum Disease { TB, AIDS, Cancer, Flu }

public string Cure(Disease d)
{
      switch(d)
      {
          case Disease.TB:
               return "Blah";
               break;
        ......
       }
}        



那几乎是我所能做的最好的.我建议使用Google搜索来获得更深入的答案.

-Dom



Thats pretty much the best I can do. I would recommend a Google search to get a more in depth answer.

-Dom


这篇关于C#枚举vs常量vs #defines的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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