Marko Apfel - Afghanistan/Belgium/Germany

Management, Architecture, Programming, QA, Coach, GIS, EAI

  Home  |   Contact  |   Syndication    |   Login
  187 Posts | 2 Stories | 201 Comments | 4 Trackbacks

News



Twitter | LinkedIn | Xing

Article Categories

Archives

BizTalk

C#

Enterprise Library

SAP

SQL Server

Technologie

Motivation

Often you want to have the possibility to call a special abstract or virtual Initialize-method in a constructor of a base class to set individual initializing things in an override of this Initialize-method in a derived class.

This is similar to the GOF-Pattern “Template Method”. But with C# this ends up with a warning “Virtual member call in constructor”. Why we get this warning?

To show the effect of this approach, lets have a look at the source and the calling graph during runtime:

Consequence

You will see, that the constructor of the derived class is called *after* the call of the Initialize-method of this class. So you should be carefully if you set up things in the derived constructor and use later in the Initialize-method. Samples of such information could be identity or logging stuff.

But if you don’t have a correlation between the constructor and the Initialize-method you could use it.

posted on Saturday, October 15, 2011 2:18 PM