Quantcast
Channel: Common Language Runtime Internals and Architecture forum
Viewing all articles
Browse latest Browse all 1710

Constructor of a parent class is not getting executed

$
0
0

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");
            }
        }
    }

}


Viewing all articles
Browse latest Browse all 1710

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>