C
C#2y ago
GNUGradyn

❔ Appease "Property is uninitialized" without setting a default value? Possibly make it required?

Hello, I am trying to go through my code and fix all the warnings to improve code quality. I have this string property that must be set when this class is instantiated. It does not have a default value because it must be set every time when you create an instance of this class. Because of this I get this warning. Is there an annotation or something to tell the compiler "this property is required, and so this class cannot be instantiated without it" to appease the warning?
12 Replies
Angius
Angius2y ago
Make the property required
canton7
canton72y ago
If you can't do that because you're using an old compiler version, you can do = null!; to shut the compiler up
GNUGradyn
GNUGradynOP2y ago
required is not shutting it up but im on the latest version of rider
GNUGradyn
GNUGradynOP2y ago
ill try the = null solution
GNUGradyn
GNUGradynOP2y ago
technically worked but now its upset its redundant lol
GNUGradyn
GNUGradynOP2y ago
ill just make it an empty string by default even tho its required i guess
Angius
Angius2y ago
Not the attribute
public required string Name { get; set; }
public required string Name { get; set; }
GNUGradyn
GNUGradynOP2y ago
GNUGradyn
GNUGradynOP2y ago
.NET 7 feature mmaybe?
Angius
Angius2y ago
Yeah If you're using old versions of .NET, = null!; or = default!; is about as good as you can do
Anton
Anton2y ago
the best way is to ignore these warnings by putting a pragma over your code Rider has a fix for that
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server