.NET and the nullable configuration
-
Does anyone actually use .NET 7 with
enable
? I find this option makes life such a PITA that I always go into the .csproj file and disable it. The warnings are non-stop:Non-nullable property 'foo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable
Converting null literal or possible null value to non-nullable type.
Dereference of a possibly null reference.
Possible null reference assignment.
Nullable value type may be null.
And that's just a partial list, I believe. And that last one, "Nullable value type may be null" is the most amusing one. Seriously, if you have the "Nullable" option enabled, do you actually fix all those warning?
Latest Articles:
SVG Grids: Squares, Triangles, Hexagons with scrolling, sprites and simple animation examples
An SVG Analog Clock -
Does anyone actually use .NET 7 with
enable
? I find this option makes life such a PITA that I always go into the .csproj file and disable it. The warnings are non-stop:Non-nullable property 'foo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable
Converting null literal or possible null value to non-nullable type.
Dereference of a possibly null reference.
Possible null reference assignment.
Nullable value type may be null.
And that's just a partial list, I believe. And that last one, "Nullable value type may be null" is the most amusing one. Seriously, if you have the "Nullable" option enabled, do you actually fix all those warning?
Latest Articles:
SVG Grids: Squares, Triangles, Hexagons with scrolling, sprites and simple animation examples
An SVG Analog ClockI'm fairly sure this setting is just a reaction to new(er) languages like Swift (iOS) and Kotlin (android). Both of those languages use the idea that nothing is really nullable by default. In Swift they call these Optionals and you have to indicate in a special way that the object may contain a null (actually nil in Swift). So, for example, if you define an Optional variable you have to define it like:
var dataConnection: MyData!
That ! marks the type as Optional (meaning it may contain a real value or a nil). If you don't add that ! then you must supply a value for the variable immediately -- in an effort to insure you aren't using vars that contain nil without them being initialized. This states at the outset that this variable may be nil and there is a lot of protection around the variable so it's not as easy to accidentally set it to nil. All of this, as you mention also, really creates a bit more work. Anyways, I believe the setting in C# is now just a reaction to that and may be a precursor of things to come in C# world.
-
I'm fairly sure this setting is just a reaction to new(er) languages like Swift (iOS) and Kotlin (android). Both of those languages use the idea that nothing is really nullable by default. In Swift they call these Optionals and you have to indicate in a special way that the object may contain a null (actually nil in Swift). So, for example, if you define an Optional variable you have to define it like:
var dataConnection: MyData!
That ! marks the type as Optional (meaning it may contain a real value or a nil). If you don't add that ! then you must supply a value for the variable immediately -- in an effort to insure you aren't using vars that contain nil without them being initialized. This states at the outset that this variable may be nil and there is a lot of protection around the variable so it's not as easy to accidentally set it to nil. All of this, as you mention also, really creates a bit more work. Anyways, I believe the setting in C# is now just a reaction to that and may be a precursor of things to come in C# world.
I worked in Kotlin a while back and it was considered bad form to use
!
Everything that could potentially be null had to be handled to return a non-null value - just one of the things I disliked about Kotlin :laugh:“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
-
I worked in Kotlin a while back and it was considered bad form to use
!
Everything that could potentially be null had to be handled to return a non-null value - just one of the things I disliked about Kotlin :laugh:“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
GuyThiebaut wrote:
I worked in Kotlin a while back and it was considered bad form to use !
Yes, in Kotlin when you use ! it means, "it may be null, but I don't care, go ahead and force it" So it yells at you all the time. These new languages will definitely switch your thinking if you've been programming and are used to null. We use null as a value like false so often and then suddenly (supposedly) nothing is ever null. It's an odd thing. It's all more like having training wheels on than anything. :)
-
Does anyone actually use .NET 7 with
enable
? I find this option makes life such a PITA that I always go into the .csproj file and disable it. The warnings are non-stop:Non-nullable property 'foo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable
Converting null literal or possible null value to non-nullable type.
Dereference of a possibly null reference.
Possible null reference assignment.
Nullable value type may be null.
And that's just a partial list, I believe. And that last one, "Nullable value type may be null" is the most amusing one. Seriously, if you have the "Nullable" option enabled, do you actually fix all those warning?
Latest Articles:
SVG Grids: Squares, Triangles, Hexagons with scrolling, sprites and simple animation examples
An SVG Analog ClockI just never use nullable types. Or .net beyond 4.8 (I think).
-
Does anyone actually use .NET 7 with
enable
? I find this option makes life such a PITA that I always go into the .csproj file and disable it. The warnings are non-stop:Non-nullable property 'foo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable
Converting null literal or possible null value to non-nullable type.
Dereference of a possibly null reference.
Possible null reference assignment.
Nullable value type may be null.
And that's just a partial list, I believe. And that last one, "Nullable value type may be null" is the most amusing one. Seriously, if you have the "Nullable" option enabled, do you actually fix all those warning?
Latest Articles:
SVG Grids: Squares, Triangles, Hexagons with scrolling, sprites and simple animation examples
An SVG Analog ClockI found it rather jarring the very first time I built a .NET project using .NET 7 (something that had actually started life as a .NET Framework 4.8 project), so there were quite a few "fixes" to implement. But I came around rather quickly, and now kinda wished older versions were that restrictive/explicit.
-
Does anyone actually use .NET 7 with
enable
? I find this option makes life such a PITA that I always go into the .csproj file and disable it. The warnings are non-stop:Non-nullable property 'foo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable
Converting null literal or possible null value to non-nullable type.
Dereference of a possibly null reference.
Possible null reference assignment.
Nullable value type may be null.
And that's just a partial list, I believe. And that last one, "Nullable value type may be null" is the most amusing one. Seriously, if you have the "Nullable" option enabled, do you actually fix all those warning?
Latest Articles:
SVG Grids: Squares, Triangles, Hexagons with scrolling, sprites and simple animation examples
An SVG Analog ClockYes, I fix them. And I only add ! if I can verify the value can't be null and I am not able to rewrite the code so the compiler sees it as well. It does become a mess if using an old .NET version without the "required" keyword, so I am refusing to use anything old now. This is not just a response to other languages (though competition of course means you can't just ignore problems), but a response to something that is a fundamental design error in the type system of C# and many other OO languages that copied Tony Hoare's "billion dollar mistake".
-
Does anyone actually use .NET 7 with
enable
? I find this option makes life such a PITA that I always go into the .csproj file and disable it. The warnings are non-stop:Non-nullable property 'foo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable
Converting null literal or possible null value to non-nullable type.
Dereference of a possibly null reference.
Possible null reference assignment.
Nullable value type may be null.
And that's just a partial list, I believe. And that last one, "Nullable value type may be null" is the most amusing one. Seriously, if you have the "Nullable" option enabled, do you actually fix all those warning?
Latest Articles:
SVG Grids: Squares, Triangles, Hexagons with scrolling, sprites and simple animation examples
An SVG Analog ClockI fix most of them, but I'm not afraid to use `!` (even the phrase `null!`) or `Debug.Assert(thing != null, "the thing")` or some of the nullability-related attributes. I don't think we should drink too much of the Kool-Aid, I don't aim for perfectly "pure" use of Nullable. Using it doesn't really bother me in code that already makes use of it, there aren't tons of spurious warnings in that case, but converting a medium-size codebase to enable Nullable was a chore (that process did reveal a couple of bugs that were actually bad and had evaded detection up to then). No doubt the main thing Nullable prevents is trivial mistakes that would be noticed immediately when pressing F5 anyway. (beginners seem to live in total fear and terror of the `NullReferenceException`, but let's be real, it's usually no big deal) But since converting a codebase to use Nullable revealed a couple of actual bugs too, well those bugs would never have existed if Nullable had been enabled all along, so maybe it's doing something serious too. It's not effort-free to use, and the payoff is in something *not* happening, it's impossible to know *what* didn't happen, so who knows what the cost/benefit ratio is?
-
Does anyone actually use .NET 7 with
enable
? I find this option makes life such a PITA that I always go into the .csproj file and disable it. The warnings are non-stop:Non-nullable property 'foo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable
Converting null literal or possible null value to non-nullable type.
Dereference of a possibly null reference.
Possible null reference assignment.
Nullable value type may be null.
And that's just a partial list, I believe. And that last one, "Nullable value type may be null" is the most amusing one. Seriously, if you have the "Nullable" option enabled, do you actually fix all those warning?
Latest Articles:
SVG Grids: Squares, Triangles, Hexagons with scrolling, sprites and simple animation examples
An SVG Analog ClockSome people seem to find some computer idioms and technologies difficult to understand. And then they then attempt to popularize that poor understanding as being a failure in the idiom/technology rather than leaving it where the problem actually belongs. Myself I like null. Easier than coming up with a long list of magic values that represent the same thing as 'no value'.
-
Does anyone actually use .NET 7 with
enable
? I find this option makes life such a PITA that I always go into the .csproj file and disable it. The warnings are non-stop:Non-nullable property 'foo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable
Converting null literal or possible null value to non-nullable type.
Dereference of a possibly null reference.
Possible null reference assignment.
Nullable value type may be null.
And that's just a partial list, I believe. And that last one, "Nullable value type may be null" is the most amusing one. Seriously, if you have the "Nullable" option enabled, do you actually fix all those warning?
Latest Articles:
SVG Grids: Squares, Triangles, Hexagons with scrolling, sprites and simple animation examples
An SVG Analog ClockThat annoying nonsense started with .NET 6. I'm trying to be a good and obedient Microsoftie, leave Nullable enabled and deal with it in the code like they suggest. So I use truck loads of ! signs when I know good and well that it will not be null whether or not VS can see that I am checking for null values before referring to the object. The most annoying place it warns me of possible nulls is for string properties in a POCO. Seriously, MS, most of the time it doesn't make any difference whether or not those strings are null or not and warning me that they may be is useless and generates a false warning.
There are no solutions, only trade-offs.
- Thomas SowellA day can really slip by when you're deliberately avoiding what you're supposed to do.
- Calvin (Bill Watterson, Calvin & Hobbes) -
I found it rather jarring the very first time I built a .NET project using .NET 7 (something that had actually started life as a .NET Framework 4.8 project), so there were quite a few "fixes" to implement. But I came around rather quickly, and now kinda wished older versions were that restrictive/explicit.
You and others are convincing me that I should just bite the bullet and fix the code. ;)
Latest Articles:
A Lightweight Thread Safe In-Memory Keyed Generic Cache Collection Service A Dynamic Where Implementation for Entity Framework -
Does anyone actually use .NET 7 with
enable
? I find this option makes life such a PITA that I always go into the .csproj file and disable it. The warnings are non-stop:Non-nullable property 'foo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable
Converting null literal or possible null value to non-nullable type.
Dereference of a possibly null reference.
Possible null reference assignment.
Nullable value type may be null.
And that's just a partial list, I believe. And that last one, "Nullable value type may be null" is the most amusing one. Seriously, if you have the "Nullable" option enabled, do you actually fix all those warning?
Latest Articles:
SVG Grids: Squares, Triangles, Hexagons with scrolling, sprites and simple animation examples
An SVG Analog ClockWe do, and at first I hated it, but now I've embraced it. We find it cuts down on lots of null-ref opportunities. Sure, it can be annoying sometimes, but the occasional "!" on the end of a var, combined with the much nicer "if x is not null" makes the code very clear. Assuming you know what a trailing ! is...
cheers Chris Maunder
-
Some people seem to find some computer idioms and technologies difficult to understand. And then they then attempt to popularize that poor understanding as being a failure in the idiom/technology rather than leaving it where the problem actually belongs. Myself I like null. Easier than coming up with a long list of magic values that represent the same thing as 'no value'.
-
You and others are convincing me that I should just bite the bullet and fix the code. ;)
Latest Articles:
A Lightweight Thread Safe In-Memory Keyed Generic Cache Collection Service A Dynamic Where Implementation for Entity FrameworkMy first reaction was, "ugh, did they really do that?" followed by "am I really gonna have to propagate those fixes throughout?" If I would've had to do that throughout my entire codebase (everything I'm still maintaining), there's no way I would've done it. But starting with a small project that hadn't had much built into it yet...the changes were pretty quick and painless. I don't think I've ever adopted a language tweak this quickly. I'm all in. Greatest thing is, wherever I can provide a class instance as a param to a function, and it's *not* explicitly declared as nullable, then I'm guaranteed I don't have to bother with null checks. So I'm still trying to write all functions so nulls are (explicitly) *not* allowed.
-
We do, and at first I hated it, but now I've embraced it. We find it cuts down on lots of null-ref opportunities. Sure, it can be annoying sometimes, but the occasional "!" on the end of a var, combined with the much nicer "if x is not null" makes the code very clear. Assuming you know what a trailing ! is...
cheers Chris Maunder
Chris Maunder wrote:
Assuming you know what a trailing ! is
You nailed it on its ! head. Time to google. Technology keeps leaping forward and I am stumbling around trying to keep up while at the same time wondering why I'm trying to keep up. :laugh:
Latest Articles:
A Lightweight Thread Safe In-Memory Keyed Generic Cache Collection Service A Dynamic Where Implementation for Entity Framework -
Chris Maunder wrote:
Assuming you know what a trailing ! is
You nailed it on its ! head. Time to google. Technology keeps leaping forward and I am stumbling around trying to keep up while at the same time wondering why I'm trying to keep up. :laugh:
Latest Articles:
A Lightweight Thread Safe In-Memory Keyed Generic Cache Collection Service A Dynamic Where Implementation for Entity FrameworkI hear ya. Reminds me of the 2 day long conversation Matthew and I had about the extra negation in
if not "!arg_name:enableGPU=!" == "!arg_name!"
. I didn't see the double negation. He's pointing at his screen (over FaceTime) saying "there!". The issue was the "!" meant delayed expansion of a variable substitution in the Batch file, but to any reasonable developer it's "not arg_name". We imbue symbols with way too much meaning.cheers Chris Maunder
-
Does anyone actually use .NET 7 with
enable
? I find this option makes life such a PITA that I always go into the .csproj file and disable it. The warnings are non-stop:Non-nullable property 'foo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable
Converting null literal or possible null value to non-nullable type.
Dereference of a possibly null reference.
Possible null reference assignment.
Nullable value type may be null.
And that's just a partial list, I believe. And that last one, "Nullable value type may be null" is the most amusing one. Seriously, if you have the "Nullable" option enabled, do you actually fix all those warning?
Latest Articles:
SVG Grids: Squares, Triangles, Hexagons with scrolling, sprites and simple animation examples
An SVG Analog ClockAll the time, and I fix the warnings if they come up. It’s greatly reduced the number of null exceptions that get thrown. I’d prefer it though if they were reported as build failures by default.
Eagles may soar, but weasels don't get sucked into jet engines
-
Does anyone actually use .NET 7 with
enable
? I find this option makes life such a PITA that I always go into the .csproj file and disable it. The warnings are non-stop:Non-nullable property 'foo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable
Converting null literal or possible null value to non-nullable type.
Dereference of a possibly null reference.
Possible null reference assignment.
Nullable value type may be null.
And that's just a partial list, I believe. And that last one, "Nullable value type may be null" is the most amusing one. Seriously, if you have the "Nullable" option enabled, do you actually fix all those warning?
Latest Articles:
SVG Grids: Squares, Triangles, Hexagons with scrolling, sprites and simple animation examples
An SVG Analog ClockI have been using it on all of my work and hobby projects since it was introduced to .NET. #nullable #enable 🤣.
-
Does anyone actually use .NET 7 with
enable
? I find this option makes life such a PITA that I always go into the .csproj file and disable it. The warnings are non-stop:Non-nullable property 'foo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable
Converting null literal or possible null value to non-nullable type.
Dereference of a possibly null reference.
Possible null reference assignment.
Nullable value type may be null.
And that's just a partial list, I believe. And that last one, "Nullable value type may be null" is the most amusing one. Seriously, if you have the "Nullable" option enabled, do you actually fix all those warning?
Latest Articles:
SVG Grids: Squares, Triangles, Hexagons with scrolling, sprites and simple animation examples
An SVG Analog ClockAgree these warnings are pretty annoying in a project that didn't start out using nullable ref types correctly from the start. I have a large project in this situation, and fixing all these warnings is pretty daunting, requiring significant rework and refactoring. But I've come to see the value of using nullable ref types, so I've adopted it in smaller projects, and have been using it going forward. It's just not feasible to retrofit in all cases.
-
Does anyone actually use .NET 7 with
enable
? I find this option makes life such a PITA that I always go into the .csproj file and disable it. The warnings are non-stop:Non-nullable property 'foo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable
Converting null literal or possible null value to non-nullable type.
Dereference of a possibly null reference.
Possible null reference assignment.
Nullable value type may be null.
And that's just a partial list, I believe. And that last one, "Nullable value type may be null" is the most amusing one. Seriously, if you have the "Nullable" option enabled, do you actually fix all those warning?
Latest Articles:
SVG Grids: Squares, Triangles, Hexagons with scrolling, sprites and simple animation examples
An SVG Analog ClockYes, I use it, and I like it very much. Removed all possible NRE, and it's perfect to make sure juniors do not introduce tons of bugs. Sometimes a IF xx != null is enough, some other times we need "!", I hope the compiler could become smarter like Typescript is.