Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0
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;
 
<pre>
    //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();
    }
}</pre>




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
 
<pre>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();
    }
}</pre>

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 11-Jan-10 3:39am
Edited 19-Mar-11 4:19am
Not Active10.2K
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
Smile | :)
  Permalink  
v2
Comments
Mark Nischalke at 19-Mar-11 9:22am
   
From OP: Thanks Pallini and Navaneeth for clearing my doubt
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
Mark Nischalke at 19-Mar-11 9:22am
   
From OP: Thanks Pallini and Navaneeth for clearing my doubt
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 405
2 Maciej Los 310
3 ProgramFOX 265
4 CHill60 200
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 405
2 Maciej Los 285
3 ProgramFOX 265
4 Peter Leow 210


Advertise | Privacy | Mobile
Web03 | 2.8.150331.1 | Last Updated 19 Mar 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100