Click here to Skip to main content
15,891,607 members
Home / Discussions / C#
   

C#

 
GeneralDirectX and System.Math Pin
Ark26-Oct-03 2:56
Ark26-Oct-03 2:56 
Generalstatic constructors Pin
R. Thomas26-Oct-03 0:55
R. Thomas26-Oct-03 0:55 
GeneralRe: static constructors Pin
Blake Coverett26-Oct-03 2:10
Blake Coverett26-Oct-03 2:10 
GeneralRe: static constructors Pin
R. Thomas26-Oct-03 2:24
R. Thomas26-Oct-03 2:24 
GeneralRe: static constructors Pin
Blake Coverett26-Oct-03 2:39
Blake Coverett26-Oct-03 2:39 
GeneralRe: static constructors Pin
R. Thomas26-Oct-03 3:46
R. Thomas26-Oct-03 3:46 
GeneralRe: static constructors Pin
Blake Coverett26-Oct-03 4:09
Blake Coverett26-Oct-03 4:09 
GeneralRe: static constructors Pin
Heath Stewart26-Oct-03 5:58
protectorHeath Stewart26-Oct-03 5:58 
Blake is right, it all depends. The important thing to keep in mind is that one field is Type-based (the static field), and one field is instance-based. That static field value will be the same for all instances of the class. That instance field value will be different for each instance.

The ubiquitous example is the Company and Employees example. While the Company class may keep a count of Employee objects added or removed from it, another way to solve this is to have the Employee Type keep track of it:
public class Employee
{
  private static int count = 0;
  public Employee()
  {
    count++;
  }
  ~Employee()
  {
    count--;
  }
}
While this is a very basic and impractical solution (because it doesn't allow for multiple companies to have different employees), it is just an example.

I use this a lot to initialize caches for objects in the same context, such as a download manager that keeps a cache of files. Different instances of that download manager (actually, it's a singleton now, but the following still applies...) share the same cache, just like different instances of Internet Explorer (actually, anything that uses URL monikers or the like) share the same cache on the filesystem (though it's not "initialized" per se). I do this in the static constructor to make sure that it is initialized before the class.

So, basically, the order for instantiating of two objects is done like so:
  1. Static constructor (IL: .cctor)
  2. Instance constructor (IL: .ctor)
  3. Instance constructor (IL: .ctor)
  4. Destructor (IL: Finalize)
  5. Destructor (IL: Finalize)
Note, there is no "static destructor". Even if all instances of the Type have been disposed and garbage collected, the static field still retains it's value until the Type is unloaded (which is done when its assembly is unloaded, which is done when the AppDomain that assembly is bound to unloads).

 

-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
Generalstack and heap Pin
R. Thomas26-Oct-03 0:49
R. Thomas26-Oct-03 0:49 
GeneralRe: stack and heap Pin
Blake Coverett26-Oct-03 2:27
Blake Coverett26-Oct-03 2:27 
GeneralRe: stack and heap Pin
R. Thomas26-Oct-03 2:38
R. Thomas26-Oct-03 2:38 
GeneralRe: stack and heap Pin
Blake Coverett26-Oct-03 3:31
Blake Coverett26-Oct-03 3:31 
GeneralRe: stack and heap Pin
R. Thomas26-Oct-03 3:44
R. Thomas26-Oct-03 3:44 
GeneralRe: stack and heap Pin
Blake Coverett26-Oct-03 3:55
Blake Coverett26-Oct-03 3:55 
Generalremainder Pin
R. Thomas26-Oct-03 0:40
R. Thomas26-Oct-03 0:40 
GeneralRe: remainder Pin
leppie26-Oct-03 0:51
leppie26-Oct-03 0:51 
GeneralRe: remainder Pin
R. Thomas26-Oct-03 0:53
R. Thomas26-Oct-03 0:53 
GeneralRe: remainder Pin
Rakesh Rajan26-Oct-03 1:08
Rakesh Rajan26-Oct-03 1:08 
GeneralRe: remainder Pin
Daniel M. Edwards26-Oct-03 1:11
Daniel M. Edwards26-Oct-03 1:11 
GeneralRe: remainder Pin
R. Thomas26-Oct-03 1:16
R. Thomas26-Oct-03 1:16 
GeneralRe: remainder Pin
Daniel M. Edwards26-Oct-03 1:17
Daniel M. Edwards26-Oct-03 1:17 
GeneralType.GetMembers() Pin
Arun Bhalla25-Oct-03 15:02
Arun Bhalla25-Oct-03 15:02 
GeneralRe: Type.GetMembers() Pin
Heath Stewart26-Oct-03 6:06
protectorHeath Stewart26-Oct-03 6:06 
GeneralRe: Type.GetMembers() Pin
Arun Bhalla26-Oct-03 8:40
Arun Bhalla26-Oct-03 8:40 
GeneralRe: Type.GetMembers() Pin
Arun Bhalla27-Oct-03 12:01
Arun Bhalla27-Oct-03 12:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.