![]() ![]() The IsDefault property generalizes the is null pattern to defaultable value types. Non-defaultable fields, a warning is produced if those fields are not initialized in a constructor (including the Non-defaultable value types are allowed to have fields that are themselves non-defaultable. Fieldsįields of defaultable value types must be defaultable, which means they have a type that is either nullable or is Without this attribute are considered defaultable. Non-defaultable value types are marked with. ![]() The compiler (the same way nullable reference types behaves). Value types is enabled in nullable-aware code, and any diagnostics produced by the analysis are reported as warnings by Non-defaultable value types are value types for whiche default(T) behaves more like null. Struct StructWithDefaultConstructor Defaultable value typesĪ defaultable value type is a value type for which default(T) produces a valid (usable) instance of T. Those constructors are being implemented because a zero-filled default doesn't make sense for that type, therefore a zero-filled default(S) would be an invalid value for that struct. ![]() Likewise with structs that implement parameterless constructors. As far as I can see therefore, "fixing" that optimisation, and allowing the two struct creation options to behave differently could prove a huge breaking change for many non-nullable reference types are introduced, then for a non-nullable class, C, what would you expect the result of default(S) to be? "Zero-filled" null? That seems broken to me as suddenly my non-nullable class as null reference, which just broke my code. Because of that, lots of code will have been written that uses default(S) instead of new S(), because they currently do exactly the same thing. NET languages didn't allow custom parameterless constructors, so there was no point in calling it for structs. NET languages, due to the optimization introduced in Activator.CreateInstance. Now, if you don't like the idea of parameterless constructors in structs and don't want Microsoft to encourage writing them I definitely think that's a fair New S() and default(S) do the same thing in all. At this point the question should be only whether C# should permit defining new structs with parameterless constructors. That ship has apparently sailed a long time ago. So, in my opinion we need to move on from the conversation about whether structs should have parameterless constructors or whether C# should consider new T() and default(T) to be two different things. But it's quite possible for some other language to define such a struct, and C# already respects such structs by invoking their constructors. So low that a bug made it into the BCL assuming that such a beast wouldn't exist. Of course since neither C# nor VB.NET permitted defining a struct with a parameterless constructor the odds of running into one would be exceptionally low. To the CLR, and even to C#, new T() and default(T) have never meant the same thing. That behavior is already means something different from default(T) and, as far as I'm aware, that has been the case since C# 1.0. I'd like to reiterate that this proposal has nothing to do with changing how the C# compiler interprets new T() where T is a struct. ![]() If this feature were correctly implemented, eg via an overridable default method in the struct, then I could set those default values (or even throw some "not properly constructed" exception at the point of creation) when default(Gearbox) or new Gearbox() is called.Īs the proposal currently stands though, default(Gearbox) wouldn't call the constructor, so from my point of view it doesn't achieve anything useful: I'd still need that guard code or would still need to use a class. The latter then either suffers the equivalent of the null ref problem if created with default(Gearbox) or new Gearbox(), or I have to leave all the fields as mutable and, on all methods and properties of the struct, I have to put guard code that checks for a misconstruction and sets up a proper default set of values. Say I have a type that holds information on a car gearbox, including the number of gear stick positions and the gearing ratios and there will always be at least two positions (forward and reverse).Ĭurrently, my options are to use a class (so I can control its construction) and suffer null ref problems, or have a struct. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |