如何在 Visual C# 中定义常量,如 C 中的 #define?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1807785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:02:26  来源:igfitidea点击:

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

c#constants

提问by neuromancer

In C you can define constants like this

在 C 中,您可以像这样定义常量

#define NUMBER 9

so that wherever NUMBER appears in the program it is replaced with 9. But Visual C# doesn't do this. How is it done?

以便程序中出现 NUMBER 的任何地方都替换为 9。但 Visual C# 不这样做。它是如何完成的?

采纳答案by Jimmeh

public const int NUMBER = 9;

You'd need to put it in a class somewhere, and the usage would be ClassName.NUMBER

你需要把它放在某个地方的类中,用法是 ClassName.NUMBER

回答by Mark Byers

You can't do this in C#. Use a const intinstead.

你不能在 C# 中做到这一点。使用 aconst int代替。

回答by Vinz

Check How to: Define Constants in C#on MSDN:

在 MSDN 上查看如何:在 C# 中定义常量

In C# the #definepreprocessor directive cannot be used to define constants in the way that is typically used in C and C++.

在 C# 中,#define预处理器指令不能用于以 C 和 C++ 中通常使用的方式定义常量。

回答by Rush

static class Constants
{
    public const int MIN_LENGTH = 5;
    public const int MIN_WIDTH  = 5; 
    public const int MIN_HEIGHT = 6;
}

// elsewhere
public CBox()
{
    length = Constants.MIN_LENGTH; 
    width  = Constants.MIN_WIDTH; 
    height = Constants.MIN_HEIGHT;  
}

回答by Alireza

in c language: #define(e.g. #define counter 100)

在 c 语言中:(#define例如#define counter 100)

in assembly language: equ(e.g. counter equ 100)

汇编语言:equ(例如 counter equ 100)

in c# language: according to msdn refrence: You use #defineto define a symbol. When you use the symbol as the expression that's passed to the #ifdirective, the expression will evaluate to true, as the following example shows:

在 c# 语言中:根据 msdn 参考:您#define用来定义符号。当您使用符号作为传递给#if指令的表达式时,表达式的计算结果为 true,如以下示例所示:

# define DEBUG

# define DEBUG

The #definedirective cannot be used to declare constant values as is typically done in C and C++. Constants in C# are best defined as static members of a class or struct. If you have several such constants, consider creating a separate "Constants" class to hold them.

#define指令不能像通常在 C 和 C++ 中那样用于声明常量值。C# 中的常量最好定义为类或结构的静态成员。如果您有多个这样的常量,请考虑创建一个单独的“常量”类来保存它们。

回答by Bobby

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

在 C# 中,根据 MSDN 库,我们有“const”关键字来完成其他语言中“#define”关键字的工作。

"...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)

“...当编译器在 C# 源代码中遇到常量标识符(例如,months)时,它会将文字值直接替换为它生成的中间语言 (IL) 代码。” ( 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;

回答by Thomas M. Krystyan

What is the "Visual C#"? There is no such thing. Just C#, or .NET C# :)

什么是“Visual C#”?哪有这回事。只是 C# 或 .NET C# :)

Also, Python's convention for constants CONSTANT_NAMEis not very common in C#. We are usually using CamelCaseaccording to MSDN standards, e.g. public const string ExtractedMagicString = "vs2019";

此外,Python 的常量约定CONSTANT_NAME在 C# 中并不常见。我们通常根据 MSDN 标准使用CamelCase,例如public const string ExtractedMagicString = "vs2019";

Source: Defining constants in C#

来源:在 C# 中定义常量