Click here to Skip to main content
15,881,044 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
See more:
I have doubt in static and non static constructor;

please see following example carefully here I am declaring static and non static constructor and accessing static and non static members from it

class SampleConstructor
   {
        public static int s;
        public int ns;

        //creating a static constructor
        static SampleConstructor()
        {
            s = 23;

        }

        //Creating a non static constructor

        public SampleConstructor()
        {
           // s = 45;
            ns = 33;
        }

    }
    class UseSampleConstructor
    {
        static void Main(string[] args)
        {
            SampleConstructor sc = new SampleConstructor();
            Console.WriteLine("{0},{1}" ,sc.ns,SampleConstructor.s);
            Console.ReadKey();
        }
    }


------------------------------------------------------------------------
output of above example is 33,23

which is completely fine however when I am trying to initialize static member of class through non static constructor without altering anything like this


class SampleConstructor
    {
        public static int s;
        public int ns;

        //creating a static constructor
        static SampleConstructor()
        {
            s = 23;

        }

        //Creating a non static constructor

        public SampleConstructor()
        {   
            //here I am initializing static member also
            s = 45;
            ns = 33;
        }

    }
    class UseSampleConstructor
    {
        static void Main(string[] args)
        {
            SampleConstructor sc = new SampleConstructor();
            Console.WriteLine("{0},{1}" ,sc.ns,SampleConstructor.s);
            Console.ReadKey();
        }
    }


now In this case I am getting output as 33,45

why??? even though I am still accessing SampleConstructor.s which is static is member of static constructor

can anyone please help me??
Posted
Updated 22-Dec-17 1:19am
v3

From documentation (MSDN [^]):

A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced.


Your instance constructor (i.e. the non-static one) is called after the static one and hence it alters the static member content.
:)
 
Share this answer
 
v2
Comments
[no name] 19-Mar-11 9:22am    
From OP: Thanks Pallini and Navaneeth for clearing my doubt
wrote:
even though I am still accessing SampleConstructor.s which is static is member of static constructor


It is not a member of static constructor. It is a member of type SampleConstructor.

wrote:
now In this case I am getting output as 33,45

why???


static constructor gets executed first which will set s = 23 and instance constructor will run next which will overwrite the value to 45.
 
Share this answer
 
Comments
[no name] 19-Mar-11 9:22am    
From OP: Thanks Pallini and Navaneeth for clearing my doubt
Static members can only be called with the name of the class,initially your 's' is initialized by the static constructor but when you called the non static constructor in a main method,your 's' value is replaced by the non static constructor

Static constructor only initialize static variables but Non Static constructor can initialize both static as well as non static variables
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900