C# Nullable reference types - No more null reference exceptions!

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ก.ค. 2024
  • Are you tired of null reference exceptions and unexpected crashes due to uninitialized variables? There’s a very handy feature in C# called nullable references types that will help you catch this before it becomes a problem!
    In this video we are exploring nullable reference types in C#. This is a great addition to the C# language!
    This language feature was first introduced in C# 8.0 and is a great way to find potential problems, before it becomes an issue!
    Enable nullable reference types in your project by modifying the csproj file.
    This video also includes details on how to use the C# 9.0 language features 'new expression' and 'init only setters'.
    Follow me on Twitter: @fekberg / fekberg
    Check out my courses on Pluralsight: pluralsight.pxf.io/d1MRK
    Office background by: unsplash.com/@sunday_digital
    Table of contents:
    00:00 Introduction
    00:33 Why do null reference exceptions occur?
    01:14 C# 9.0: 'new expression'
    04:29 Nullable Reference Types
    05:06 Nullable Reference Type Syntax
    05:21 Enable the Nullable Reference Types on the Project level
    05:56 Compiler Warnings
    06:33 Introducing a Nullable Reference Type
    09:07 More Compiler Warnings
    09:55 Damn it/Null forgiving operator
    12:39 C# 9.0: Init only setters
    15:34 Enable the Nullable REference Types using Directives
    16:04 Summary
    DISCLAIMER: This video and description contains affiliate links, which means that if you click on one of the links, I may receive a small commission.

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

  • @Mosern1977
    @Mosern1977 3 ปีที่แล้ว +11

    This is super useful to get into the compiler. I bet probably 80% of the exceptions I encounter are Null Reference Exceptions. Also I shoutout to a quick Yield video.

  • @Al-mansur1999
    @Al-mansur1999 11 วันที่ผ่านมา

    very informative, what a great Teacher

  • @uncommonbg
    @uncommonbg 2 ปีที่แล้ว

    Wow, your way of presenting information is just phenomenal.

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

    Thanks soooo much I needed to hear the first part where you have to use new to allocate an instance. Thanks for the info, your a life saver.

  • @Time21
    @Time21 3 ปีที่แล้ว +3

    You make me recheck all my projects and rethink about nullable types. Thanks for the info shared.

    • @FilipEkberg
      @FilipEkberg  3 ปีที่แล้ว

      Thanks for watching the video, glad you found it useful!
      If you can find even just one potential null reference exception it's a win!

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

      Right there with you, Nick

  • @GBG731
    @GBG731 2 ปีที่แล้ว

    thank you for a great presentation, it simplified and understanding the work with issue nullable and very handy with key features in C#9

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

    This was really great, thanks, man!

  • @jaswantbisane4848
    @jaswantbisane4848 26 วันที่ผ่านมา

    Like your content! Please add a video on Events and Delegates.

  • @niloo195
    @niloo195 3 ปีที่แล้ว +10

    Thanks for the great content. That would be more great if you get to "yield" later in another video. Cheers!

  • @spyros5528
    @spyros5528 3 ปีที่แล้ว

    Very good and clear explanation. Thank you.

  • @qwerty5689
    @qwerty5689 3 ปีที่แล้ว +11

    Hi, thank you for this great video. It's easy to understand 👍
    I also agree that your next vid should be the yield keyword :)

    • @FilipEkberg
      @FilipEkberg  3 ปีที่แล้ว

      Thank you for the kind words!

  • @MrLucki001
    @MrLucki001 3 ปีที่แล้ว

    You are providing very good explanations!!! Please publish more videos in the future.

    • @FilipEkberg
      @FilipEkberg  3 ปีที่แล้ว

      Thank you so much! I will, soon :)

  • @chandanpandey1847
    @chandanpandey1847 3 ปีที่แล้ว

    Thanks for delivering deep diving leacture 👍

  • @antonvdovin8104
    @antonvdovin8104 3 ปีที่แล้ว

    Thanks for explaining!

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

    Watched your async course on Pluralsight, best C# tutorial author and tutor!

    • @FilipEkberg
      @FilipEkberg  3 ปีที่แล้ว

      Thank you so much for those kind words!

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

    Very good video. Hope you make many more.

  • @johot516
    @johot516 3 ปีที่แล้ว

    Such a great video! Tack så mycket!

  • @TaliandEyal
    @TaliandEyal 2 ปีที่แล้ว

    Amazing explanation!

  • @r3ady150
    @r3ady150 2 ปีที่แล้ว

    I’m starting out with c# kinda and am going through a c# book and doing the activities when I seen an null warning and wanted to see what it meant and ways to fix it. Great vid

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

    Thanks a lot! very helpful!

  • @sonomirco3
    @sonomirco3 3 ปีที่แล้ว

    Great video super useful. It would be great to have a video on yield!!!

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

    Omg! Perfect. Not indian english. Thank you for the content!

  • @fazmercs532
    @fazmercs532 2 ปีที่แล้ว

    Great video.

  • @chyldstudios
    @chyldstudios 2 ปีที่แล้ว

    Nice video!

  • @imikhan83
    @imikhan83 3 ปีที่แล้ว

    Saved my life 🙌❤

  • @TheJessejunior
    @TheJessejunior 3 ปีที่แล้ว

    very good!

  • @fass3686
    @fass3686 2 ปีที่แล้ว

    thank you :)

  • @zankomag8322
    @zankomag8322 3 ปีที่แล้ว

    OMG, You make such a good content, I like you!

    • @FilipEkberg
      @FilipEkberg  3 ปีที่แล้ว

      Thank you so much 🙏
      More is on the way!!

  • @vibes6915
    @vibes6915 2 ปีที่แล้ว

    Greate video

  • @techrelated2417
    @techrelated2417 3 ปีที่แล้ว

    you are awsome!

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

    Hi Filip, great content and an outstanding presentation. I work as a trainer myself and use OBS for a lot of video training nowadays. What do you use for this awesome green screen effect in the video? Keep this great stuff coming also really like your Pluralsight courses! Thanks

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

      Thank you very much Sebastian! For the green screen and keying it, I use DaVinci Resolve. If you want GPU acceleration you can pay a one-time license fee, otherwise it's a free tool.
      I can't praise it enough, such a great software that "just works" :)
      I've used OBS as well, but it's two completely different tools in my opinion. I stitch everything together manually and do my edits in Resolve. What's even better is that if I badly light my green screen, which does happen, I can select multiple shades and tweak the keying until it's good enough.
      It's not the fastest process, but I quite enjoy the post processing part!
      Hope this helps

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

      @@FilipEkberg I agree that OBS and DaVinci Resolve are two completely different tools that serve two distinct purposes. I use OBS for my live video productions via Teams. Actually I use Resolve for my post production stuff as well. Great to see what results you achieve using it. I have also used Final Cut Pro as well as Premiere Pro for video processing. But currently Resolve is my favourite tool for this purpose as well. Thanks for letting me know what you use in your toolchain! It's those tools which make the process enjoyable ;-) Thanks again

  • @chammu6936
    @chammu6936 3 ปีที่แล้ว

    Thank you for the clear explanation .What is the use of #nullable restore and when to use it?

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

    Very clear thanks.
    Using Nullable Ref Types with EF POCO classes feels awkward, I've been suppressing it with the null forgivable operator, but maybe 'init' from C#9 will improve this?
    And yes for Yield

    • @FilipEkberg
      @FilipEkberg  3 ปีที่แล้ว

      Thanks for watching!
      'init' will just ensure that the property won't be changed after it's constructed, so probably wouldn't help in your case I'm afraid.
      Have you seen this: docs.microsoft.com/en-us/ef/core/miscellaneous/nullable-reference-types ? An interesting read on nullable reference types with EF.

  • @cchance
    @cchance 3 ปีที่แล้ว

    Thanks always forgot to enable this but should be using if(person is null) instead of ==

  • @mrmoinn
    @mrmoinn 3 ปีที่แล้ว +3

    we want a video on "yield" !!!

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

      Will do! Keep an eye out :)

  • @balamurugankalyanasundaram426
    @balamurugankalyanasundaram426 3 ปีที่แล้ว

    Good Video Filip. Can you explain about the #nullable restore and where to add the #nullable enable directive either before namespace or class when applying to the class? The indention was to understand the best practices.

    • @FilipEkberg
      @FilipEkberg  3 ปีที่แล้ว

      Thank you!
      If you are slowly refactoring a project I would add the #nullable enable directive in the file before the namespace. When you have completely refactored the project, I'd recommend enabling the setting in the project file instead, and remove "#nullable enable" from the files, to make it a bit more clean.
      Hope this helps!

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

    Null object pattern fixes all the nullable issues!

  • @Alhansy21
    @Alhansy21 2 ปีที่แล้ว

    Thanks for the amazing video, kindly how can I convert the nullable from warning to error?

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

      Thanks! You can enable "Treat Warnings as Errors" in the project properties!

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

    I yeild 😁

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

    Fuck, so much new things I haven't touched yet...

  • @Talk378
    @Talk378 3 ปีที่แล้ว

    I’d dig a video about yield

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

      In the pipeline! 😁
      Planned to get it released sooner, but some work got in the way! Stay tuned.

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

    Microsoft solves probles that created by his own. Not nullable references should exist since C# 1. And there is a lack of keyword which should enforce programmer to check whether method result is null or not null.

  • @kiranshetty8342
    @kiranshetty8342 2 ปีที่แล้ว

    Hi Filip . It’s a grt content . Thanks for that . I have an question on nullable types
    Where in memory are nullable types stored ?? Say for example : int? I = null ;
    Is it in stack or heap ? Pls explain me with example . Thank you in advance

    • @FilipEkberg
      @FilipEkberg  2 ปีที่แล้ว

      Reference types are stored on the heap.

    • @kiranshetty8342
      @kiranshetty8342 2 ปีที่แล้ว

      @@FilipEkberg sorry filip you didn’t get my question ryt . Pls have a second look

    • @FilipEkberg
      @FilipEkberg  2 ปีที่แล้ว

      @@kiranshetty8342 Nullable types are reference types, when they are instantiated they are stored on the heap. If it's null, you don't have an instance and there's no object allocated.

  • @TT-ud5gf
    @TT-ud5gf 3 ปีที่แล้ว

    What does the “two question marks” mean? What is it named?

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

      It's name is "null coalescing operator".
      Simply put, it's a way to check if everything on the left of ?? is null, for example:
      var name = person.Name ?? "Filip";
      If the property "Name" is null, the variable "name" will now contain the value "Filip". What ?? did was to check if person.Name was null, if it was it uses "Filip".
      The "old" way of writing this would be:
      string name;
      if(person.Name == null)
      {
      name = "Filip";
      }
      else
      {
      name = person.Name;
      }
      You can read more about it on the Microsoft docs: docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator
      Really handy language feature!

  • @crist2000a
    @crist2000a 3 ปีที่แล้ว

    At 12:28 something strange. Default value for string is string.empty. So I do not understand null reference warning.

    • @FilipEkberg
      @FilipEkberg  3 ปีที่แล้ว

      Default value for string is null

    • @crist2000a
      @crist2000a 3 ปีที่แล้ว

      @@FilipEkberg Run this and check if there will be null reference:
      class Program
      {
      static void Main(string[] args)
      {
      test t = new test();
      Console.WriteLine(":" + t.Str + ":");
      Console.ReadKey();
      }
      }
      class test
      {
      public string Str { get; set; }
      }

    • @FilipEkberg
      @FilipEkberg  3 ปีที่แล้ว

      @@crist2000a Try calling t.Str.Length and see what happens. Reference types in C# are null by default.

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

    "Object reference not set instance object" I hate this error

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

    If they recreated C# from scatch without nulls, I think there would be some another issue, caused by other workarounds that would have been required to deal with the root causes, which made them introduce nulls in the first place; and that another issue because of those workarounds would have costed just as much, that's my theory))

  • @user-qy6yn4kl8d
    @user-qy6yn4kl8d ปีที่แล้ว +1

    Pluralsight? Thought I'd recognized your voice. Good breakdown. Your low sub count doesn't reflect the quality of your content.

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

      Thank you, that is very kind! You are absolutely correct, I have a whole lot of courses in Pluralsight!

  • @yogxoth1959
    @yogxoth1959 2 ปีที่แล้ว

    Did you stop making videos?

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

      Unfortunately it's been a while since I put new content on TH-cam. I've got a lot of new content published on Pluralsight ( www.pluralsight.com/authors/filip-ekberg ) and I do have a long "todo" list with videos I want to make for this channel, but other things had to take priority for a while!

    • @yogxoth1959
      @yogxoth1959 2 ปีที่แล้ว

      @@FilipEkberg I understand. Thanks for replying! I enjoy your content.

  • @gibinoza8868
    @gibinoza8868 3 ปีที่แล้ว

    watched best at 1.5x

  • @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"

  • @user-sz3nn2lb6u
    @user-sz3nn2lb6u 2 ปีที่แล้ว

    why your head looks so big?

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

      Probably because the angle of the camera.

    • @user-sz3nn2lb6u
      @user-sz3nn2lb6u 2 ปีที่แล้ว

      @@FilipEkberg thank you for your great video!

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

      because he is very smart