In case you didn't know, you don't need to type constructors manually like you did at ~2:15, instead of ctor (parameterless constructor), you can use ctorf (constructor with parameters for fields) or for your case ctorp (constructor with parameters for properties.
I'm still not on board the nullable train. I understand its purpose and how to use it, but if feels like an extra dimension to think about when I'm trying to accomplish the real objective. Especially due to the existence of the null forgiving operator, where I now need to placate the compiler when it thinks something could be null but can't. It's all markup and ceremony distracting me from the greater mission. This required feature, adding new ceremony to the constructor scenarios feels more of the same. But I thank you Tim, because now I'll at least recognize it, if others end up adopting it!
The nullability design-time checks are designed to help improve your code quality. If you have a string that you use, but you don't validate that it is not null first, that could throw an exception. This system shows you all of the places items could be null, but where you are treating them as not null. Those are all potential exceptions in the future. That's what it is designed to do.
Thx, but i have question, What is the different between the required key word and the [Required] attribut in the DataAnnotation? It would be great to know the differents.
Most importantly, the required keyword makes code violations into compile-time errors instead of runtime errors, which is awesome for pre-squashing bugs.
Great Video!, i have a small API, and I will probably upgrade to dotnet 7 just for this feature alone! some of my constructor were very big and sometimes i forgot to include some properties in the constructor, this seems to me, way easier to avoid mistakes like that and to not have to write a lot of code, now i just need to test to see if my NuGet Packages are compatible with .net 7
Can you plz make tutorial about Reactive programming, and what is Observable, observer, Subject and SubjectBehaivor. it is hard to understand the code that is in other tutorial. Thx again.
Thanks for this. The other day, I started up an empty project to prototype something, noticed .net7 and figured I'd try it, and ran into this nullable warning throughout. Just ignored it, but good to know how to properly work with it! Edit** Do you think this was designed so that we no longer need to check for nulls? Or what does this benefit? We still need to check for (or convert) nulls should data be coming from a store or API, for example.
The benefit of the nullable warnings is to help you make explicit choices about nulls in your applications. When you turn it on in an existing application, you will see all of the places where the data could be null, but you aren't accounting for it. Addressing those issues gives you better code quality.
Oh no it doesn’t 😢 You can still write and editor will not tell you that in MyComponent there is [Parameter] public required int MyParameter {get; set;}. You will find out on runtime. What a wasted opportunity.
Very informative, however in a use case for using this with a REST api service, there are times where for numeric values or even GUIDs where a user may not provide the information ( In essence an underposting attack) how does that required feature work... does this do the same thing as it did before where it supplies whatever the default value is (so for an int it would default it to 0, which in some scenarios could be a valid value so we wouldnt want it to default to that.) I do feel like some of the issues with underposting could be made easier for developers to tackle but I am aware of at least one way to prevent the issues from happening. This is just about whether it makes that easier or not. This might also be more of an appropriate question against whether MVC's model binding process is trying to be too clever.
This isn't related to that. Required on properties and fields is for when a class is initialized in C#. What you are referring to is when a model is populated externally. For that, you would use the Required attribute on the property. In that case, what happens is that the value is either not posted to the API or in the case of an MVC form, it can be posted but the model state will be invalid. You check for the model state first in both of these cases before using the model's information. If they posted nothing to a required field, the model state is invalid and nothing should be trusted in that model's data.
Just an opinion on this: I don't know what the point of this feature in C# is... there's already a way to do this: require them in constructor arguments. It just bloats the language syntax and increases the learning curve for beginners in my opinion.
That's the point, to not have to have a monster constructor. I would rather use an object initializer with required properties than a constructor with (many) parameters, especially when the number of parameters is large i.e. 5+. In the end it's personal preference.
Not everything can use a constructor. ORMs typically use empty constructors to populate a model. Also, creating constructors simply to populate properties isn't a great solution in every case. What if you had six properties and only two were required. How do you set that up? A constructor that takes the two, but then the other four are optional? Or just take the two and let the caller mix the constructor call with object initializers? It makes for a messy setup. This is an option now, which allows for a choice of what is best for a given project rather than being forced into one way of doing things.
this feels like a serious mess. 1. this prevents using "partial" constructors to set some of the values and object initialization for the others, since ctors must be "all or nothing" in terms of which properties they set. 2. why doesn't the ctor know that it sets the properties? it seems like a weird miss that would happen when the declaration and definition are separated. 3. question - if you had removed the "nullable" option from the project definition, wouldn't you get this behavior for free because the member are not initialized?
1. Trying to set property values in two different constructors in two different locations using partial classes seems wrong. I can't figure out when that would be a wise solution. 2. That's a tooling fix that will probably come soon. 3. Nullable only gives you a warning. It is a recommendation rather than a requirement. Plus, nullable won't know that you are setting the values via the curly-brace option, so it will yell at us even if we are doing it "right".
Here's my take on this: the only thing you communicated was that it could have been different. That isn't "nice positive feedback". It is a drive-by attack on how I chose to structure the video. It assumes that I failed to properly evaluate what should be in the video. It assumes that I did not consider cutting out content. There are a lot of negative assumptions in your statement. While I wasn't angry, I wasn't going to just sit there and try to pick the good out of the complaint. To answer the heart of your issue, I disagree with you. I felt (and still do) that setting up the need for required and how they work would allow people to understand what gap this fills. I did this to reduce confusion.
Why do you always use Blazor in your teachings?! In case you're living under a rock Blazor isn't that widely used. Why don't you use something like React for frontend?
In this video, I use a Console application, not Blazor. As for why I use Blazor, it is because it is a great framework that uses C#. I do not, in fact, live under a rock. I do know that React is popular. What you don't seem to understand is that companies do not suddenly all pick a new framework right when it comes out. It takes time. Blazor is gaining in popularity. It also might not be as popular as a pure JavaScript framework because it is based upon C#. It was designed for C# developers who want a great web framework without switching languages. That leads me to my last point, which is that I cover C# development. Blazor is a C# framework. React is not. I don't cover it because doing so would mean covering a TON of additional things in preparation for covering React. People tend to think that the "best" framework is the one that is most popular. That's simply not true. In order to pick the best framework, it needs to be the best framework for your specific project. Otherwise, you are making a big mistake (which, as a consultant for a number of years, I had to pick up the pieces for numerous times).
@@IAmTimCorey I wish Blazor will be more popular than Javascript...but I don't think it will ever happen. I think Blazor will remain a niche thing. React was released in 2013 so it's not new.
I just wanted to let you know I am loving the 10 Minute series.
Excellent!
In case you didn't know, you don't need to type constructors manually like you did at ~2:15, instead of ctor (parameterless constructor), you can use ctorf (constructor with parameters for fields) or for your case ctorp (constructor with parameters for properties.
Fun. I didn't know about that one. I've used the quick action refactoring to do the same, but I'll check out the ctorf.
Nice! I use the "ctrl + ." shortcut and ctor snippet but didn't know about ctorf!
I didn't know about ctorf either! Learn something every day. Thanks!
I tried it and it didn't work out of the box. VS2022 community version.
This is definitely one of those i've waited for a while, no longer do I need a nugget package to do this.
Great!
Tim never disappoints, thanks for these 🙏🙏
You are welcome.
thanks Tim! This a feature we didnt we needed until now.
You are welcome.
I'm still not on board the nullable train. I understand its purpose and how to use it, but if feels like an extra dimension to think about when I'm trying to accomplish the real objective. Especially due to the existence of the null forgiving operator, where I now need to placate the compiler when it thinks something could be null but can't. It's all markup and ceremony distracting me from the greater mission. This required feature, adding new ceremony to the constructor scenarios feels more of the same. But I thank you Tim, because now I'll at least recognize it, if others end up adopting it!
The nullability design-time checks are designed to help improve your code quality. If you have a string that you use, but you don't validate that it is not null first, that could throw an exception. This system shows you all of the places items could be null, but where you are treating them as not null. Those are all potential exceptions in the future. That's what it is designed to do.
Thanks Tim. I think they have refocused improved security, privacy and validation in this dotNet version 7. I was watching your web api videos.
You are welcome.
Thx, but i have question,
What is the different between the required key word and the [Required] attribut in the DataAnnotation?
It would be great to know the differents.
The Required data annotation is for forms, so when you have a form bound to a model, the model can tell the form that a given field must be filled in.
it's also used for Swagger to know what is required when building an API, if I am not mistaken
Most importantly, the required keyword makes code violations into compile-time errors instead of runtime errors, which is awesome for pre-squashing bugs.
Great Video!, i have a small API, and I will probably upgrade to dotnet 7 just for this feature alone! some of my constructor were very big and sometimes i forgot to include some properties in the constructor, this seems to me, way easier to avoid mistakes like that and to not have to write a lot of code, now i just need to test to see if my NuGet Packages are compatible with .net 7
Sounds good.
This was a great explanation
Thanks!
Can you plz make tutorial about Reactive programming, and what is Observable, observer, Subject and SubjectBehaivor. it is hard to understand the code that is in other tutorial.
Thx again.
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/
Thanks for the nice explanation..
You are welcome.
Thanks for a very good video!
You are welcome.
Thank you!
You are welcome.
Thanks for this. The other day, I started up an empty project to prototype something, noticed .net7 and figured I'd try it, and ran into this nullable warning throughout. Just ignored it, but good to know how to properly work with it!
Edit** Do you think this was designed so that we no longer need to check for nulls? Or what does this benefit? We still need to check for (or convert) nulls should data be coming from a store or API, for example.
The benefit of the nullable warnings is to help you make explicit choices about nulls in your applications. When you turn it on in an existing application, you will see all of the places where the data could be null, but you aren't accounting for it. Addressing those issues gives you better code quality.
Great videos
Thanks!
Please someone tell me this works in Blazor Components
Oh no it doesn’t 😢 You can still write and editor will not tell you that in MyComponent there is [Parameter] public required int MyParameter {get; set;}. You will find out on runtime. What a wasted opportunity.
These are great!
Thanks!
This should work with EFCore to not need the [Required] Attribute anymore
No, those are different things. The Required attribute is still necessary.
Tnx Tim
You are welcome.
I think will be good if for records required will be default
So we create record by ctor or by object initializer
That would be a fundamental change to how records work, which would break existing code, though.
Very informative, however in a use case for using this with a REST api service, there are times where for numeric values or even GUIDs where a user may not provide the information ( In essence an underposting attack) how does that required feature work... does this do the same thing as it did before where it supplies whatever the default value is (so for an int it would default it to 0, which in some scenarios could be a valid value so we wouldnt want it to default to that.) I do feel like some of the issues with underposting could be made easier for developers to tackle but I am aware of at least one way to prevent the issues from happening. This is just about whether it makes that easier or not. This might also be more of an appropriate question against whether MVC's model binding process is trying to be too clever.
Its interesting how many comments have had replies and yet nothing on this one.
This isn't related to that. Required on properties and fields is for when a class is initialized in C#. What you are referring to is when a model is populated externally. For that, you would use the Required attribute on the property. In that case, what happens is that the value is either not posted to the API or in the case of an MVC form, it can be posted but the model state will be invalid. You check for the model state first in both of these cases before using the model's information. If they posted nothing to a required field, the model state is invalid and nothing should be trusted in that model's data.
You also can assign a default value to the autoproperty. I really do not see a clear need for the required keyword.
What should the default value of EmployeeId be? Some fields need to be populated with real values, not just starter values.
Just an opinion on this: I don't know what the point of this feature in C# is... there's already a way to do this: require them in constructor arguments. It just bloats the language syntax and increases the learning curve for beginners in my opinion.
That's the point, to not have to have a monster constructor. I would rather use an object initializer with required properties than a constructor with (many) parameters, especially when the number of parameters is large i.e. 5+. In the end it's personal preference.
Not everything can use a constructor. ORMs typically use empty constructors to populate a model. Also, creating constructors simply to populate properties isn't a great solution in every case. What if you had six properties and only two were required. How do you set that up? A constructor that takes the two, but then the other four are optional? Or just take the two and let the caller mix the constructor call with object initializers? It makes for a messy setup. This is an option now, which allows for a choice of what is best for a given project rather than being forced into one way of doing things.
Is there a way to deserialize to a required property?
When I use JsonSerializer.Deserialise() it throws a exception on the required property.
this feels like a serious mess.
1. this prevents using "partial" constructors to set some of the values and object initialization for the others, since ctors must be "all or nothing" in terms of which properties they set.
2. why doesn't the ctor know that it sets the properties? it seems like a weird miss that would happen when the declaration and definition are separated.
3. question - if you had removed the "nullable" option from the project definition, wouldn't you get this behavior for free because the member are not initialized?
1. Trying to set property values in two different constructors in two different locations using partial classes seems wrong. I can't figure out when that would be a wise solution.
2. That's a tooling fix that will probably come soon.
3. Nullable only gives you a warning. It is a recommendation rather than a requirement. Plus, nullable won't know that you are setting the values via the curly-brace option, so it will yell at us even if we are doing it "right".
I really appriepriate your content, but this one could be 2 minutes or less :D
You could have done this in one minute.
And you could have gone to the developer's blog and read about it yourself. It feels like you are just complaining about a free resource.
Here's my take on this: the only thing you communicated was that it could have been different. That isn't "nice positive feedback". It is a drive-by attack on how I chose to structure the video. It assumes that I failed to properly evaluate what should be in the video. It assumes that I did not consider cutting out content. There are a lot of negative assumptions in your statement. While I wasn't angry, I wasn't going to just sit there and try to pick the good out of the complaint.
To answer the heart of your issue, I disagree with you. I felt (and still do) that setting up the need for required and how they work would allow people to understand what gap this fills. I did this to reduce confusion.
ERROR When used required >>>: Missing compiler required member system.runtime.compilerServices.CompilerFeatureRequiredAttribute !!! Any Solution
Are you trying to use this in a .NET Framework or .NET 5/6 project?
.NET 4.8 WindowsForm App Last Version i have and play good with My Maui project Anyway, thank you for bringing it to my attention
Yep, that's the issue. This is only for .NET 7 and beyond.
Why do you always use Blazor in your teachings?!
In case you're living under a rock Blazor isn't that widely used.
Why don't you use something like React for frontend?
In this video, I use a Console application, not Blazor. As for why I use Blazor, it is because it is a great framework that uses C#. I do not, in fact, live under a rock. I do know that React is popular. What you don't seem to understand is that companies do not suddenly all pick a new framework right when it comes out. It takes time. Blazor is gaining in popularity. It also might not be as popular as a pure JavaScript framework because it is based upon C#. It was designed for C# developers who want a great web framework without switching languages. That leads me to my last point, which is that I cover C# development. Blazor is a C# framework. React is not. I don't cover it because doing so would mean covering a TON of additional things in preparation for covering React. People tend to think that the "best" framework is the one that is most popular. That's simply not true. In order to pick the best framework, it needs to be the best framework for your specific project. Otherwise, you are making a big mistake (which, as a consultant for a number of years, I had to pick up the pieces for numerous times).
@@IAmTimCorey I wish Blazor will be more popular than Javascript...but I don't think it will ever happen. I think Blazor will remain a niche thing. React was released in 2013 so it's not new.