天生舞男

我喜欢谦虚的学习各种...,希望自己能坚持一辈子,因为即使一张卫生巾也是有它的作用.
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Reference and allocate a new memory is diffrent

Posted on 2006-01-11 22:02  天生舞男  阅读(203)  评论(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;

  }