结构中的数组声明 [英] array declaration in structure

查看:99
本文介绍了结构中的数组声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

如何在结构中声明数组?



  public   struct  test 
{
public byte itemcode;
public byte [] itemname;
/// / 20
public byte 方法;
public string [] controlname4QC;
// 保留公共字符串;
public byte [] reserved;
public void initialize()
{
for int i = 0 ; i < 20 ; i ++)
{
reserved [i] = 0 ;
controlname4QC [i] = 0;
}

}

// test * p;
}





此代码是否正确?

解决方案

否。

数组是引用类型,而不是值类型,因此要使用它,您需要通过在堆上分配空间新的关键字。

您不这样做,因此当您尝试访问阵列时会出现空引用错误。



另外,正如Eliyahu所说,你应该使用标准构造函数而不是发明你自己的。



加上,这可能是高于 struct 的最大大小准则是16字节:请记住,每个引用将采用4或8个字节,具体取决于应用程序/操作系统的32/64位性质。



看起来你正试图匹配ac / c ++结构进行相互通信 - 如果是的话,然后你需要更仔细地思考它:引用与指针不同!


这是一个具有相同结构成员的类,这是我能做的最好的为你做。

  public   class 测试
{
public byte 项目代码{获得; set ;}
public byte [] Itemname { get ; set ;}

public byte 方法{ get ; set ; }
public string [] Controlname4QC { get ; set ; }
// 保留公共字符串;
public byte []保留{获取; set ;}
public Test()
{
Itemcode = 0 ;
Itemname = new byte [ 20 ];
方法= 0 ;
Controlname4QC = new string [ 20 ];
保留= 字节 [ 20 ];
}
}


它可以在一个带有如下构造函数的stuct中完成:



  public   struct  ProofWithStruct 
{

public ProofWithStruct( int charArraySize)
()
{
domain = new [charArraySize];
Peers = new int [ 20 ];
NumberOfPeers = 20 ;
assignValue = ' [';
}

public char [] domain;
public int []同行;
public int NumberOfPeers;
public char assignValue;
}





但请记住结构的最大16字节或者您希望遇到性能问题。


Hi!
how an array is declare in structure??

public struct test
{
    public byte itemcode;
    public byte[] itemname;
    ////20
    public  byte method;
    public string[] controlname4QC;
    //  public string reserved;
    public byte[] reserved;
    public void initialize()
    {
        for (int i = 0; i < 20; i++)
        {
            reserved[i] = 0;
            controlname4QC[i] = "0";
        }
             
    }

    //test* p;
}



Is this code correct?

解决方案

No.
An array is a reference type, not a value type, and so to use it you need to allocate the space on the heap via the new keyword.
You don't do that, so you will get null reference errors when you try to access the arrays.

In addition, as Eliyahu has said, you should use the standard constructor instead of "inventing" your own.

Plus, that is probably above the maximum size guidelines for a struct which is 16 bytes: remember that each reference will take either 4 or 8 bytes depending on the 32/64 bit nature of your application / OS.

It looks like you are trying to "match" a c / c++ structure for intercommunication - if so, then you need to think rather more carefully about it: a reference is not the same as a pointer!


This is a class with the same members of your struct, this is the best I can do for you.

public class Test
    {
        public byte Itemcode{get; set;}
        public byte[] Itemname{get; set;}

        public byte Method { get; set; }
        public string[] Controlname4QC { get; set; }
        //  public string reserved;
        public byte[] Reserved{get; set;}
        public Test()
        {
            Itemcode = 0;
            Itemname = new byte[20];
            Method = 0;
            Controlname4QC = new string[20];
            Reserved = new byte[20];
        }
    }


It can be done in a stuct with a constructor like:

public struct ProofWithStruct
    {

        public ProofWithStruct(int charArraySize)
                : this()
            {
                domain = new char[charArraySize];
                Peers = new int[20];
                NumberOfPeers = 20;
                assignValue = '[';
            }

            public char[] domain;
            public int[] Peers;
            public int NumberOfPeers;
            public char assignValue;
    }



but keep the maximum of 16 bytes for your struct in mind or you like to have performance issues.


这篇关于结构中的数组声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆