Working with Null in .NET 6 and C# 10

Поделиться
HTML-код
  • Опубликовано: 6 сен 2024

Комментарии • 129

  • @mcintoshdev
    @mcintoshdev 2 года назад +49

    I am a fairly advanced C#/C++ Software Engineer and I really enjoy your teaching style. You teach like you are speaking to a beginner and you are very professional, flexible and articulate. Plus I do not feel like I am being talked down to like some of the other you tube channels. Thanks again for a great video!

    • @IAmTimCorey
      @IAmTimCorey  2 года назад +4

      You're very welcome!

    • @efexzium
      @efexzium 2 года назад

      That’s the best way of teaching. That’s how I love to do it.

    • @stefano_schmidt
      @stefano_schmidt 2 года назад

      is there any chance to get a job and code with vsCode instead of Visual Studio? I'm a self-student, learning C#, and I don't really like using Ide. Cause of that, I'm gonna change the language I learning. (I know it sounds quite silly). What environment do you use?

    • @poloska9471
      @poloska9471 Год назад

      @@stefano_schmidt do you and however you want to approach programming that is how you will ultimately approach it, but imo - I advise against this mentality because if/when you do get a job, your employer may very well require you to use a certain set of technologies that you have no say in (including frameworks, languages, 3rd party services, and IDEs and SDKs, and potentially even operating systems). Some companies are relaxed on this while others are not, and etc… although it is a “to each his own” sort of thing, I highly recommend being as technology-agnostic as possible when learning and becoming a developer, and by that I mean you should be able to get down with whatever technology or tech-related task there is to accomplish an outcome… remember, software development is about problem solving, not about coding… code is merely the facilitation of the problem’s solution, not the fundamental activity for which you are necessarily employed. There is a famous saying in Russia about this “for a bad dancer, even his balls get in the way”.

  • @tosinakinyemi3948
    @tosinakinyemi3948 2 года назад +11

    To be honest at first I was like why should I watch 1 hour video on NULL but at last I am glad I did, cos it was insightful and it bring clarity to the subject. Thank you Tim.

    • @IAmTimCorey
      @IAmTimCorey  2 года назад

      I am glad it was so helpful.

  • @cisip
    @cisip Год назад +3

    This is awesome Tim! Big help for my migration to .Net6. I’ve been seeing a lot of null-related warnings and this answers a lot (if not all) of my questions. Thank you very much!!

  • @lucazl
    @lucazl 2 года назад +2

    Me: Is confused on a subject
    Tim: Uploads a tutorial on that subject

    • @IAmTimCorey
      @IAmTimCorey  2 года назад

      I am glad it was so helpful.

  • @ivetaivanova1600
    @ivetaivanova1600 5 месяцев назад

    Everything anyone would want to know about null in C# 6+ at one place, thank you so much for the great content :)

  • @mrsajjad30
    @mrsajjad30 2 года назад

    After watching this video, for me "What are nullables?" CHAPTER has been CLOSED. Thank you Mr. Tim Corey.👍

  • @stef2528
    @stef2528 Год назад

    Learned from you something new though beeing a 30 years professional just coming back to C# after a while. Your stye is awesome. Thank you!

  • @micro2743
    @micro2743 2 года назад +8

    Great Video, as Always! It is targeted at beginners, so for me it was a bit long and I skipped a lot. A shorter viodeo, just on the feaatures that are not in previous versions would have been better for me. I know you primarly target beginners, so this goes into a lot of detail they may actually need to knw. Agaain... Great Video... keep up the good work!

  • @KnowlegeStream
    @KnowlegeStream 2 года назад

    Thanks for your valuable video . i was recently started working on .Net 6 C# i came across these warning which were new to me. i did search and use only ? and ! operators. now i can do more and with confidence after this. Thanks once again.

    • @IAmTimCorey
      @IAmTimCorey  2 года назад

      You are welcome. I’m glad it was so helpful.

  • @alexrusnak7574
    @alexrusnak7574 Год назад

    Good tutorial on nullable things in C#! Will recommend to my team migrating big project to .NET 6.

  • @sebastianweise4790
    @sebastianweise4790 2 года назад +1

    Im slowly getting into c# in the next months and years(all im good at right now is PS). I will probably dig through alot of your videos, thank you alot for the effort and time you put into this!! It is indeed great times when it comes to soaking up knowledge and learning when there is a will! Thanks again, you rock!

  • @Terminator090909
    @Terminator090909 2 года назад

    Didn't know I can watch a video about nothing for an hour xD!
    Was wondering what they did with the IDE because all these null warnings were driving me crazy, thanks for a very well explanation and making better coders of us all!

    • @IAmTimCorey
      @IAmTimCorey  2 года назад

      I am glad it was so helpful.

  • @user-lw6jt1nt4k
    @user-lw6jt1nt4k 2 года назад +1

    thanks man , this is really an all-in-one tutorial that I have been looking for!

  • @pqners
    @pqners 2 года назад

    Great video! Refactored a lot of my actual project with ??= and the null forgiving operator. Can't wait for the next sale on your website I will buy a lot of courses.

    • @IAmTimCorey
      @IAmTimCorey  2 года назад

      I am glad it was so helpful.

  • @vssang4035
    @vssang4035 Год назад +1

    Great video Tim! I wish this (detailed explanation on Nulls) was included in your complete C# Masterclass. I find it frustrating bc I have to go through the Masterclass and then find YT videos to get more info on a topic (I know your classes teach in layers and not all at once). Maybe add links in your paid videos (in each chapter) which point to your free YT videos? Reagardless keep up the good work!

    • @IAmTimCorey
      @IAmTimCorey  Год назад +1

      I hear you, but the C# Mastercourse isn't about providing every single topic in full depth. In fact, there are a lot of places that you should go deeper. The purpose of the C# Mastercourse is to get you from knowing nothing (or wherever you are starting) to having a firm foundation in C# that you can both get a job with and also build upon to grow as a developer. There will always be more topics to learn.

  • @Pierre1O
    @Pierre1O Год назад

    Know all this stuff but ??= was a surprise, thanks for the thorough handling.

  • @swordblaster2596
    @swordblaster2596 Год назад

    That null forgiving operator is more trouble than it's worth IMHO, I'll never use it. However, having recently started a new project in net 7, the extra null warnings and suggestions are massively improving my code and making me think about the design in ways I wasn't doing before. The resulting code is clearly better and more resilient, huge improvement from MS in that area.

    • @IAmTimCorey
      @IAmTimCorey  Год назад

      I'm glad you are getting value out of it.

  • @maryambibi9687
    @maryambibi9687 Год назад

    Hi please make a tutorial full on c# basics in depth . I like your way of teaching style.

  • @user-lq3ye9yi1e
    @user-lq3ye9yi1e 2 года назад

    Special thanks for explaining about conditional operator! Never used it before and couldn't find answer with uncomplicated requests like "What means ? in .NET/C#?" just like you said😵

  • @MukundanMA
    @MukundanMA Год назад

    Love your videos as they are very comprehensive and very nicely explained. Thanks a lot.

  • @metalnotis
    @metalnotis 2 года назад

    Thank you Tim! Your videos are great and really helpful. I'm learning a lot from them. I'm not a software engineer nor do I work as such. I'm actually a sales rep. I wish you had posted this before last weekend. I started migrating one of my .net framework apps to net 6 on Saturday, and a lot of these warnings started to poppingup here and there. Actualy, everywhere. I've started changing some of my classes to get rid of these warnings, but still have a long way to go. So, a little bit late, but this video comes at the right time to help me out. Again, thank you very much Tim.

    • @IAmTimCorey
      @IAmTimCorey  2 года назад

      I am glad it will be helpful.

  • @coding_refactor
    @coding_refactor Год назад

    Thanks I was confused about the nullable option and the operators

  • @bovineox1111
    @bovineox1111 Год назад

    Thanks for this video. Only 22 minutes in, hoping to get some claity on how to work with this because I am not a massive fan of doing null checks everywhere. For he most part I'd rather expect it to not be null and fine, throw me an exception if it happens to be and I'll gladly catch that and see what went on. The only good reason to do a null check is if under normal circumstances something could be null AND you can continue, which is likely what you'd want to do. I really don't want to be checking for null and throwing an exception of any kind...

    • @IAmTimCorey
      @IAmTimCorey  Год назад

      The issue they are working to address is that unexpected null values can cause major problems in applications. In most cases, it is better to catch the issue and address it rather than just letting it crash the application. At the very least, you can log the issue before letting it crash.

  • @dasfahrer8187
    @dasfahrer8187 2 года назад

    Excellent explanations. Would love to see a follow-up to this covering "default" values.

    • @IAmTimCorey
      @IAmTimCorey  2 года назад

      Thanks for the suggestion. Please add it to the list on the suggestion site so others can vote on it as well: suggestions.iamtimcorey.com/

  • @KouroushMetal
    @KouroushMetal 2 года назад

    Perfect demonstration Tim. Thank You!

  • @caseyspaulding
    @caseyspaulding Год назад

    Thank you for making this video!

  • @UmanPC
    @UmanPC 2 года назад

    Another great one.
    Tnx, Tim

  • @shashankshetye197
    @shashankshetye197 Год назад

    As per reference type logic,
    string ab = "ABV";
    string cv = ab;
    ab = "NEW ab";
    Console.WriteLine(ab);
    Console.WriteLine(cv);
    Expected O/P:
    NEW ab
    NEW ab
    Actual O/P:
    NEW ab
    ABV

  • @Toughmenchannel
    @Toughmenchannel Год назад

    Thanks for making this video.

  • @mateuszspychaa3841
    @mateuszspychaa3841 2 года назад +2

    Hi Tim! Thanks for the video - I have a question about one detail. Is there any guideline for using "var" vs "new()", e.g.:
    PersonModel user = new();
    vs
    var user = new PersonModel();
    I'm sure it's opinion-based, and there's no real right or wrong here, but is there any way we could organize it in our heads? The fact that they're so close in purpose (conciseness) makes it hard to choose...
    Also, in conext of the video, what do you think about ArgumentNullException.ThrowIfNull(user);?

    • @IAmTimCorey
      @IAmTimCorey  2 года назад +2

      As you said, this is an opinion but I prefer PersonModel user = new() as opposed to using var. The reason why is because code is meant to be read by humans. Seeing what the type is in front is what we are used to in most cases. It also easily communicates what we are making. That's just my thoughts, though. As for the ThrowIfNull, that's a good call in some circumstances. Just make sure you can't handle the error directly instead.

    • @mateuszspychaa3841
      @mateuszspychaa3841 2 года назад

      @@IAmTimCorey Thank you for the great remarks!

  • @00wiking
    @00wiking 2 года назад

    Very good episode with lot of useful staff. Thanks a lot sir :)

  • @AxL28AxL
    @AxL28AxL 2 года назад

    how to create global field here to use by other methods and also how to create DI in a console app for the new Net Core?

  • @emmanuelcyubahiro4415
    @emmanuelcyubahiro4415 2 года назад

    Thanks for the video Tim - You are the best!

  • @refactorear
    @refactorear 2 года назад +2

    Cool recap!
    34:50 I mostly use it to set default values, especially in the builder pattern so your constructor never gets a null reference. So, if my builder has ConnectingWith(IClient client) but I don't specify it in the Build method I assign the client attribute as _client ??= new DefaultClient() or similar.
    50:20 And about Nullable maybe you could have mentioned that Nullable is equal to type? for primitive types without having to dive into T. We have some very old code that uses Nullable and which will hopefully be updated eventually.
    Personally I dislike the ! operator, it's kind of contradictory saying "This variable could be null but here it will never be". I also kind of dislike the fact that by the time you leave the constructor every attribute must be initialized, it asks me to initialize every attribute in a builder at the constructor even if the end user (or I) will override them before using them in the Build() method. Thankfully they removed the !! operator from the NET 7 / C# 11 specs too.

  • @phileakins1407
    @phileakins1407 2 года назад

    Thanks Tim - Just what I needed!

  • @eddiev9254
    @eddiev9254 2 года назад

    Great video Tim! The challenge I run into is that when working on a front-end client, I sometimes get null values from a back-end API, including for a boolean property. On the back-end they use a NoSQL which is fine with null values for any property. But on the front end I have checkboxes on the UI which require either a true or false. I'm going to give coalesing a try.

    • @IAmTimCorey
      @IAmTimCorey  2 года назад

      Yep, that sounds like the right solution. Although, it would be good to clean up your front-end where those values are generated so that it properly gives you all of the required values.

    • @eddiev9254
      @eddiev9254 2 года назад

      @ghost mall thanks Ghost. Yup I've been using a view model to handle the nulls for the bool items coming from the API and converting them to false. But I'm going to try the suggestion you provided.

    • @eddiev9254
      @eddiev9254 2 года назад

      @@IAmTimCorey Thanks Tim. The initial values come from a third party, then to our back-end. They allow for null values, where we cannot. I really liked the 'coalesing' portion of the video, since it seems much more streamlined than the view model method I've been using.

  • @niklu4038
    @niklu4038 2 года назад

    Thank you for the explanation!

  • @richardpearce2404
    @richardpearce2404 2 года назад +1

    Awesome content, thank you

  • @josuemercally
    @josuemercally 2 года назад

    Thank you so much!!!

  • @abhishekbevinal7495
    @abhishekbevinal7495 2 года назад

    Thanks a lot tim,
    Really helpful...

  • @jesusdelarua5995
    @jesusdelarua5995 2 года назад

    Great video. Loved it.

  • @pierreplourde
    @pierreplourde 2 года назад

    Great lesson, thank you!

  • @florent9555
    @florent9555 Год назад

    A reference type is expected to be null but now we have to tell that the compiler? Why should I tell that the compiler, I know that null is to be expected out of a reference type and I write my code around that? Sounds very strange to me, maybe I am confusing myself here.

    • @IAmTimCorey
      @IAmTimCorey  Год назад +1

      It is about expectations (assumptions) in your code. By marking an object as nullable, you are saying "I'm ok with this being null sometimes". The compiler will then make sure that you have the appropriate null checks everywhere that you use that object. If you forget a place, the compiler will warn you about it. If you don't mark it as nullable, the compiler will assume that you are expecting it to be not null everywhere. It will then check to be sure that the object always has a value. If there is a place where it might not have a value, it will warn you. Null reference errors are a big problem in applications. This system allows you to more easily check for those issues at design time.

    • @florent9555
      @florent9555 Год назад

      @@IAmTimCorey Thanks for the clarification :)

  • @iancarr3923
    @iancarr3923 2 года назад

    Thanks!

  • @rodrigo6459
    @rodrigo6459 2 года назад

    The "In-line If statement is called Ternary operator".. and yes.. dealing with nulls when you have LOTS of Models to work with dababases.. it CAN get very painfull!.. i've seen this issue dealed like:
    public int Id { get; set; }
    public string FirstName { get; set; } = null!;
    public string LastName { get; set; } = null!;

    • @IAmTimCorey
      @IAmTimCorey  2 года назад +1

      I'm not a huge fan of lying to the compiler, but I definitely get it. I would rather see the nullable disabled for that model.

    • @rodrigo6459
      @rodrigo6459 2 года назад +2

      @@IAmTimCorey indeed! Thanks to this video I changed all those = null!; to just a #nullable disabled for those entities. 👍

  • @JuliLukiKon
    @JuliLukiKon Год назад

    string[] explanations = {"first", "second", "third"};
    string? me = explanations.Perfection(every, single, one);

  • @daliangilbert
    @daliangilbert 2 года назад

    Thanks Tim!
    In ef core I often use:
    public DbSet Customers { get; set; } = null!;
    to avoid green underline, but may somebody have some other clue?

    • @IAmTimCorey
      @IAmTimCorey  2 года назад +4

      That's not the right solution. Here you are setting the value of the property to null and then telling the compiler it is not actually null. If the DbSet can be null, make it nullable: DbSet? Customers { get; set; }

    • @lowkey122
      @lowkey122 2 года назад +1

      A solution I found that works for me is: public DbSet Authors => Set();
      Not sure what the source was but it works quite well

  • @Terminator090909
    @Terminator090909 2 года назад

    Thanks

  • @user-kv5sd8fp5n
    @user-kv5sd8fp5n 2 года назад

    Hello tim!
    I have a question about the elvis operator ?. (null conditional operator) and null forgiving operator !.
    Are the following statements the same?
    Console.WriteLine(person?.Name);
    Console.WriteLine(person!.Name);

    • @IAmTimCorey
      @IAmTimCorey  2 года назад +1

      No. The first one says "if person is not null, get the Name property on it, but if person is null, don't do anything." (so it will print a blank line) The second one says "I'm sure the person object is not null so don't warn me that it might be null." (if it is null, it will throw an exception)

  • @brandonh5142
    @brandonh5142 2 года назад

    What about assigning default to the non-nullable reference types? For example, public string Name {get; set;} = default!; Would this still be considered a non-nullable reference type?

    • @IAmTimCorey
      @IAmTimCorey  2 года назад +2

      The default value is still null for a string. So setting it to default would be null. Using the null-forgiving operator would just be lying to the compiler.

  • @isen1
    @isen1 2 года назад

    very good , liked it . ... As allways!

  • @rafarvid
    @rafarvid 2 года назад

    Great info😊😊😊

  • @akangtoshi5085
    @akangtoshi5085 2 года назад

    Hello Tim can you please please make a video on signalR?

    • @IAmTimCorey
      @IAmTimCorey  2 года назад

      I already did. Here is part 1: ruclips.net/video/RaXx_f3bIRU/видео.html and here is part 2: ruclips.net/video/-JzWg-Kwu7g/видео.html

  • @ivandrofly
    @ivandrofly 2 года назад

    Can you explain the nullable generic list

    • @IAmTimCorey
      @IAmTimCorey  2 года назад

      Thanks for the suggestion. Please add it to the list on the suggestion site so others can vote on it as well: suggestions.iamtimcorey.com/

  • @angmak7321
    @angmak7321 Год назад

    One day when i make it i buy you the best steak in town.

  • @mohammedalbosifi3975
    @mohammedalbosifi3975 2 года назад +1

    Thank you... Pleas i want 1or 2 vedios about identity server 4

    • @IAmTimCorey
      @IAmTimCorey  2 года назад

      Thanks for the suggestion. Please add it to the list on the suggestion site so others can vote on it as well: suggestions.iamtimcorey.com/

  • @RandalGJunior
    @RandalGJunior 2 года назад

    I use a ??= as a lazy loader for properties.
    private int? _i;
    public int i {get { return _i ??= doSomeCalc();}}

  • @Ultimate_Luki
    @Ultimate_Luki 2 года назад

    I dont like the forgiving operator. The entire point of these changes is to make you aware of null values and this operator just says: ignore the warnings.

    • @IAmTimCorey
      @IAmTimCorey  2 года назад

      The compiler doesn’t always know best. That’s what it is for.

  • @Aaron.Aguilar
    @Aaron.Aguilar Год назад

    Before the video: ??, ?, null!;

    • @Aaron.Aguilar
      @Aaron.Aguilar Год назад

      after the video things i know now (s is not null), ??= nice

    • @IAmTimCorey
      @IAmTimCorey  Год назад

      I am glad you learned something.

  • @jennibgmailcom
    @jennibgmailcom 2 года назад

    Thanks