Can I really and truly trust .NET to initialize fields (like ints, structs and the like)? And what if I still want to initialize those fields - what could be the repercussions?
Asked
Active
Viewed 5,358 times
2 Answers
18
The C# specification states on p.305 (17.4.4)
The initial value of a field, whether it be a static field or an instance field, is the default value (§12.2) of the field’s type. It is not possible to observe the value of a field before this default initialization has occurred, and a field is thus never “uninitialized”.

tamberg
- 1,987
- 14
- 23
12
Yes, you can really trust .NET to initialize fields to their default values. There are basically no repercussions for doing it explicitly. (One small caveat here: if you initialize static fields explicitly, then anyone running the type initializer a second time via reflection will end up re-initializing those fields. This is a real corner case though!)
Do whatever promotes the most readability in your particular codebase.

Jon Skeet
- 1,421,763
- 867
- 9,128
- 9,194