As an option, you can use the default
literal in combination with the null forgiving operator
public class Item
{
public string Id { get; set; } = default!;
public string Name { get; set; } = default!;
public string Description { get; set; } = default!;
}
Since your DTO is populated from DynamoDB, you can use MaybeNull/NotNull
postcondition attributes to control the nullability
MaybeNull
A non-nullable return value may be null.
NotNull
A nullable return value will never be null.
But these attributes only affect nullable analysis for the callers of members that are annotated with them. Typically, you apply these attributes to method returns, properties and indexers getters.
So, you can consider all of your properties non-nullable ones and decorate them with MaybeNull
attribute, indicating them return possible null
value
public class Item
{
public string Id { get; set; } = "";
[MaybeNull] public string Name { get; set; } = default!;
[MaybeNull] public string Description { get; set; } = default!;
}
The following example shows the usage of updated Item
class. As you can see, second line doesn't show warning, but third does
var item = new Item();
string id = item.Id;
string name = item.Name; //warning CS8600: Converting null literal or possible null value to non-nullable type.
Or you can make all properties a nullable ones and use NoNull
to indicate the return value can't be null
(Id
for example)
public class Item
{
[NotNull] public string? Id { get; set; }
public string? Name { get; set; }
public string? Description { get; set; }
}
The warning will be the same with previous example.
There is also AllowNull/DisallowNull
precondition attributes for input parameters, properties and indexers setters, working on the similar way.
AllowNull
A non-nullable input argument may be null.
DisallowNull
A nullable input argument should never be null.
I don't think that it will help you, since your class is populated from database, but you can use them to control the nullability of properties setters, like this for the first option
[MaybeNull, AllowNull] public string Description { get; set; }
And for second one
[NotNull, DisallowNull] public string? Id { get; set; }
Some helpful details and examples of post/preconditions can be found in this devblog article
#pragma warning disable CS8618
at the top of the file. – Bikaner= ""
, you can use= null!
to initialize a property that you know will never effectively benull
(when the compiler has no way of knowing that). IfDescription
can legally benull
, it should be declared astring?
. Alternatively, if nullability checking for the DTO is more nuisance than help, you can simply wrap the type in#nullable disable
/#nullable restore
to turn off NRTs for this type only. – Brythonnull!
(e.g.public string Id { get; set; } = null!
) prevent that property from being subsequently assigned anull
value? Doesn't seem like it, but I want to be certain. – Eakinsnull
. They are still reference types that can have a null value. All it does is add compiler warnings to let you know that you are potentially using a null value where a non-null value is expected. But it should be noted that deserialization code may take the type into account and reject a null value where as it would not if nullable reference types are turned off. – Jordanna