C# 声明一个长常量字节数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15560007/
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-10 17:10:19  来源:igfitidea点击:

Declaring a long constant byte array

c#arraysconst

提问by ahmd0

I have a long byte array that I need to declare in my C# code. I do something like this:

我有一个需要在 C# 代码中声明的长字节数组。我做这样的事情:

public static class Definitions
{
    public const byte[] gLongByteArray = new byte[] { 
        1, 2, 3,
        //and so on
    };
}

But I get an error that the const array may be initialized only with nulls.

但是我收到一个错误,即 const 数组只能用空值初始化。

If I change constto staticit compiles, but the question I have is this -- when I declare it as public static byte[] gLongByteArrayit won't be initialized every time my app loads, right? In that case the gLongByteArrayvariable will simply point to an array that is defined in the compiled exe/dll file that loads into memory. The reason I'm asking is because this array is pretty long and I don't want my program to waste CPU cycles on loading it every time the app starts, or worse, this class is referenced...

如果我改变conststatic它编译,但我的问题是这样的-当我宣布它作为public static byte[] gLongByteArray它不会每次我的应用程序加载时初始化,对不对?在这种情况下,该gLongByteArray变量将简单地指向在加载到内存中的已编译 exe/dll 文件中定义的数组。我问的原因是因为这个数组很长,我不希望我的程序在每次应用程序启动时都浪费 CPU 周期来加载它,或者更糟的是,引用了这个类......

采纳答案by Daniel A.A. Pelsmaeker

Compile-time constants(those declared with the constkeyword) are severely restricted. No code must be executed to get such a constant, or otherwise it could not be a compile-time constant. constconstants are staticby default.

编译时常量(那些用const关键字声明的)受到严格限制。不需要执行任何代码来获得这样的常量,否则它就不是编译时常量。const常量是static默认的。

If you want to create a constant and you cannot use a compile-time constant, you can use static readonlyinstead:

如果要创建常量并且不能使用编译时常量,则可以static readonly改用:

public static readonly byte[] longByteArray = new byte[] { 1, 2, 3, 4, 5 };

The statickeyword ensures it is initialized only once, and part of the declaring type (and not each instance). The readonlykeyword ensures the longByteArrayvariablecannot be changed afterwards.

static关键字确保它被初始化仅仅一次,并声明类型的一部分(而不是每个实例)。的readonly关键字确保longByteArray变量之后不能改变。

Definitions.longByteArray = new byte[] { 4, 5, 6 };   // Not possible.

Warning: An array is mutable, so in the above code I canstill do this:

警告:数组是可变的,所以在上面的代码中我仍然可以这样做:

Definitions.longByteArray[3] = 82;                    // Allowed.

To prevent that, make the type not an array but a read-only collection interface, such as IEnumerable<T>or IReadOnlyList<T>, or even better a read-only collection type such as ReadOnlyCollection<T>which doesn't even allow modification through casting.

为了防止这种情况,使类型不是数组而是只读集合接口,例如IEnumerable<T>or IReadOnlyList<T>,或者甚至更好的只读集合类型,例如ReadOnlyCollection<T>它甚至不允许通过转换进行修改。

public static readonly IReadOnlyList<byte> longByteArray = new byte[] { 1, 2, 3, 4, 5 };

回答by p.s.w.g

You can't make a constarray. According to the documentation:

你不能做一个const数组。根据文档

User-defined types, including classes, structs, and arrays, cannot be const.

用户定义的类型,包括类、结构体和数组,不能是const.

You'd have to declare it as a static readonly field like this

您必须像这样将其声明为静态只读字段

public static class Definitions
{
    public static readonly byte[] gLongByteArray = new byte[] { 
        1, 2, 3,
        //and so on
    };
}

Of course, there's nothing to stop someone from overwritting your array elements at run-time, like this:

当然,没有什么可以阻止某人在运行时覆盖您的数组元素,如下所示:

Definitions.gLongByteArray[0] = 0xFF; 

You'd have to use one of the built in collections that @Virtlinksuggests or create your own custom readonly array class to prevent that (example).

您必须使用@Virtlink建议的内置集合之一或创建您自己的自定义只读数组类来防止这种情况(示例)。

回答by rein

static classes will be initialized when you first load the application. Don't worry about the performance unless you've specifically measured that it's a problem.

当您第一次加载应用程序时,静态类将被初始化。不要担心性能,除非您已经特别测量它是一个问题。

回答by IlPADlI

Write all content to a file and embed as resource!

将所有内容写入文件并作为资源嵌入!