不确定类的内存分配 [英] not sure about memory allocation for class
本文介绍了不确定类的内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的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; } }
}
推荐答案
NPOINT和NSIDE的整数分配不是必需的 - 它们是结构,所以除非你需要调用构造函数,你不需要使用new
。
赋予NAME和SIDESTRING虽然是必要的,因为string
是一个引用类型,它的默认值是null
- 一个空引用。因此,如果您没有指定字符串值,那么当您尝试使用它们时,如果您使用无参数构造函数,则会获得空引用异常。
但我不会不这样做,它不是很清楚。试试这个:
The allocations of integers to NPOINT and NSIDE aren't necessary - they are structs, so unless you need to call the constructor, you don't need to usenew
.
The assignment to NAME and SIDESTRING though are necessary, becausestring
is a reference type, and it's default isnull
- an empty reference. So if you don't assign a string value then you will get null reference exceptions when you try to use them if you ever use your parameterless constructor.
But I wouldn't do it like that, it's not very clear. Try this:
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; }
这篇关于不确定类的内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文