Instance Constructor vs. The Class Constructor
http://www.csharpfriends.com
World's Greatest C# Community    
Home Articles C# Forums Books C# Syntax C# Spec C# Jobs free Source Code Advertise About
 

Control Panel

[ Sign In / register ]
Points   
Notes 
My Forums
My Tutorials
My Profile

Resources

Learn
 Articles
 QuickStarts
 C# Spec
 Whitepapers
 Tools
 Class Browser
 C# Code Generator
 Links
 Misc Rss Feeds
 Code Highlight
 411 Directory
 FREE magazines
 freevb.net

Reviews
  ASP.NET Hosting

Source Code
 Get Version 1.0



C# Consulting
AspDotNetStoreFront
Chapter:   UnCategorized
Current Lesson:
Instance Constructor vs. The Class Constructor
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  Download And Upload A File Using C# [next Lesson]  Simple Calculator in C#
Instance Constructor vs. The Class Constructor
  by: Govinda

Instance Constructor vs. The Class Constructor

by: Govinda

The Common Language Runtime Object Model provides two special methods, the instance constructor and the class constructor. This class constructor method is also known as type constructor or type initializer.

Instance constructor is executed when a new instance of type is created and the class constructor is executed after the type is loaded and before any one of the type members is accessed. We have a perfect setup for field initialization since the class constructors are static and can deal with static members of the type only and instance constructors take care of instance members only.

Class constructor, or type initializer, is the method specific to a type as a whole that run after the type is loaded and before any of the type's members are accessed. Class constructors are used for static field initialization. Only one class constructor per type is permitted, and it cannot use the vararg (variable argument) calling convention. Normally, class constructors are never called from the IL code. If a type has a class constructor, this constructor is executed automatically after the type is loaded. However, a class constructor, like any other static method, can be called explicitly. As a result of such a call, the global fields of the type are reset to their initial values. Calling class constructor explicitly does not lead to type reloading.

Instance constructors, unlike class constructors, are specific to an instance of a type and are used to initialize both static and instance fields of the type. Functionally, instance constructors in IL are a direct analog of C++ constructors. Instance constructors can have parameters but must return void. Like class constructors, instance constructors cannot use the vararg calling convention. In the current release of the common language runtime, instance constructors are not allowed to be a virtual type. A type can have multiple instance constructors, but they must have different parameter lists because the name and the return type (void) are fixed. Usually, instance constructors are called during a new type instance is created. An instance constructor can be called explicitly and calling it resets the fields of the type instance and does not create a new instance. You should be aware of explicitly calling constructors especially when you have reference type members; this is just to avoid multiple type instantiations. Calling the class and instance constructors explicitly is possible, but it makes the code unverifiable. This limitation is imposed on the constructors of reference types (classes) only and does not concern those of the value types. But wait, the instance constructor can explicitly called within the instance of constructor of the class's direct descendent or child.

Example:
using System;
class SomeClass {

private Int32 member1;
private static Int32 member2;

//Type constructor
static SomeClass(){}

//Instance constructor
public SomeClass(){} //default instance Constructor
public SomeClass(Int32 memberval){ member1 = memberval;}

//Other class operations
...

}

1 


Build Your Own ASP.NET Website Using C# & VB.NET

Chapter:  UnCategorized
Current Lesson:
Instance Constructor vs. The Class Constructor
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  Download And Upload A File Using C# [next Lesson]  Simple Calculator in C#



Today's Top Movers
vulpes 6800
MadHatter 2220
jal 867
Jeff1203 857
muster 791

Yesterday Top Movers
shakti sin.. 9
MadHatter 3
C#fanatic 2
Al_Pennywo.. 2
snaso 1



Monthly Leaders
vulpes 6800
MadHatter 2260
jal 867
Jeff1203 857
muster 791

Top Members
mosessaur 18457
Rincewind 7074
stanleytan 6995
vulpes 6800
Gsuttie 6046

Great Offers
.net hosting
Go To My Pc
Remote Pc Control
zonealarm
spam blocker
web hosting directory
ad server   C#
snadtech GoToMyPc

Top of Page

Advertise | About | Link To Us | Privacy Notice Copyright © 2003 - 2005 CSharpFriends.com  All Rights Reserved  Visual C# Developer Center