Existance of a Static variable.

D

Duggi

Hi

When does a static variable come into life? Well, as far as my
knowledge goes, I think it is during the execution of static
constructor. Because thats the logic point in the program where the
usage of static variable starts.

however from the following program sample, I see that static variables
will come into existence even before the static constructor gets
invoked.

public class A
{
public static B abc = new B("from decalration");

static A()
{
abc = new B("From Constructor");
}

}

public class B
{
public B(string message)
{
Console.WriteLine(message);
}
}

public class TestPrg
{
public static void Main()
{
A a = new A();

Console.ReadLine();
}
}


Please let me know if I am wrong or your view on it.

-Cnu
 
J

Jon Skeet [C# MVP]

Duggi said:
When does a static variable come into life? Well, as far as my
knowledge goes, I think it is during the execution of static
constructor. Because thats the logic point in the program where the
usage of static variable starts.

however from the following program sample, I see that static variables
will come into existence even before the static constructor gets
invoked.

What you're really interested in is when the initializer is executed.

From the spec, section 10.5.5.1:

<quote>
The static field variable initializers of a class correspond to a
sequence of assignments that are executed in the textual order in which
they appear in the class declaration. If a static constructor (§10.12)
exists in the class, execution of the static field initializers occurs
immediately prior to executing that static constructor. Otherwise, the
static field initializers are executed at an implementation-dependent
time prior to the first use of a static field of that class.
</quote>
 
D

Duggi

What you're really interested in is when the initializer is executed.

From the spec, section 10.5.5.1:

<quote>
The static field variable initializers of a class correspond to a
sequence of assignments that are executed in the textual order in which
they appear in the class declaration. If a static constructor (§10.12)
exists in the class, execution of the static field initializers occurs
immediately prior to executing that static constructor. Otherwise, the
static field initializers are executed at an implementation-dependent
time prior to the first use of a static field of that class.
</quote>

--
Jon Skeet - <[email protected]>
Web site:http://www.pobox.com/~skeet 
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com

Hi Jon,

Thanks for pulling out the specification from ???

If a static constructor (§10.12)
exists in the class, execution of the static field initializers occurs
immediately prior to executing that static constructor.

So a static variable will come into existance even before the static
constructor gets the control ?

-Cnu
 
J

Jon Skeet [C# MVP]

Duggi said:
Thanks for pulling out the specification from ???

http://download.microsoft.com/download/3/8/8/388e7205-bc10-4226-b2a8-
75351c669b09/CSharp%20Language%20Specification.doc
If a static constructor (§10.12)

So a static variable will come into existance even before the static
constructor gets the control ?

Again, it's not a case of when the variable "comes into existence" -
it's when the initializer is executed. And yes, the initializer is
executed before the static constructor is executed.
 
J

Jon Skeet [C# MVP]

Duggi said:
Is there any difference between, "comes into existence" and execution
of initializer??

Yes. All the variables effectively "come into existence" at the same
time, before any of the initializers are executed. Consider the
following program:

public class Test
{
static int first = SumFirstAndSecond();
static int second = SumFirstAndSecond();

private static int SumFirstAndSecond()
{
return first + second;
}

static void Main(string[] args)
{
// No-op
}
}

The initializer for first calls SumFirstAndSecond, which uses both
first and second, so both variables must *exist* (and be addressable)
at that point. However, the initializer for first is still executing
and the initializer for second hasn't even begun executing at that
point.
 
D

Duggi

Duggi said:
Is there any difference between, "comes into existence" and execution
of initializer??

Yes. All the variables effectively "come into existence" at the same
time, before any of the initializers are executed. Consider the
following program:

public class Test
{
    static int first = SumFirstAndSecond();
    static int second = SumFirstAndSecond();

    private static int SumFirstAndSecond()
    {
        return first + second;
    }    

    static void Main(string[] args)
    {
        // No-op
    }

}

The initializer for first calls SumFirstAndSecond, which uses both
first and second, so both variables must *exist* (and be addressable)
at that point. However, the initializer for first is still executing
and the initializer for second hasn't even begun executing at that
point.

--
Jon Skeet - <[email protected]>
Web site:http://www.pobox.com/~skeet 
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com

quite interesting....Mr. Jon Skeet.

I think I understood the concept here. The static variables come into
*existance* before the static constructor is executed. And
initializers will execute in the sequence of textual order in which
they appear (before the static constructor.) If static constructor is
not available, initializers gets executed before the first use of the
class.

BTW, Just now I understood you are the author of my favorite book, C#
in Depth. I am really honored that I got reply from a gre8 author

-Cnu
 
J

Jon Skeet [C# MVP]

Duggi said:
quite interesting....Mr. Jon Skeet.

I think I understood the concept here. The static variables come into
*existance* before the static constructor is executed. And
initializers will execute in the sequence of textual order in which
they appear (before the static constructor.) If static constructor is
not available, initializers gets executed before the first use of the
class.

Yup, that's basically it. The "coming into existence" is really just a
case of allocating and clearing memory for the variables.
BTW, Just now I understood you are the author of my favorite book, C#
in Depth. I am really honored that I got reply from a gre8 author

Glad you like the book :)
 
D

Duggi

Yup, that's basically it. The "coming into existence" is really just a
case of allocating and clearing memory for the variables.


Glad you like the book :)

--
Jon Skeet - <[email protected]>
Web site:http://www.pobox.com/~skeet 
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com
Jon,

Glad you like the book :)

I am you fan after reading the book..The way you explained the
differences between 1.0, 2.0 and 3.0 / 3.5 is extreemly impressive. I
yet to complete reading the book...

-Cnu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Automatic vs. explicit initialization of immutable static objects. 3
static 2
file [?] 7
Static variable and constructor 10
C# and COM Object 2
Override static derived variable 14
interface 1
Use of event handling 6

Top