在C#中,你如何完成与#define相同的事情

时间:2021-06-29 20:56:35

Coming from a C background I'm used to defining the size of the buffer in the following way:

来自C背景我习惯用以下方式定义缓冲区的大小:

#define BUFFER_SIZE 1024

uint8_t buffer[BUFFER_SIZE];

How would you do the accomplish the same thing in C#?

你会如何在C#中完成同样的事情?

Also does the all-caps K&R style fit in with normal C# Pascal/Camel case?

全罩式K&R风格是否也适合普通的C#Pascal / Camel表壳?

5 个解决方案

#1


const int BUFFER_SIZE = 1024;

Do not use "static readonly" because it creates a variable. "const" are replaced at build time and do not create variables.

不要使用“static readonly”,因为它会创建一个变量。 “const”在构建时被替换,不会创建变量。

#2


Personally, I prefer constants:

就个人而言,我更喜欢常数:

private const int BUFFER_SIZE = 1024;

Though, if it's public and you're a framework, you may want it to be a readonly to avoid client recompiles.

但是,如果它是公共的并且您是一个框架,您可能希望它是一个只读,以避免客户端重新编译。

#3


public static readonly int BUFFER_SIZE = 1024;

I prefer this over a const due to the compiler shenanigans that can happen with a const value (const is just used for replacement, so changing the value will not change it in any assembly compiled against the original).

由于编译器的恶作剧可能会发生const值(const仅用于替换,因此更改值不会在任何针对原始编译的程序集中更改它),因此我更倾向于使用const。

#4


Don't use #define.

不要使用#define。

Define a constante: private const int BUFFER_SIZE or readonly variable: private readonly int BUFFER_SIZE

定义一个constante:private const int BUFFER_SIZE或readonly变量:private readonly int BUFFER_SIZE

#5


In C#, I decided to do that this way:

在C#中,我决定这样做:

//C# replace C++ #define
struct define
{
    public const int BUFFER_SIZE = 1024;
    //public const int STAN_LIMIT = 6;
    //public const String SIEMENS_FDATE = "1990-01-01";
}

//some code
byte[] buffer = new byte[define.BUFFER_SIZE];

#1


const int BUFFER_SIZE = 1024;

Do not use "static readonly" because it creates a variable. "const" are replaced at build time and do not create variables.

不要使用“static readonly”,因为它会创建一个变量。 “const”在构建时被替换,不会创建变量。

#2


Personally, I prefer constants:

就个人而言,我更喜欢常数:

private const int BUFFER_SIZE = 1024;

Though, if it's public and you're a framework, you may want it to be a readonly to avoid client recompiles.

但是,如果它是公共的并且您是一个框架,您可能希望它是一个只读,以避免客户端重新编译。

#3


public static readonly int BUFFER_SIZE = 1024;

I prefer this over a const due to the compiler shenanigans that can happen with a const value (const is just used for replacement, so changing the value will not change it in any assembly compiled against the original).

由于编译器的恶作剧可能会发生const值(const仅用于替换,因此更改值不会在任何针对原始编译的程序集中更改它),因此我更倾向于使用const。

#4


Don't use #define.

不要使用#define。

Define a constante: private const int BUFFER_SIZE or readonly variable: private readonly int BUFFER_SIZE

定义一个constante:private const int BUFFER_SIZE或readonly变量:private readonly int BUFFER_SIZE

#5


In C#, I decided to do that this way:

在C#中,我决定这样做:

//C# replace C++ #define
struct define
{
    public const int BUFFER_SIZE = 1024;
    //public const int STAN_LIMIT = 6;
    //public const String SIEMENS_FDATE = "1990-01-01";
}

//some code
byte[] buffer = new byte[define.BUFFER_SIZE];