views:

195

answers:

3

When you have automatic properties, C# compiler asks you to call the this constructor on any constructor you have, to make sure everything is initialized before you access them.

If you don't use automatic properties, but simply declare the values, you can avoid using the this constructor.

What's the overhead of using this on constructors in structs? Is it the same as double initializing the values?

Would you recommend not using it, if performance was a top concern for this particular type?

+2  A: 

Yes, the values will be initialized twice and without profiling it is difficult to say whether or not this performance hit would be significant.

The default constructor of a struct initializes all members to their default values. After this happens your constructor will run in which you undoubtedly set the values of those properties again.

I would imagine this would be no different than the CLR's practice of initializing all fields of a reference type upon instantiation.

Andrew Hare
But if it's an automatic property, I guess it's just gonna be the default value, right? 0 for int, null for string?
Joan Venge
Correct, even though it is an automatic property it still has a field backing it up. The field's value will be set to the default value for its type (integrals = 0, reference types = null, etc.)
Andrew Hare
Thanks Andrew. Just out of curiosity, by integrals you mean integers? Or are they the same thing?
Joan Venge
Sorry, integrals are basically "integer-like" types (int, long, uint, etc.) but what I said also applies to singles and doubles as well.
Andrew Hare
Thanks Andrew, now I learnt something new. That's why I asked. Is that a .net term?
Joan Venge
No, just an old mathematical term.
Andrew Hare
+1  A: 

The reason the C# compiler requires you to chain to the default constructor (i.e. append : this() to your constructor declaration) when auto-implemented properties are used is because all variables need to be assigned before exiting the constructor. Now, auto-implemented properties mess this up a bit in that they don't allow you to directly access the variables that back the properties. The method the compiler uses to get around this is to automatically assign all the variables to their default values, and to insure this, you must chain to the default constructor. It's not a particularly clever method, but it does the job well enough.

So indeed, this will mean that some variables will end up getting initialised twice. However, I don't think this will be a big performance problem. I would be very surprised it the compiler (or at very least the JIT) didn't simply remove the first initialisation statement for any variable that is set twice in your constructor. A quick benchmark should confirm this for you, though I'm quite sure you will get the suspected results. (If you by chance don't, and you absolutely need the tiny performance boost that avoidance of duplicate initialisation offers, you can just define your properties the normal way, i.e. with backing variables.)

To be honest, my advice would be not even to bother with auto-implemented properties in structures. It's perfectly acceptable just to use public variables in lieu of them, and they offer no less functionality than auto-implemented properties. Classes are a different situation of course, but I really wouldn't hesitate to use public variables in structs. (Any complex properties can be defined normally, if you need them.)

Hope that helps.

Noldorin
+3  A: 

I would recommend not using automatic properties at all for structs, as it means they'll be mutable - if only privately.

Use readonly fields, and public properties to provide access to them where appropriate. Mutable structures are almost always a bad idea, and have all kinds of nasty little niggles.

Do you definitely need to create your own value type in the first place though? In my experience it's very rare to find a good reason to create a struct rather than a class. It may be that you've got one, but it's worth checking.

Back to your original question: if you care about performance, measure it. Always. In this case it's really easy - you can write the struct using an automatic property and then reimplement it without. You could use a #if block to keep both options available. Then you can measure typical situations and see whether the difference is significant. Of course, I think the design implications are likely to be more important anyway :)

Jon Skeet
Thanks Jon. Just saw your reply. Automatic properties mean mutable, how? I thought if I use private on the set keyword in the auto property, that would make it immutable? Also what's the threshold of setting values of an immutable type? Setting them inside the constructor is fine, right?
Joan Venge
It doesn't make them properly immutable - it just means that you can only mutate them within the type. While that may well seem like a small difference, I believe it's an important one. And yes, you can set readonly variables within a constructor as much as you like.
Jon Skeet
Thanks Jon for your reply.
Joan Venge