0

I have a few questions on the usage of the "New" keyword and removing multiple instances of an object.

If a subroutine in a code-behind ASP.Net statement like this is executed each time the subroutine is executed, will there be many instances of employeeDetails.DataKeyNames? This object is local to the subroutine.

employeeDetails.DataKeyNames = New String() {"EmployeeID"} 

If the answer is yes, should I use code to remove all copies of employeeDetails.DataKeyNames when the subroutine containing this code is finished? In that case please show what coding is required to remove all copies of employeeDetails.DataKeyNames hanging around.

Emad-ud-deen
  • 4,734
  • 21
  • 87
  • 152

2 Answers2

2

The object references you create in your application follow the normal scoping rules that you would expect. For example, if you create an object reference inside a method, then it will only exist until the method returns control to its caller. If you create a reference inside a For loop, then the reference is in scope only for that loop. However, that does not mean the object the reference was pointing to goes away immediately. In managed environments such as .NET, a garbage collector handles destroying the objects in memory only after an object no longer has any references pointing to it. Even then, the GC will schedule the appropriate time to handle this in the background and does not necessarily perform the garbage collection as soon as the number of references to an object reaches 0.

See this article for an introduction to the concepts of garbage collection in .NET.

There are exceptions to this rule when your application is referencing objects created using unmanaged resources, or resources that are not directly controlled by the CLR. A typical example of this might be a database connection or network socket. In these cases you may need to actually call Dispose on an object in order to ensure that its resources are freed.

See this question for a nice explanation of why and when to use IDisposable.

Community
  • 1
  • 1
mclark1129
  • 7,532
  • 5
  • 48
  • 84
1

The New in this case will replace whatever exists in the .DataKeyNames property with the new value. In this case, you aren't adding more values to that property, but will see a performance penalty of repeatedly creating and destroying the string arrays. It is better to check to see if the value was set and then not replace it with a simple if clause:

If employeeDetails.DataKeyNames Is Nothing Then
  employeeDetails.DataKeyNames = New String() {"EmployeeID"}
End If

That being said, you may want to check the other logic in your code to determine why you are calling into this method repeatedly and see if there are ways of reducing those calls.

Jim Wooley
  • 10,169
  • 1
  • 25
  • 43
  • Hi Everyone, Thanks for the detailed responses. They are very helpful. I will use the if statement as described. I also plan to stick a message box in there to see if employeeDetails.DataKeyNames Is Nothing each time the subroutine is called. The purpose of the subroutine is to display details after the user selects a summary row of data then the subroutine is called after the user clicks a button. – Emad-ud-deen Sep 01 '12 at 00:31