Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. .NET and the nullable configuration

.NET and the nullable configuration

Scheduled Pinned Locked Moved The Lounge
csharpcomhelpquestionworkspace
29 Posts 18 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Marc Clifton
    wrote on last edited by
    #1

    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

    R P D L L 15 Replies Last reply
    0
    • M Marc Clifton

      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

      R Offline
      R Offline
      raddevus
      wrote on last edited by
      #2

      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.

      G 1 Reply Last reply
      0
      • R raddevus

        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.

        G Offline
        G Offline
        GuyThiebaut
        wrote on last edited by
        #3

        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

        R 1 Reply Last reply
        0
        • G GuyThiebaut

          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

          R Offline
          R Offline
          raddevus
          wrote on last edited by
          #4

          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. :)

          1 Reply Last reply
          0
          • M Marc Clifton

            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

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            I just never use nullable types. Or .net beyond 4.8 (I think).

            1 Reply Last reply
            0
            • M Marc Clifton

              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

              D Offline
              D Offline
              dandy72
              wrote on last edited by
              #6

              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.

              M 1 Reply Last reply
              0
              • M Marc Clifton

                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

                L Offline
                L Offline
                lmoelleb
                wrote on last edited by
                #7

                Yes, 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".

                1 Reply Last reply
                0
                • M Marc Clifton

                  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

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  I 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?

                  1 Reply Last reply
                  0
                  • M Marc Clifton

                    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

                    J Offline
                    J Offline
                    jschell
                    wrote on last edited by
                    #9

                    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'.

                    L 1 Reply Last reply
                    0
                    • M Marc Clifton

                      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

                      T Offline
                      T Offline
                      TNCaver
                      wrote on last edited by
                      #10

                      That 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 Sowell

                      A day can really slip by when you're deliberately avoiding what you're supposed to do.
                         - Calvin (Bill Watterson, Calvin & Hobbes)

                      1 Reply Last reply
                      0
                      • D dandy72

                        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.

                        M Offline
                        M Offline
                        Marc Clifton
                        wrote on last edited by
                        #11

                        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

                        D P 2 Replies Last reply
                        0
                        • M Marc Clifton

                          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

                          C Offline
                          C Offline
                          Chris Maunder
                          wrote on last edited by
                          #12

                          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

                          M 1 Reply Last reply
                          0
                          • J jschell

                            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'.

                            L Offline
                            L Offline
                            lmoelleb
                            wrote on last edited by
                            #13

                            No one is suggesting you should not use null. The nullable check makes it explicit where you intend to use it - just as it already is for value types.

                            J 1 Reply Last reply
                            0
                            • M Marc Clifton

                              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

                              D Offline
                              D Offline
                              dandy72
                              wrote on last edited by
                              #14

                              My 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.

                              1 Reply Last reply
                              0
                              • C Chris Maunder

                                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

                                M Offline
                                M Offline
                                Marc Clifton
                                wrote on last edited by
                                #15

                                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

                                C 1 Reply Last reply
                                0
                                • M Marc Clifton

                                  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

                                  C Offline
                                  C Offline
                                  Chris Maunder
                                  wrote on last edited by
                                  #16

                                  I hear ya. Reminds me of the 2 day long conversation Matthew and I had about the extra negation inif 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

                                  1 Reply Last reply
                                  0
                                  • M Marc Clifton

                                    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

                                    D Offline
                                    D Offline
                                    dazfuller
                                    wrote on last edited by
                                    #17

                                    All 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

                                    1 Reply Last reply
                                    0
                                    • M Marc Clifton

                                      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

                                      V Offline
                                      V Offline
                                      Vaso Elias
                                      wrote on last edited by
                                      #18

                                      I have been using it on all of my work and hobby projects since it was introduced to .NET. #nullable #enable 🤣.

                                      1 Reply Last reply
                                      0
                                      • M Marc Clifton

                                        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

                                        A Offline
                                        A Offline
                                        Adam ONeil Travelers Rest SC
                                        wrote on last edited by
                                        #19

                                        Agree 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.

                                        1 Reply Last reply
                                        0
                                        • M Marc Clifton

                                          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

                                          R Offline
                                          R Offline
                                          rallets
                                          wrote on last edited by
                                          #20

                                          Yes, 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.

                                          J 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups