Initialize Objects Properly: Listing 3: C#, Don't Call Virtual Methods

This sample shows what happens when you call a virtual method from a constructor in a base class. The derived class's constructor hasn't executed, so some of the member variables in the derived class haven't been initialized. That could cause null reference exceptions in many applications.

public class BaseTrackType
{
   private static Marker staticInit = 
     new Marker("base static - initializer1");
   protected static Marker staticInit2 =
     new Marker("base static - initializer2");

   private static Marker staticCtor;

   static BaseTrackType()
   {
     staticCtor = 
       new Marker("base static - ctor");
   }

   private Marker fieldInit = 
     new Marker("base instance - initializer");
   private Marker fieldInit2 =
     new Marker("base instance - initializer2");

   private Marker ctorInit;

   public BaseTrackType()
   {
     ctorInit = new Marker("base instance - ctor");
     // Call a virtual function:
     Console.WriteLine("Contructing an object: " + this.ToString());
   }

   protected Marker CreateMarker(string s)
   {
     return new Marker(s);
   }

   public override string ToString()
   {
     return "A BaseTracktype object";
   }
}

public class DerivedTrackType : BaseTrackType
{
   private static Marker static1Derived = new 	
		  Marker("derived static - initializer");

   private static Marker staticCtorDerived;

   static DerivedTrackType()
   {
     staticCtorDerived = new Marker("derived static - Ctor");
   }

   private Marker derivedInit = new Marker("derived instance - Initializer");

   private Marker derivedCtor;

   public DerivedTrackType()
   {
     derivedCtor = new Marker("derived instance - Ctor");
   }

   public override string ToString()
   {
     StringBuilder rVal = new StringBuilder
       ("A DerivedTrackType object");
     rVal.Append(Environment.NewLine);
     rVal.Append((derivedInit != null) ? 
       "derivedInit has been created" :
       "derivedInit is null");
     rVal.Append(Environment.NewLine);
     rVal.Append((derivedCtor != null) ? 
       "derivedCtor has been created" :
       "derivedCtor is null");
     rVal.Append(Environment.NewLine);
     rVal.Append(base.ToString());
     return rVal.ToString();
   }
}
comments powered by Disqus

Featured

  • Hands On with GitHub Copilot App Technical Preview: Turning a Blazor Issue into a PR

    GitHub's brand-new Copilot desktop app, in technical preview, handled a small Blazor issue from planning through pull request creation, but the hands-on test also showed why developers still need to verify agent work in the running app before merging.

  • At Build 2026, Microsoft Sets Up Windows as an OS for AI Agents

    Microsoft's Build 2026 Windows developer announcements point to a broader platform strategy for agentic AI, spanning terminal workflows, local models, app-building skills, Cloud PCs and operating system-level containment.

  • Slammed by Copilot Usage-Based Billing on Day 1, Facing $180 Bill for June

    A journalist using GitHub Copilot Pro details how a broken editorial workflow on day one of usage-based billing led to runaway token consumption, a projected $180 monthly bill, and practical tactics for cutting AI credit burn.

  • AdaBoost.R2 Regression Using C#

    AdaBoost.R2 regression works by building an ensemble of decision trees, training them on reweighted data, and combining their predictions with a weighted median, while also showing how parameter choices affect accuracy and overfitting.

Subscribe on YouTube