Instance Constructors & Type Constructors In C#

In this article i’ll explain what’s instance constructor and what’s type constructor, and how each of them works.

Of course everybody reading this article is already a developer or at least have a slight knowledge of OOP concepts so i assume all of you know already what a constructor is but many of you have confusion and always asking (What’s the difference between instance constructor and type constructor aren’t they the same??) the answer is simply No!!

Instance Constructors

Is a special method allow an instance of a type to be initialized to a good state. This means when you create an instance of a type memory allocated for the instance’s data fields and the object’s overhead fields (Described below) and initialize them, then the instance constructor is called to set the initial state of the object (i.e., set the instance’s fields values either to their default values according to their type or to values you have explicitly wrote in the constructor body).

Object’s Overhead Fields:

1 – Type Object Pointer : Of course a type is itself an Object. Confusing right! No it’s not, think for a moment if you defines the type Animal how do you expect the runtime to know about Animal and also how you call GetType() method to check any object’s type without having any problems and how polymorphism works!! Now it’s clear i think that every type is an object itself which is created on the heap at the first time you are attempting to access the type or any of it’s static fields or methods. The static fields are allocated within the Type Object as well as a method table which contains one entry for each method defined within the type as well the two overhead members like any other object but the Type Object Pointer which contained in the Type Object is pointing to itself, But any instance created of this type it’s Type Object Pointer will be pointing to the one in the Type Object ( Check Figure 1 ).

2 – Sync Block Index: Contains an integer index into the array of sync blocks.

Fig1-CodeFig1
  So What’s Instance Constructor Characteristics:

  • Each class (Reference Type) must have at least one Constructor (If you didn’t define a constructor inside your class a parameterless constructor is automatically added which implementation simply call the base class constructor).
  • A struct (Value Type) may have a constructor if you explicitly add one if you didn’t no parameterless constructor is implicitly added.
  • If you defined an Instance Constructor in a value type you have to assign a value for every field of the struct within the constructor otherwise a compilation-error occurs.
  • Instance Constructors can’t be inherited.
  • You can have several constructors defined within your class but with different parameters list.
  • Instance Constructor is called using ‘new’ keyword every time you want to instantiate an object of the type.

Type Constructors:

some people call it (Static Constructor) so what’s it? Simply lnstance Constructor is used to set the initial state of an instance fields, a static constructor is used to set the initial state of static fields. Does that means i can set an initial values for my static fields in that constructor (Yes) and how should i invoke it? Do i use something like the new operator which i’m using to call Instance Constructors? (No, you can’t call a type constructor it’s got called automatically for you when first accessing the class or any of it’s static fields) Interesting so what’s the Type Constructor Characteristics:

  • A Type can have a static constructor only if you explicitly added it or it will be added implicitly in only one case (If you have a static field and assigned value to it via in-line initialization. Check Person class in Figure-1).
  • A Type can’t have more than one static constructor.
  • Type Constructor must be parameterless.
  • You can’t specify an access modifier (public/private/protected/..etc) to the Type Constructor specifying one will result Compilation-Error. In fact the Type Constructor have implicit private accessibility preventing developers from calling it.
  • Have to be marked as ‘static’.
  • Type Constructor Invoked only one time during the application lifetime which is when your first attempt to access the class or any of it’s static fields.

Finally, Hope you found this article useful and that you are now aware of the difference between Instance & Type Constructors. If you have any questions regarding this topic i’ll be welcomed to answer it just leave a comment and i’ll answer it as soon i’m available. Thanks a lot ^^

Advertisements

3 thoughts on “Instance Constructors & Type Constructors In C#

  1. Good job Ibrahim!

    I didn’t know you were writing blogs now. Hit me up… I can help you get your blog promoted..

    I have a network of around 500 sites, article directories, forums, blogs, etc..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s