473,386 Members | 1,793 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,386 software developers and data experts.

Compiler: static constructors and field init compilation order

Hi everyone,

Is it possible, using an Attribute or by some other means, to notify the C#
Compiler to serialize all static field's that have initializers before code
in an explicit static constructor?

Example:

public class MyClass
{
public static readonly int MyStaticField = 1;

static MyClass()
{
// Compiler emits the code to initialize MyStaticField as 1 here

Console.WriteLine();

// I need the compiler to emit the code here
}
}

-- I know it might not be clear as to why I need this functionality by
looking at the example above, but it would be helpful in my real-world
situation.

If you need details as to why I need this functionality from the compiler,
just let me know and I'll post it.

Thanks in advance,

~ Dave
Nov 16 '05 #1
3 2767
Dave <replace_with@dave_jwaonline.com> wrote:
Is it possible, using an Attribute or by some other means, to notify the C#
Compiler to serialize all static field's that have initializers before code
in an explicit static constructor?
I'm not sure what the benefit would be - surely all the fields will
just have the default values for their types, won't they?
Example:

public class MyClass
{
public static readonly int MyStaticField = 1;

static MyClass()
{
// Compiler emits the code to initialize MyStaticField as 1 here

Console.WriteLine();

// I need the compiler to emit the code here
}
}
If you really need some code to be executed before the rest of the
variable initializers, why not have one variable initializer at the
top:

bool ignored = DoStuffNow();

static bool DoStuffNow()
{
// Do everything here...
return true;
}
-- I know it might not be clear as to why I need this functionality by
looking at the example above, but it would be helpful in my real-world
situation.

If you need details as to why I need this functionality from the compiler,
just let me know and I'll post it.


I don't *need* details as to why you want this, but I must say I'm
intrigued and would like to know more.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Hi Jon,

Thanks for responding.

I will try to explain this the best that I can:

I have about 20 of the "MyStaticField" inline initializers, all of them of
type SqlCommand. I expect to have more in the future and use #region to
categorize them in an orderly fashion. I would like to keep them inline if
possible (ie not in a Method "DoStuffNow" or a static constructor) because
it would be somewhat of a hassle to manage the constructor code and the
various readonly variable declarations. Each of the intializers use a
helper function I've written that requires a certain static field (in the
helper library) to be set. The field is set to a default value per a custom
section in the web.config file. I'd like to programmatically set this field
to a different "default" value upon static construction of the class being
discussed.

In other words...

Each inline initializer depends upon a variable that must be changed before
they are "initialized". The most appropriate place to set this variable
would be in the static constructor of this class. The C# compiler
automatically serializes all inline statements before the statements in the
explicit static constructor. Obviously this is not what I need, as I want
my constructor statements to change the "default" value before the commands
are actually created.

I hope I've made my situation clear enough to you.

I'm open to any work-arounds or solutions that are different than my
expectations, although I've researched on google groups and some books and
have found no way of doing this... or even something like it for that
matter. :(

Well, I appreciate your time and thank you for your responses in advance...

~ Dave

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Dave <replace_with@dave_jwaonline.com> wrote:
Is it possible, using an Attribute or by some other means, to notify the C# Compiler to serialize all static field's that have initializers before code in an explicit static constructor?


I'm not sure what the benefit would be - surely all the fields will
just have the default values for their types, won't they?
Example:

public class MyClass
{
public static readonly int MyStaticField = 1;

static MyClass()
{
// Compiler emits the code to initialize MyStaticField as 1 here

Console.WriteLine();

// I need the compiler to emit the code here
}
}


If you really need some code to be executed before the rest of the
variable initializers, why not have one variable initializer at the
top:

bool ignored = DoStuffNow();

static bool DoStuffNow()
{
// Do everything here...
return true;
}
-- I know it might not be clear as to why I need this functionality by
looking at the example above, but it would be helpful in my real-world
situation.

If you need details as to why I need this functionality from the compiler, just let me know and I'll post it.


I don't *need* details as to why you want this, but I must say I'm
intrigued and would like to know more.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
Dave <NO*********@dotcomdatasolutions.com> wrote:
I have about 20 of the "MyStaticField" inline initializers, all of them of
type SqlCommand. I expect to have more in the future and use #region to
categorize them in an orderly fashion. I would like to keep them inline if
possible (ie not in a Method "DoStuffNow" or a static constructor) because
it would be somewhat of a hassle to manage the constructor code and the
various readonly variable declarations. Each of the intializers use a
helper function I've written that requires a certain static field (in the
helper library) to be set. The field is set to a default value per a custom
section in the web.config file. I'd like to programmatically set this field
to a different "default" value upon static construction of the class being
discussed.


Right. I can't say it sounds like a terribly pleasant design to me, but
there we go. The only thing I can suggest is the example I gave before
- put a "dummy" value at the top of your class file, and make that use
a method which sets the default value you want the rest of your
initializers to use.

You should be aware, however, that if you've got two different classes
being initialized at the same time, everything could go very pear-
shaped.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

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

Similar topics

1
by: Alex Vinokur | last post by:
Hi, I have a problem with invoking an instance before main() when using memory allocation for static member. Is the program below valid? =========================================== Windows...
8
by: jose luis fernandez diaz | last post by:
Hi, I am reading Stroustrup's book 'C++ Programming Language'. In the 10.4.9 section (Nonlocal Store) he says: "A variable defined outside any function (that is global, namespace, and class...
6
by: clilley | last post by:
The following code causes a segmentation fault on DEC Tru64: foo.cc (built into libFoo.so) //--------------------------- include <iostream> bool createFoo() { std::cout << "createFoo" <<...
4
by: Jim Tester | last post by:
I have the following class: In MyIClass class MyInterfaceClass { public: explicit MyInterfaceClass( char const * const pName, bool init ); virtual ~MyInterfaceClass( void );
4
by: pixelbeast | last post by:
hi, In the following code, I would like not to have to declare the constructors in bar ( either the default or the (int a) constructor ). When I remove them, bar has no idea of construction with...
3
by: Amit | last post by:
is there anything like static constructors or destructors in C++ ? if yes, how to implement it? Thanks, Amit.
3
by: Dave | last post by:
Hi everyone, Is it possible, using an Attribute or by some other means, to notify the C# Compiler to serialize all static field's that have initializers before code in an explicit static...
3
by: Karl M | last post by:
Hi everyone, I just notice some strange behaviors on the MS C++ compiler regarding struct default constructor, see the example bellow: struct MyStruct { int a; }; class MyClass { public:
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.