Changes

Jump to: navigation, search

C Sharp Object Oriented Programming

892 bytes added, 17:34, 18 January 2008
C# Constructors and Finalizers
private int accountNumber;
// Constructor
public BankAccount(string acctName, int acctNumber)
{
<pre>
BankAccount custAccount = new BankAccount("Fred Wilson", 123456);
</pre>
 
Finalizers are used to clean up any resources used by a class object when the object is destroyed. Unlike constructors which can be triggered from code using the ''new''' keyword there is no way to explicitly call a finalizer (for example there is no ''delete''' equivalent to the ''new'' keyword. Instead, the finalizer will be called when the ''garbage collector'' decides that the object instance is no longer needed. All the programmer can be sure of is that the finalizer will be called at some point between the when the object is no longer needed by the code and the point that the application terminates.
 
Finalizers are defined in the same way as constructors with the exeception that the name is receded by a tilde (~):
 
<pre>
// Finalizer
public ~BankAccount(string acctName, int acctNumber)
{
// Code to perform clean up
}
</pre>

Navigation menu