本文介绍了不确定类的内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的2个课程中,我对FormLoad中的分配有疑问。

在下面的代码中,我对A行,B行,C行有疑问和行D

i需要你的评论。

Hellow in my 2 classes i have a doubt about the allocation in FormLoad.
In the code below ,i have doubt about Line A, Line B,Line C and line D
i need your fine comment.

	MyLine[] MyRealLine;//<---- Globalley declared 
    in FormLoad i wrote
	          MyRealLine = new MyLine[100];
              for (i = 0; i < 100; i++)
              {
                  MyRealLine[i] = new MyLine();
                  MyRealLine[i].NPOINT = new int(); //<----- Line A (is this code line neccery ? )
                  MyRealLine[i].NSIDE = new int();//<----- Line B   (is this code line neccery ? )
              }

              for (i = 0; i < 100; i++)
                  MyRealLine[i].PNT = new point[100];
              //
              for (i = 0; i < 100; i++)
                  for (j = 0; j < 100; j++)
                      MyRealLine[i].PNT[j] = new point();
              //--------------
              for (i = 0; i < 100; i++)
              MyRealLine[i].SIDE = new MySide[100];
              //
              for (i = 0; i < 100; i++)
                  for (j = 0; j < 100; j++)
                  {
                  MyRealLine[i].SIDE[j] = new MySide();
MyRealLine[i].SIDE[j].NAME=new string();//<----- Line C (if line A and B required  is required what about this string ? )
MyRealLine[i].SIDE[j].SIDESTRINGLINE=new string();//<----- Line D (if line A and line B required what about this string ?)
                  }


	class MyLine
    {
        private string name;
        private int npoint, nside;

        private MySide[] side = new MySide[100];
        private point[] pnt = new point[100];
        public MyLine() { }
        public MyLine(string Xname, int Xnpoint, int Xnside, MySide[] Xside, point[] Xpnt)
        {
			NAME = Xname;
            NPOINT = Xnpoint;
            NSIDE = Xnside;
            //
            SIDE = Xside;
            PNT = Xpnt;
            allocate();
        }
        //---------------------------------------------------
        private void allocate()
        {
			int i;
            for (i = 0; i < 100; i++)
            {
                SIDE[i] = new MySide();
                PNT[i] = new point();
            }
        }
        //---------------------------------------------------
        public string NAME { get { return name; } set { name = value; } }
        public int NPOINT { get { return npoint; } set { npoint = value; } }
        public int NSIDE { get { return nside; } set { nside = value; } }
        //
        public MySide[] SIDE { get { return side; } set { side = value; } }
        public point[] PNT { get { return pnt; } set { pnt = value; } }
    }

    class MySide
    {
        private string name, sidestringline;
        public MySide() { }
        public MySide(string Xname, string Xsidestringline)
        {
            NAME = Xname;
            SIDESTRINGLINE = Xsidestringline;
        }
        public string NAME { get { return name; } set { name = value; } }
        public string SIDESTRINGLINE { get { return sidestringline; } set { sidestringline = value; } }
    }

推荐答案

MyRealLine[i].SIDE[j].NAME ="";
MyRealLine[i].SIDE[j].SIDESTRINGLINE = "";



纯粹主义者会告诉你使用它:


Purists will tell you to use this instead:

MyRealLine[i].SIDE[j].NAME = string.Empty;
MyRealLine[i].SIDE[j].SIDESTRINGLINE = string.Empty;



但是MSDN文档说它们是等价的,第一个版本更清晰!



BTW:请不要使用全部大写的属性名称:有标准它描述了名称应该是什么,属性应该以大写字母开头,其余应该是CamelCase:


But the MSDN documentation says they are equivalent, and the first version is a lot clearer!

BTW: Please don't use all upper case for property names: there are Standards which describe what names should be, and properties should start with an uppercase and the rest should be CamelCase:

public string SideStringLine { get { return _SideStringLine; } set { _SideStringLine = value; } 


这篇关于不确定类的内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 00:40