none
Constructor of a parent class is not getting executed RRS feed

  • Question

  • Hi,

             I have created a container class with two public nested class, The constructor or static constructor is never getting called. How are we able to access nested class like accessing a static variable even thought the class is not static.Please check the code below.

    using System;
    
    namespace NestedClass
    {
        class Program
        {
            static void Main(string[] args)
            {
                Container.ChildContainer.Text = "Some value";
                Container.StaticChildContainer.Text = "Some value";
    
                Container.ChildContainer childContainer = new Container.ChildContainer();
                childContainer.Input = "Some Input";
    
                Console.ReadKey();
            }
        }
    
        public class Container
        {
            public static string Text { get; set; }
    
            public string Input { get; set; }
    
            static Container() 
            {
                Console.WriteLine("Container Static Constructor");
            }
            public Container() 
            {
                Console.WriteLine("Container Static Constructor");
            }
            public class ChildContainer
            {
                public static string Text { get; set; }
                
                public string Input { get; set; }
               
                public ChildContainer() 
                {
                    Console.WriteLine("ChildContainer Constructor");
                }
    
                static ChildContainer()
                {
                    Console.WriteLine("ChildContainer static Constructor");
                }
            }
    
            public static class StaticChildContainer
            {
                public static string Text { get; set; }
    
                static StaticChildContainer()
                {
                    Console.WriteLine("StaticChildContainer static Constructor");
                }
            }
        }
    
    }
    

    Wednesday, July 10, 2013 5:13 AM

Answers

All replies

  • hi,

    static constructor is priority to generic  constructor  at runtime. But Static member is priority  to static constructor . Generic constructor will execute until  common class are  instantiated. Creating a static class is therefore basically the same as creating a class that derives from Object, contains only static members, and is both sealed and abstract. Static classes are sealed and therefore can't be inherited. They can't inherit from any class except Object. A static class can't contain an instance constructor but can contain a static constructor.

    If you want to call base class ,you can make subclass inherited  from base class.

    Wednesday, July 10, 2013 8:18 AM
  • Thanks for your reply, I wanted to know why its not invoking any parent class constructors... You will get the following output..

    ChildContainer static Constructor
    StaticChildContainer static Constructor
    ChildContainer Constructor

    But it never calls Container constructor.

    Regards,

    Shijith

    Wednesday, July 10, 2013 3:23 PM
  • Thats rule of inheritance, would be applicable for derived class not for nested class.
    Wednesday, July 10, 2013 4:59 PM
  • The runtime doesn't call the static constructor of Container because your program neither creates an instance of Container nor accesses any static field of Container.

    If you want to ensure that the static constructor of Container is run, you can use the RuntimeHelpers.RunClassConstructor method.

    • Marked as answer by Shijith M.C Friday, July 12, 2013 2:57 AM
    Thursday, July 11, 2013 9:40 PM