cannot have instance field initializers in structs

R

Rene

Could anyone tell me what is the reason I can't initialize the members of a
struct like this:

public struct SomeStruct
{
public int uno = 1; // Error
public int dos = 2; // Error
public int tres = 3; // Error
}

I understand that I can initialize the members by providing a constructor or
directly setting them, I just don't see the harm on doing the above.
Thank you.
 
M

Mattias Sjögren

Could anyone tell me what is the reason I can't initialize the members of a
struct like this:

Because the compiler moves the initialization code to the constructor.
But it can't generate a default constructor for structs.



Mattias
 
R

Richard Blewett [DevelopMentor]

I blogged about this a while back:

http://www.dotnetconsult.co.uk/weblog/commentview.aspx/03805a0c-525f-4b7d-b0a0-f5f2773b4a7c

(watch for line breaks)

I mention field initializers at the end of the section on default constructors

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk/weblog

Could anyone tell me what is the reason I can't initialize the members of a
struct like this:

public struct SomeStruct
{
public int uno = 1; // Error
public int dos = 2; // Error
public int tres = 3; // Error
}

I understand that I can initialize the members by providing a constructor or
directly setting them, I just don't see the harm on doing the above.
Thank you.
 
R

Rene

I read your article and it was very interesting *however*, it appears that
the main reason not to allow a default constructor on a struct is because
you can have code in the constructor that could delay the creation of the
struc.



The problem that I have with this is "So What?" let the constructor take a
year to initialize, who cares. If I wanted to, I could have a constructor in
a regular class that takes 3 years to run and nobody cares, why this sudden
urge not to allow this from happening on a struct?



My guess is that something must be going on in the runtime that would get
screwed up if they let this happen, perhaps all application using the
runtime will stop working, perhaps, the garage collector would stop cleaning
while this goes on, I have no idea but that is exactly what I would like to
find out, do you know what it is?



Thank you.
 
R

Richard Blewett [DevelopMentor]

Because when you allocate an array of reference types all you get is an array of references and therefore the allocation is always a known thing. With value type arrays, the memory for the value type is allocated inline and therefore needds to be initialized. If you provide a default constructor (or field initializer) that those would have to run as a result. This creates a potential hidden cost in array allocation for value types. Also, you can obtain value types from unmanaged code which certainly won't have run your default constructor code.

Why don't you put your initialization in a non-default constructor?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

I read your article and it was very interesting *however*, it appears that
the main reason not to allow a default constructor on a struct is because
you can have code in the constructor that could delay the creation of the
struc.



The problem that I have with this is "So What?" let the constructor take a
year to initialize, who cares. If I wanted to, I could have a constructor in
a regular class that takes 3 years to run and nobody cares, why this sudden
urge not to allow this from happening on a struct?



My guess is that something must be going on in the runtime that would get
screwed up if they let this happen, perhaps all application using the
runtime will stop working, perhaps, the garage collector would stop cleaning
while this goes on, I have no idea but that is exactly what I would like to
find out, do you know what it is?
 
R

Rene

Hey Richard:

Thanks for the respond. First things first, I don't really have a need to
implement my own default constructor or to manually initialize field
variables, I am just curios to know why this is not possible. At the end of
the day, I have to accept what the compiler allows me to do and it really
makes no different if I know what's going on behind the scenes or not but I
can't help my self!! :)

I am still not quite sure why would the runtime have a problem with how long
it would take to allocate my struct. As far as I am concern, the runtime
should say "screw the programmer", if it takes an hour to initialize this
struct because the programmer made a ridiculous default stuct then... who
cares?

Sorry for not getting it! Maybe I should give up programming and become a
porn star!!!
 
R

Richard Blewett [DevelopMentor]

You can create a default ctor for a value type using IL rather than C#. However, the point is that the newarr IL opcode does not call it. It does one thing and one thing only to the memory of the allocated array - zero it. This is to make sure that array allocation is always a know quantity. For ref types this is straightforward as we just get null references, for value types though this adds a restriction. If we provided a default constructor then the newarr opcode should call it, but all it does is allocate a memory block of the correct size and zero it. So they would have to encode a call to the types default constuctor in the newarr opcode for value types on the offchance that someone had provided a custom default constructor. This would be a completely unnecessary overhead in the vast majority of situations, apart from the fact that value type arrays would then have to have special handling in JIT compilers handling of the newarr opcode.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hey Richard:

Thanks for the respond. First things first, I don't really have a need to
implement my own default constructor or to manually initialize field
variables, I am just curios to know why this is not possible. At the end of
the day, I have to accept what the compiler allows me to do and it really
makes no different if I know what's going on behind the scenes or not but I
can't help my self!! :)

I am still not quite sure why would the runtime have a problem with how long
it would take to allocate my struct. As far as I am concern, the runtime
should say "screw the programmer", if it takes an hour to initialize this
struct because the programmer made a ridiculous default stuct then... who
cares?

Sorry for not getting it! Maybe I should give up programming and become a
porn star!!!
 

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

Top