Fremus.co.za

Demistifying Life and Web Development

C# Constructors

Have you noted that when you create a constructor for a base class with any parameters you get a compiler error? So for example this gives an error:

namespace Constructors
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
    class MyTestClass
    {
        public MyTestClass(int testValue1, int testValue2)
        {
        }
    }
    class MyOtherTestClass : MyTestClass
    {
    }
}

Error 1 ‘Constructors.MyTestClass’ does not contain a constructor that takes ’0′ arguments

To fix this error you have to use the base keyword like this:

    class MyOtherTestClass : MyTestClass
    {
        public MyOtherTestClass(int testVal1, int testVal2)
            : base(testVal1, testVal2)
        {
        }
    }
  • Share/Bookmark
posted by fr3dr1k in C# and have No Comments

Place your comment

Please fill your data and comment below.
Name
Email
Website
Your comment
Get Adobe Flash playerPlugin by wpburn.com wordpress themes