... all I'm offering is the truth. Nothing more. RSS 2.0
# Friday, May 30, 2008

Reading source code should be the aim of every serious software developer. I am constantly looking for patterns that are interesting and potentially useful for future projects. Anyway, I was reviewing some code over the weekend and there was a pattern that I have not seen for a while.

So below is a basic abstract Mammal class, you have two constructors differentiated by the method signature (of course),

    public abstract class Mammal
    {
        public Mammal()
        {
            Console.WriteLine("A");
        }
        public Mammal(bool asf)
        {
            Console.WriteLine("B");
        }
    }
So the MyDog class inherits from from Mammal and provides an opportunity to access the either of the base class constructors.

    public class MyDog : Mammal
    {
        public MyDog() : base()
        {
            Console.WriteLine("1");
        }
        public MyDog(bool asf) : base(asf)
        {
            Console.WriteLine("2");
        }
    }

You can explicitly select which base class constructor gets called by using ": base()" pattern after the constructor. Simple but nice!

Technorati tags: ,

Friday, May 30, 2008 12:05:09 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] - Trackback
C#
Blogroll
Statistics
Total Posts: 336
This Year: 24
This Month: 2
This Week: 2
Comments: 32
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Mark Downie
Sign In
All Content © 2010, Mark Downie
DasBlog theme 'Business' created by Christoph De Baene (delarou)