Static constructors


 The Static constructor runs nly for the first tym when the class is instantiated .So we can see the value of the count_static is not increasing ,but the value of count_normal is incremented when more and more instance of class is created .A static constructor does not take access modifiers or have parameters and can't access any non-static data member of a class



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Memory_understanding
{
    class StaticConstructor
    {
        static int a;
        static int count_static;
        static int count_normal;
        private int b;

        static StaticConstructor()
        {
            a = 12;
            count_static++;

        }
        public StaticConstructor(int c)
        {
            b = c;
            count_normal++;
        }

        public void display()
        {
            Console.WriteLine(b);
            Console.WriteLine(a);
            Console.WriteLine(count_static);
            Console.WriteLine(count_normal);
        }
    }

    class program
    {
        public static void Main()
        {
            StaticConstructor aa_= new StaticConstructor(7);
            aa_.display();
            StaticConstructor aaa_ = new StaticConstructor(17);
            aaa_.display();
            StaticConstructor aaaa_ = new StaticConstructor(17);
            aaaa_.display();


            Console.ReadLine();

            /// The Static constructor runs nly for the first tym when the class is instantiated
            /// So we can see the value of the count_static is not increasing ,but the value of
            /// count_normal is incremented when more and more instance of class is created
            ///  A static constructor does not take access modifiers or have parameters and
            ///  can't access any non-static data member of a class.



        }
    }

}

Comments

Popular posts from this blog

Authentication and Authorization in Web API -Part1

My Gardening Journey 6