Questions tagged [nullability]
27 questions
10
votes
3 answers
Kotlin checkNotNull vs requireNotNull
As I learn new components in Kotlin, I came accross requireNotNull and checkNotNull but the only difference I've found is that requireNotNull can throw an IllegalArgumentException while checkNotNull can throw an IllegalStateException. Is this the…

Biasu
- 439
- 10
- 19
8
votes
2 answers
How to use TypeScript's indexed access types with nullable nested types?
I'm trying to define a TypeScript type in terms of another type.
This works:
type Result = { data: { nestedData: { foo: string; bar: string } } };
type NestedData = Result['data']['nestedData'];
But, when the data property is nullable, this…

ruohola
- 21,987
- 6
- 62
- 97
6
votes
1 answer
How to resolve error 'NotNullWhen attribute is inaccessible due to its protection level'
I have the following extension method and I'm trying to decorate the out parameter (T value) with the NotNullWhen attribute. However, it displays the error 'NotNullWhen attribute is inaccessible due to its protection level' that I can't figure out…

Khan
- 185
- 2
- 10
3
votes
0 answers
How to Solve Event Hander Nullable Warning Problem
button1.Click += new System.EventHandler(button1_Click);
For this code above the VS gives warning of CS8622 for "button1_Click" in the EventHandler:…

Programmable Physics
- 71
- 7
3
votes
0 answers
C# - nullability warning disappears when commenting out *seemingly* unrelated code
I've found some behaviour with the nullability analysis in a C# project that I don't understand, and I was hoping someone might be able to shed some light on it.
In short, I've got a nullability warning which I can't work out why it's appearing and,…

mclayton
- 8,025
- 2
- 21
- 26
3
votes
2 answers
Nullability warning for Task.FromResult
We have the following method in our code base (.NET Standard 2.0 library):
public Task GetDefaultTask()
{
return Task.FromResult(default(T));
}
We're currently trying to shift to C# 8.0 Nullability and get a warning in the code…

D.R.
- 20,268
- 21
- 102
- 205
2
votes
1 answer
Making the C# compiler aware that a null-oblivious method may return null?
AutoMapper's T IMapper.Map(object) is null-oblivious for historical reasons. If you pass null, the result is null. The C# compiler does not warn about the possible null return when returning the result of Map from a method declared to return…

Kevin Krumwiede
- 9,868
- 4
- 34
- 82
2
votes
1 answer
Why is there nullability warning for explicitly declared reference return value?
I enabled Nullability context for my test project and tried to fix all nullability warnings. I was able to fix them all except the below one which I don't understand (at return ref book; line). I pasted the warning in the comment at this line that…

Anatolii Humennyi
- 1,807
- 3
- 26
- 37
1
vote
1 answer
Nullability Check: Difference between literals and objects, or: "Why does "if(is null) [..]" work for object? but not for int?/long?/.. ?"
I stumbled across an interesting issue where I do not have any explanation for, hoping someone could explain.
When dealing with nullable objects (pseudo) code like the following works (Note: working snipped below):
object? obj =…

user3227340
- 23
- 4
1
vote
2 answers
Is it considered bad practice to use a "string?" parameter?
In a system where every customer has a unique e-mail address, there is the following code to retrieve the customer id by it's e-mail:
public int? GetCustomerIdByEMail(string? email) {
if(email==null) return null;
return…

Urs Meili
- 618
- 7
- 19
1
vote
1 answer
Visual Studio nullability ref errors show as warnings if defined in Directory.Build.props
This might just be a temporary VS bug, but I wanted to see if anyone had any fixes or if there was perhaps a VS setting I was missing.
If I define nullability errors:
enable
nullable
And I…

Noggog
- 61
- 5
1
vote
3 answers
How do you properly return `null` from a generic marked with [return:MaybeNull]?
In Microsoft's nullability documentation, there appears to be conflicting information.
On this page, it says the following (important part in bold/italic):
Generic definitions and nullability
Correctly communicating the null state of generic types…

Mark A. Donohoe
- 28,442
- 25
- 137
- 286
1
vote
0 answers
Can method return value nullability be inferred from the method argument nullability in JSR-305?
Suppose I have a method to parse a number and whose return value nullability contract should be inferred from its argument:
@...
public static Integer tryParse(@Nullable final String in, @... final Integer defaultValue) {
if ( in == null ) {
…

terrorrussia-keeps-killing
- 1,533
- 1
- 5
- 17
0
votes
1 answer
Filtering for Non-Nullable Strings in C# Lists
I'm trying to create a list of non-nullable string values by filtering, but I always end up with a list of nullable strings.
var list = new List {"Cars", "Audi", "", "1920"};
var list2 = list.Where(p =>…

group
- 9
- 2
0
votes
0 answers
Nullability of generic type arguments
So far I've worked with Nullable.GetUnderlyingType and even NullabilityInfo.WriteState to determine the nullability of types. This worked well - sometimes.
I'd like to know the nullability of the Dictionary generic type…

nd_
- 93
- 1
- 9