How to define constants in Visual C# like #define in C?

后端 未结 7 899
情歌与酒
情歌与酒 2020-12-29 18:53

In C you can define constants like this

#define NUMBER 9

so that wherever NUMBER appears in the program it is replaced with 9. But Visual

7条回答
  •  长发绾君心
    2020-12-29 19:41

    In C#, per MSDN library, we have the "const" keyword that does the work of the "#define" keyword in other languages.

    "...when the compiler encounters a constant identifier in C# source code (for example, months), it substitutes the literal value directly into the intermediate language (IL) code that it produces." ( https://msdn.microsoft.com/en-us/library/ms173119.aspx )

    Initialize constants at time of declaration since there is no changing them.

    public const int cMonths = 12;
    

提交回复
热议问题