473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Existance of a Static variable.

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
Sep 22 '08 #1
3 1668
Duggi <Du***************@gmail.comwrote:
Thanks for pulling out the specification from ???
http://download.microsoft.com/downlo...c10-4226-b2a8-
75351c669b09/CSharp%20Language%20Specification.doc
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 ?
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.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 22 '08 #2
On Sep 22, 6:11*pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Duggi <DuggiSrinivasa...@gmail.comwrote:
Thanks for pulling out the specification from ???

http://download.microsoft.com/downlo...c10-4226-b2a8-
75351c669b09/CSharp%20Language%20Specification.doc
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 ?

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.

--
Jon Skeet - <sk...@pobox.com>
Web site:http://www.pobox.com/~skeet*
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com
Thanks Jon.

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

-Cnu
Sep 22 '08 #3
Duggi <Du***************@gmail.comwrote:
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 - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 22 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Patrick Kowalzick | last post by:
Hello all, I want to test run-time if a template function for a special type exists. See example below. The checkfoo function is where I need some hints. Regards, Patrick #include...
3
by: Fabian | last post by:
How can I check whether or not a variable exists in a script? I'm thinking in terms of if (my variable exists) { myfunction(); } -- -- Fabian Visit my website often and for long periods!
9
by: Josiah Manson | last post by:
Hello. I am very new to Python, and have been unable to figure out how to check if a variable exists or not. In the following code I have made a kludge that works, but I think that it would be...
0
by: Bob | last post by:
I want to find a way to detect the existance of the private member of a particular type in the derived class from inside the base class itself and call its Dispose() method. Reflection GetFields()...
2
by: Imran Aziz | last post by:
Hello All, I have ASP.net , C# pages checking for query string variables, the old ASP has a count property that one could check for each key/value pair to determine if it is available in the query...
28
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. ...
2
by: PiotrKolodziej | last post by:
Hi I have a simple question. Here is the code related to my question: while (true) { if (tcpListenerServer.Pending() && !this.Disposing) { TcpClient tcpClient =...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
2
by: Duggi | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.