有如下例子:
using System;namespace StaticTest
{
class Employee
{
private string firstName;
private string lastName;
private static int count; //Employee objects in memory //constructor increments static Employee count
public Employee(string fName, string lName)
{
firstName = fName;
lastName = lName; ++count; Console.WriteLine("Employee object constructor: " + firstName + " " + lastName + "; count = " + Count);
} //destructor decrements static Employee count
~Employee()
{
--count; Console.WriteLine("Employee object destructor: " + firstName + " " + lastName + "; count =" + Count);
} //FirstName property
public string FirstName
{
get
{
return firstName;
}
} //LastName property
public string LastName
{
get
{
return lastName;
}
} //static Count property
public static int Count
{
get
{
return count;
}
}
}
}
using System;namespace StaticTest
{
class StaticTest
{
static void Main(string[] args)
{
Console.WriteLine("Employee before instantiation: " + Employee.Count + "\n"); //create two Employees
Employee employee1 = new Employee("Susan", "Baker");
Employee employee2=new Employee("Bob","Jones"); Console.WriteLine("\nEmployees after instantiation: Employee.Count = " + Employee.Count + "\n"); //display the Employees
Console.WriteLine("Employee 1: " + employee1.FirstName + " " + employee1.LastName + "\nEmployee 2: " + employee2.FirstName + " " + employee2.LastName + "\n"); //remove references to objects to indicate that objects can be garbage collected
employee1 = null;
employee2 = null; //force garbage collection
System.GC.Collect(); //wait until collection completes
System.GC.WaitForPendingFinalizers(); Console.WriteLine("\nEmployees after garbage collection: " + Employee.Count);
}
}
}
输出:Employee before instantiation: 0Employee object constructor: Susan Baker; count = 1
Employee object constructor: Bob Jones; count = 2Employees after instantiation: Employee.Count = 2Employee 1: Susan Baker
Employee 2: Bob JonesEmployee object destructor: Bob Jones; count =1
Employee object destructor: Susan Baker; count =0Employees after garbage collection: 0
请按任意键继续. . .
请指教其中~Employee()方法起什么作用?
using System;namespace StaticTest
{
class Employee
{
private string firstName;
private string lastName;
private static int count; //Employee objects in memory //constructor increments static Employee count
public Employee(string fName, string lName)
{
firstName = fName;
lastName = lName; ++count; Console.WriteLine("Employee object constructor: " + firstName + " " + lastName + "; count = " + Count);
} //destructor decrements static Employee count
~Employee()
{
--count; Console.WriteLine("Employee object destructor: " + firstName + " " + lastName + "; count =" + Count);
} //FirstName property
public string FirstName
{
get
{
return firstName;
}
} //LastName property
public string LastName
{
get
{
return lastName;
}
} //static Count property
public static int Count
{
get
{
return count;
}
}
}
}
using System;namespace StaticTest
{
class StaticTest
{
static void Main(string[] args)
{
Console.WriteLine("Employee before instantiation: " + Employee.Count + "\n"); //create two Employees
Employee employee1 = new Employee("Susan", "Baker");
Employee employee2=new Employee("Bob","Jones"); Console.WriteLine("\nEmployees after instantiation: Employee.Count = " + Employee.Count + "\n"); //display the Employees
Console.WriteLine("Employee 1: " + employee1.FirstName + " " + employee1.LastName + "\nEmployee 2: " + employee2.FirstName + " " + employee2.LastName + "\n"); //remove references to objects to indicate that objects can be garbage collected
employee1 = null;
employee2 = null; //force garbage collection
System.GC.Collect(); //wait until collection completes
System.GC.WaitForPendingFinalizers(); Console.WriteLine("\nEmployees after garbage collection: " + Employee.Count);
}
}
}
输出:Employee before instantiation: 0Employee object constructor: Susan Baker; count = 1
Employee object constructor: Bob Jones; count = 2Employees after instantiation: Employee.Count = 2Employee 1: Susan Baker
Employee 2: Bob JonesEmployee object destructor: Bob Jones; count =1
Employee object destructor: Susan Baker; count =0Employees after garbage collection: 0
请按任意键继续. . .
请指教其中~Employee()方法起什么作用?
大致可以理解为和 构造函数相反的操作。====CSDN 小助手 V2.5 2006年1月18日发布====
CSDN小助手是一款脱离浏览器也可以访问Csdn论坛的软件
界面:http://blog.csdn.net/Qqwwee_Com/archive/2005/11/05/523395.aspx
下载:http://szlawbook.com/csdnv2