• <bdo id='55ydr'></bdo><ul id='55ydr'></ul>
    <i id='55ydr'><tr id='55ydr'><dt id='55ydr'><q id='55ydr'><span id='55ydr'><b id='55ydr'><form id='55ydr'><ins id='55ydr'></ins><ul id='55ydr'></ul><sub id='55ydr'></sub></form><legend id='55ydr'></legend><bdo id='55ydr'><pre id='55ydr'><center id='55ydr'></center></pre></bdo></b><th id='55ydr'></th></span></q></dt></tr></i><div id='55ydr'><tfoot id='55ydr'></tfoot><dl id='55ydr'><fieldset id='55ydr'></fieldset></dl></div>
    1. <tfoot id='55ydr'></tfoot>

      <small id='55ydr'></small><noframes id='55ydr'>

        <legend id='55ydr'><style id='55ydr'><dir id='55ydr'><q id='55ydr'></q></dir></style></legend>

      1. 将字节数组初始化为某个值,而不是默认的null?

        Initialize a byte array to a certain value, other than the default null?(将字节数组初始化为某个值,而不是默认的null?)

            <bdo id='cz7ES'></bdo><ul id='cz7ES'></ul>
          • <tfoot id='cz7ES'></tfoot>
          • <i id='cz7ES'><tr id='cz7ES'><dt id='cz7ES'><q id='cz7ES'><span id='cz7ES'><b id='cz7ES'><form id='cz7ES'><ins id='cz7ES'></ins><ul id='cz7ES'></ul><sub id='cz7ES'></sub></form><legend id='cz7ES'></legend><bdo id='cz7ES'><pre id='cz7ES'><center id='cz7ES'></center></pre></bdo></b><th id='cz7ES'></th></span></q></dt></tr></i><div id='cz7ES'><tfoot id='cz7ES'></tfoot><dl id='cz7ES'><fieldset id='cz7ES'></fieldset></dl></div>
              <tbody id='cz7ES'></tbody>
            <legend id='cz7ES'><style id='cz7ES'><dir id='cz7ES'><q id='cz7ES'></q></dir></style></legend>

                  <small id='cz7ES'></small><noframes id='cz7ES'>



                  我正忙着将一个用 C++ 完成的旧项目改写为 C#.

                  I'm busy rewriting an old project that was done in C++, to C#.


                  My task is to rewrite the program so that it functions as close to the original as possible.


                  During a bunch of file-handling the previous developer who wrote this program creates a structure containing a ton of fields that correspond to the set format that a file has to be written in, so all that work is already done for me.

                  这些字段都是字节数组.然后 C++ 代码所做的是使用 memset 将整个结构设置为所有空格字符 (0x20).一行代码.很简单.

                  These fields are all byte arrays. What the C++ code then does is use memset to set this entire structure to all spaces characters (0x20). One line of code. Easy.

                  这是非常重要的,因为该文件最终进入的实用程序需要这种格式的文件.我必须做的是将此结构更改为 C# 中的一个类,但我找不到一种方法可以轻松地将这些字节数组中的每一个初始化为所有空格字符.

                  This is very important as the utility that this file eventually goes to is expecting the file in this format. What I've had to do is change this struct to a class in C#, but I cannot find a way to easily initialize each of these byte arrays to all space characters.


                  What I've ended up having to do is this in the class constructor:

                  //Initialize all of the variables to spaces.
                  int index = 0;
                  foreach (byte b in UserCode)
                      UserCode[index] = 0x20;

                  这很好用,但我确信必须有更简单的方法来做到这一点.当数组在构造函数中设置为 UserCode = new byte[6] 时,字节数组会自动初始化为默认的空值.有没有办法让它在声明时变成所有空格,这样当我调用我的类的构造函数时,它会像这样立即初始化?还是一些类似 memset 的函数?

                  This works fine, but I'm sure there must be a simpler way to do this. When the array is set to UserCode = new byte[6] in the constructor the byte array gets automatically initialized to the default null values. Is there no way that I can make it become all spaces upon declaration, so that when I call my class' constructor that it is initialized straight away like this? Or some memset-like function?



                  For small arrays use array initialisation syntax:

                  var sevenItems = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 };

                  对于较大的数组,使用标准的 for 循环.这是最易读和最有效的方法:

                  For larger arrays use a standard for loop. This is the most readable and efficient way to do it:

                  var sevenThousandItems = new byte[7000];
                  for (int i = 0; i < sevenThousandItems.Length; i++)
                      sevenThousandItems[i] = 0x20;


                  Of course, if you need to do this a lot then you could create a helper method to help keep your code concise:

                  byte[] sevenItems = CreateSpecialByteArray(7);
                  byte[] sevenThousandItems = CreateSpecialByteArray(7000);
                  // ...
                  public static byte[] CreateSpecialByteArray(int length)
                      var arr = new byte[length];
                      for (int i = 0; i < arr.Length; i++)
                          arr[i] = 0x20;
                      return arr;




                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)
                  • <legend id='r3cuL'><style id='r3cuL'><dir id='r3cuL'><q id='r3cuL'></q></dir></style></legend>
                    <tfoot id='r3cuL'></tfoot>
                      <tbody id='r3cuL'></tbody>
                    • <bdo id='r3cuL'></bdo><ul id='r3cuL'></ul>

                        <small id='r3cuL'></small><noframes id='r3cuL'>

                            <i id='r3cuL'><tr id='r3cuL'><dt id='r3cuL'><q id='r3cuL'><span id='r3cuL'><b id='r3cuL'><form id='r3cuL'><ins id='r3cuL'></ins><ul id='r3cuL'></ul><sub id='r3cuL'></sub></form><legend id='r3cuL'></legend><bdo id='r3cuL'><pre id='r3cuL'><center id='r3cuL'></center></pre></bdo></b><th id='r3cuL'></th></span></q></dt></tr></i><div id='r3cuL'><tfoot id='r3cuL'></tfoot><dl id='r3cuL'><fieldset id='r3cuL'></fieldset></dl></div>