Reference and allocate a new memory is diffrent
Posted on 2006-01-11 22:02 天生舞男 阅读(206) 评论(0) 编辑 收藏 举报In C#,
1.Test objTest
It is not allocate memory ,It is just a statement.
2.Test objExistedTest = new Test();
Test objTest = objExistedTest;
objTest will not allocate memory,It is just pointer the objExistedTest address.
3.If you want allocate a new memory,you can use copy constructor in Test class.
private void Page_Load(object sender, System.EventArgs e)
{
SortedList slNewSortedList = CreateSortedList();
SortedList slCopySortedList = new SortedList();
foreach(string strCopySortedListKey in slNewSortedList.Keys)
{
//The slCopySortedList can allocate a new memory.
slCopySortedList.Add(strCopySortedListKey,slNewSortedList[strCopySortedListKey]);
}
}
public SortedList CreateSortedList()
{
SortedList mySL = new SortedList();
mySL.Add( "1", "The" );
mySL.Add( "2", "quick" );
mySL.Add( "3", "brown" );
mySL.Add( "4", "fox" );
mySL.Add( "5", "jumped" );
mySL.Add( "6", "over" );
mySL.Add( "7", "the" );
mySL.Add( "8", "lazy" );
mySL.Add( "9", "dog" );
return mySL;
}