Working with Null in .NET 6 and C# 10

  Рет қаралды 36,542

IAmTimCorey

IAmTimCorey

Күн бұрын

Пікірлер: 131
@mcintoshdev
@mcintoshdev 2 жыл бұрын
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 жыл бұрын
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 2 жыл бұрын
@@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 жыл бұрын
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.
@ivetaivanova1600
@ivetaivanova1600 9 ай бұрын
Everything anyone would want to know about null in C# 6+ at one place, thank you so much for the great content :)
@IAmTimCorey
@IAmTimCorey 9 ай бұрын
You are welcome.
@mrsajjad30
@mrsajjad30 2 жыл бұрын
After watching this video, for me "What are nullables?" CHAPTER has been CLOSED. Thank you Mr. Tim Corey.👍
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Excellent!
@cisip
@cisip 2 жыл бұрын
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!!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
I am glad it was so helpful.
@stef2528
@stef2528 2 жыл бұрын
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!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@alex.rusnak
@alex.rusnak Жыл бұрын
Good tutorial on nullable things in C#! Will recommend to my team migrating big project to .NET 6.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Thank you!
@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.
@lucazl
@lucazl 2 жыл бұрын
Me: Is confused on a subject Tim: Uploads a tutorial on that subject
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
I am glad it was so helpful.
@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.
@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.
@sebastianweise4790
@sebastianweise4790 2 жыл бұрын
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!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@fanpeter-z3c
@fanpeter-z3c 2 жыл бұрын
thanks man , this is really an all-in-one tutorial that I have been looking for!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@iancarr3923
@iancarr3923 2 жыл бұрын
Thanks!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Thank you!
@maryambibi9687
@maryambibi9687 Жыл бұрын
Hi please make a tutorial full on c# basics in depth . I like your way of teaching style.
@MukundanMA
@MukundanMA 2 жыл бұрын
Love your videos as they are very comprehensive and very nicely explained. Thanks a lot.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@Terminator090909
@Terminator090909 2 жыл бұрын
Thanks
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Thank you!
@micro2743
@micro2743 2 жыл бұрын
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!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Thanks!
@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.
@Pierre1O
@Pierre1O 2 жыл бұрын
Know all this stuff but ??= was a surprise, thanks for the thorough handling.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@vssang4035
@vssang4035 Жыл бұрын
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 Жыл бұрын
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.
@АлександрБорисов-п7ъ
@АлександрБорисов-п7ъ 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😵
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@UmanPC
@UmanPC 2 жыл бұрын
Another great one. Tnx, Tim
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@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.
@caseyspaulding
@caseyspaulding 2 жыл бұрын
Thank you for making this video!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@emmanuelcyubahiro4415
@emmanuelcyubahiro4415 2 жыл бұрын
Thanks for the video Tim - You are the best!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@KouroushMetal
@KouroushMetal 2 жыл бұрын
Perfect demonstration Tim. Thank You!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@coding_refactor
@coding_refactor 2 жыл бұрын
Thanks I was confused about the nullable option and the operators
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@00wiking
@00wiking 2 жыл бұрын
Very good episode with lot of useful staff. Thanks a lot sir :)
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@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?
@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/
@Toughmenchannel
@Toughmenchannel Жыл бұрын
Thanks for making this video.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You are welcome.
@phileakins1407
@phileakins1407 2 жыл бұрын
Thanks Tim - Just what I needed!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@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.
@BenjaminKHelfman
@BenjaminKHelfman 26 күн бұрын
At the end of this video, you give an example of getting a value from a database and make the comment that the value from the database would not be null. Could it be though? From my limited understanding, some fields in a database can be null depending on the design. Correct? If so, could I use this to check for that condition?
@IAmTimCorey
@IAmTimCorey 23 күн бұрын
I believe in that specific case, the database column was marked as not null. But yes, you are correct that database values can be null if the design allows it to be. In that case, you would need to check for the null condition before using the property.
@refactorear
@refactorear 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.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
I'm glad you enjoyed it.
@mateuszspychaa3841
@mateuszspychaa3841 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 жыл бұрын
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!
@niklu4038
@niklu4038 2 жыл бұрын
Thank you for the explanation!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@jesusdelarua5995
@jesusdelarua5995 2 жыл бұрын
Great video. Loved it.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Thank you!
@abhishekbevinal7495
@abhishekbevinal7495 2 жыл бұрын
Thanks a lot tim, Really helpful...
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome!
@richardpearce2404
@richardpearce2404 2 жыл бұрын
Awesome content, thank you
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@josuemercally
@josuemercally 2 жыл бұрын
Thank you so much!!!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@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
@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 жыл бұрын
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 жыл бұрын
A solution I found that works for me is: public DbSet Authors => Set(); Not sure what the source was but it works quite well
@rafarvid
@rafarvid 2 жыл бұрын
Great info😊😊😊
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Thanks!
@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 жыл бұрын
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.
@pierreplourde
@pierreplourde 2 жыл бұрын
Great lesson, thank you!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@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.
@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 Жыл бұрын
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 :)
@ΠΑΝΑΓΙΩΤΗΣΜΗΤΡΟΠΑΝΟΣ
@ΠΑΝΑΓΙΩΤΗΣΜΗΤΡΟΠΑΝΟΣ 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 жыл бұрын
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)
@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/
@isen1
@isen1 2 жыл бұрын
very good , liked it . ... As allways!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Thank you!
@akangtoshi5085
@akangtoshi5085 2 жыл бұрын
Hello Tim can you please please make a video on signalR?
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
I already did. Here is part 1: kzbin.info/www/bejne/iJK7qZKcaMd8iLc and here is part 2: kzbin.info/www/bejne/Y3vdiJpjgNyobck
@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 жыл бұрын
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 жыл бұрын
@@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);
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Thank you!
@angmak7321
@angmak7321 Жыл бұрын
One day when i make it i buy you the best steak in town.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
I am glad it was helpful.
@RandalGJunior
@RandalGJunior 2 жыл бұрын
I use a ??= as a lazy loader for properties. private int? _i; public int i {get { return _i ??= doSomeCalc();}}
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Ah, that's a good option.
@mohammedalbosifi3975
@mohammedalbosifi3975 2 жыл бұрын
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/
@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 2 жыл бұрын
Before the video: ??, ?, null!;
@Aaron.Aguilar
@Aaron.Aguilar 2 жыл бұрын
after the video things i know now (s is not null), ??= nice
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
I am glad you learned something.
Intro to Records in C# 9 - How To Use Records And When To Use Them
1:19:44
Support each other🤝
00:31
ISSEI / いっせい
Рет қаралды 81 МЛН
Enceinte et en Bazard: Les Chroniques du Nettoyage ! 🚽✨
00:21
Two More French
Рет қаралды 42 МЛН
黑天使被操控了#short #angel #clown
00:40
Super Beauty team
Рет қаралды 61 МЛН
Delegates in C# - A practical demonstration, including Action and Func
1:09:11
C# Nullable reference types - No more null reference exceptions!
18:06
Intro to Regular Expressions - How to use Regex in C#
55:52
IAmTimCorey
Рет қаралды 41 М.
Manage Nulls Like a Boss and Never Fail!
21:43
Zoran Horvat
Рет қаралды 15 М.
Dependency Injection in .NET Core (.NET 6)
1:00:32
IAmTimCorey
Рет қаралды 196 М.
C# Null-Conditional Operator (? Operator) Explained!
13:26
Gerald Versluis
Рет қаралды 6 М.
How to Avoid Null Reference Exceptions: Optional Objects in C#
18:13
RECORD TYPES | Exploring C# and DOTNET | Rahul Nath
15:32
Rahul Nath
Рет қаралды 4,3 М.
What are record types in C# and how they ACTUALLY work
15:36
Nick Chapsas
Рет қаралды 123 М.