Coding Shorts: Nullable Reference Types: Or, Why Do I Need to Use the ? So Much!

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ก.ค. 2024
  • As the C# language matures, Microsoft has now made Nullable Reference Types the default in new projects. It might be confusing why this was done. Let's find out:
    00:00 - Introduction
    00:32 - Creating a Project
    01:02 - Before Nullable Reference Types
    03:45 - Using Nullable Reference Types
    09:16 - Nullable Reference Types in Generics
    12:01 - Wrapping Up
    Source code: github.com/shawnwildermuth/co...
    If you like this video, you might like other videos in my Instructional Videos:
    - • Instructional Videos
    You can hire me too! You can reach me at my new website:
    - shawn.wildermuth.com
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 53

  • @arttrenton4467
    @arttrenton4467 ปีที่แล้ว +6

    Shawn, you did an outstanding job of clarifying this new feature. I was struggling to understand why this was done. It now make sense to me. Thanks 😊

  • @hozmannew896
    @hozmannew896 ปีที่แล้ว +1

    you have unusual way to simplify anything in a minute, great explanation as usual, thank you so much

  • @arjanvandenberg5313
    @arjanvandenberg5313 ปีที่แล้ว

    Brilliantly explained in such a short way! Thanks a lot for your educational lessons 😊

  • @TheDrewCrawford
    @TheDrewCrawford ปีที่แล้ว

    This information was exactly what I was looking for. Greate stuff Shawn. Thanks for sharing your knowledge.

  • @nalcora7389
    @nalcora7389 ปีที่แล้ว

    Extemely well done video, full and concise; have watched many video tutorials over the years both on YT, Pluralsight, etc; format is excellent.

  • @imaginative-monkey
    @imaginative-monkey ปีที่แล้ว

    Thanks for the video! 👍 Sometime for new projects, I add Nullable to the csproj, so the code won't compile unless I fix nullability issues. This makes me understand the concept better.

  • @lasermouth
    @lasermouth 10 หลายเดือนก่อน

    Amazing video! The only video that cleared up for me why there's such a thing as nullable ref types. In short, it was to eliminate ambiguity by forcing an explicit declaration of intent. I also enjoyed the latter half where you went into the operators and the generic example.

    • @swildermuth
      @swildermuth  10 หลายเดือนก่อน

      Glad it helped!

  • @robby-de-laet
    @robby-de-laet ปีที่แล้ว +1

    Well, that was a clear explanation. Thanks Shawn.

  • @enigma_dev
    @enigma_dev 8 หลายเดือนก่อน

    Amazing video, thanks for putting it together!

  • @sschulze2891
    @sschulze2891 ปีที่แล้ว

    Thanks for the clear explanation on this. One thing that might be missing is that you can also get ris of the "may be null" warning by performing a null check somewhere before in the code.
    To me the addition of nullabel reference types possibly had the biggest impact of all newer features and i would never deactivate it on a new project now.
    Migrating old code can be a pain though ...

  • @swordblaster2596
    @swordblaster2596 ปีที่แล้ว

    most important change in the last 5 years. Paying attention to this as the tools are trying to make you do, massively improves your design.

    • @jonb8869
      @jonb8869 3 หลายเดือนก่อน

      How? Can you elaborate on this?

  • @jasonazevedo1983
    @jasonazevedo1983 9 หลายเดือนก่อน

    Your content is amazing.. well done! don't stop!!

  • @sadhappy8860
    @sadhappy8860 5 หลายเดือนก่อน +1

    Great video, really helped me thank you. Although the word null kept going funny in my head. Haha

  • @onedev7316
    @onedev7316 ปีที่แล้ว +1

    Nice one. Thank you

  • @montana3426
    @montana3426 ปีที่แล้ว +1

    Thanks for great video!

  • @prateektomar4874
    @prateektomar4874 2 หลายเดือนก่อน

    Nice explaination.

    • @swildermuth
      @swildermuth  2 หลายเดือนก่อน

      Glad you liked it

  • @kraadhithya9841
    @kraadhithya9841 ปีที่แล้ว +1

    good explanation

  • @a-s733
    @a-s733 ปีที่แล้ว +1

    short and clear

  • @jedjohan
    @jedjohan ปีที่แล้ว +1

    Thanks, very good stuff. What would you say are the benefits, for developers and the applications we build. I know you did mention it in the end, but I somehow got the impression of "just do it" rather than "do it so that your code will ..."

    • @swildermuth
      @swildermuth  ปีที่แล้ว +2

      I think the benefit is more transparency to what you expected. By specifying nullability, you're telling the developer after you what you expect. Whereas before Nullable Reference Types, I didn't have a way of saying "This will never be null".

    • @jedjohan
      @jedjohan ปีที่แล้ว +1

      @@swildermuth I also like that the code, when in runtime, will not (hopefully) get "null ref"-issues. Or am I misunderstanding ?

    • @swildermuth
      @swildermuth  ปีที่แล้ว

      @@jedjohan That's true (harder to get null ref issues, but not impossible)

  • @semuhphor
    @semuhphor ปีที่แล้ว

    Cool channel. Thanks. I have been uneducated about how this feature works. Now I ain't. :D

  • @coderider3022
    @coderider3022 4 หลายเดือนก่อน

    I switched to using result / option / elevated types from the functional world in my c# and never looked back. Nullable and handling of them was a terrible idea for c#. I get its backward compatibility but time to evolve.

    • @swildermuth
      @swildermuth  4 หลายเดือนก่อน

      Immutability and nullability are not the same issue. But I'm glad it is working for you.

  • @redcrafterlppa303
    @redcrafterlppa303 ปีที่แล้ว

    It's funny how c# is now doing the same thing java did with generics decades ago. Provide an optional "upgrade" to the new system and all old stuff is now a warning.
    And seeing that this "slow migration system" never completed in java we can assume that c# will have projects with nullabity and without for pretty much forever.

  • @MohammadKomaei
    @MohammadKomaei ปีที่แล้ว

    How to create project by n new instead of dotnet new? when I use it I get error: n : The term 'n' is not recognized as the name of a cmdlet

    • @swildermuth
      @swildermuth  ปีที่แล้ว +4

      Sorry, it is "dotnet new" - I just have a batch file that redirects to dotnet so I only have to type "n"

  • @georgepagotelis
    @georgepagotelis ปีที่แล้ว

    At 3:00 - "int? x = default" - the value is not 0 but null! (I was testing code as you were displaying it)
    Which brings me to my next question, why isn't it clear by the compiler what "default" value is!

    • @swildermuth
      @swildermuth  ปีที่แล้ว

      Null isn't the default for anything. Default for value types are typically 0 for numeric types.

  • @torrex4
    @torrex4 ปีที่แล้ว +1

    when I scaffold the database using EF Core I get a lot of warnings about nullable

    • @swildermuth
      @swildermuth  ปีที่แล้ว

      The scaffolding hasn't caught up with it. I don't have a good solution (adding the "#nullable disable" on every file isn't scalable really).

    • @swildermuth
      @swildermuth  ปีที่แล้ว

      What version of .NET Core are you using? You might need to get the latest version of EF Tools. It is handling the null-ness in my recent projects.

    • @torrex4
      @torrex4 ปีที่แล้ว

      ​@@swildermuth I am using .NET 7 and the dotnet-ef is 7.0.2 but in .NET 6 I also had the same warnings, I tried NullableReferenceTypes but not working in my case I can send more info if needed

  • @redcrafterlppa303
    @redcrafterlppa303 ปีที่แล้ว

    9:25
    In fact the code:
    var len = user.Name?.Length;
    len is of type int?
    var len = user.Name!.Length;
    len is of type int

  • @tecTitus
    @tecTitus 9 หลายเดือนก่อน

    in short. "nullable reference types" enabled makes reference types into "value types". the name "nullable reference types" is quite bad, defeats it's own purpose. They should have called it "non-nullable reference types"

  • @allanhouston22
    @allanhouston22 2 หลายเดือนก่อน

    C# is a Frankenstein language for a long time. Now this nullable stupity is made so that the code is easily read and used by the AI... I hate it

    • @swildermuth
      @swildermuth  หลายเดือนก่อน

      Not sure how AI plays into it. But you're able to not use nullable reference types. There seems to be a separation into two dialects of the language over the years. C# 13 is the maturation of including more functional ideas, but it's all opt-in. You don't have to use it if it's not serving your needs.

  • @user-mw4yp3jm1v
    @user-mw4yp3jm1v ปีที่แล้ว +3

    This feature adds unnecessary complexity to the c# language.

  • @SasanSalem
    @SasanSalem ปีที่แล้ว

    Great explanation