Questions tagged [nullability]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
2 votes
4 answers
86 views

Is it possible to create a String extension `isEmptyOrNull` in dart?

First off let's define some criteria for this extension: It should return true if the object is null or an empty string. I should be able to use it on a chained nullable object. I should not have to ...
Joel Broström's user avatar
0 votes
0 answers
9 views

How do I resolve nullability of reference types in type of parameter warnings?

I'm getting a warning "Nullability of reference types in type of parameter 'obj' of 'lambda expression' doesn't match the target delegate (possibly because of nullability attributes)." Here ...
Johan Cloete's user avatar
0 votes
1 answer
49 views

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<string?> {"Cars", "Audi", &...
group's user avatar
  • 9
1 vote
1 answer
57 views

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 ...
user3227340's user avatar
-1 votes
1 answer
95 views

Vector of 'nullable' HashMaps in rust

I am finding extremely difficult to deal with vectors of HashMaps in rust. I want to use vectors of HashMaps as an implementation of a sparse matrix container according to constraints that I, ...
Redirectk's user avatar
  • 171
0 votes
0 answers
92 views

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 ...
nd_'s user avatar
  • 93
0 votes
0 answers
76 views

Warning [CS8622] caused by NotNullWhenAttribute

Is this a bug in the compiler, or am I missing something? public delegate bool Lambda<TState>([NotNullWhen(true)] out TState? state); class Foo { void Bar() { var lambda = new Lambda<...
Ed Pavlov's user avatar
  • 2,388
2 votes
1 answer
119 views

Making the C# compiler aware that a null-oblivious method may return null?

AutoMapper's T IMapper.Map<T>(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 ...
Kevin Krumwiede's user avatar
0 votes
1 answer
295 views

Stop nullability warnings from entity framework queries that include nullable navigation properties

Consider an entity framework core model with the following two entities: public class RiskAssessment { public string Title { get; set; } = string.Empty; public Guid? ActivityID { get; set; } ...
Kevin O'Donovan's user avatar
0 votes
2 answers
315 views

What is the difference between LiveData<String>() and LiveData<String?>()

I am new to LiveData thing in general and I am having a hard time understanding the difference between LiveData<String>() and LiveData<String?>(). I used them interchangeably and nothing ...
Khalida Aliyeva's user avatar
0 votes
1 answer
41 views

Is there a way to indicate to C# nullability analysis that an instance variable will never be null after a certain method runs?

I've got a class with an instance var that can be null, and that class has a method that initializes that var. Broadly simplified, it looks a bit like this #nullable enable class SomeClass { ...
newclearwinter's user avatar
0 votes
1 answer
197 views

Make the Intellisense nullability warning conditional, similar to Dictionary<string, string>.TryGetValue(...)

In Visual Studio, Intellisense is smart enough to know that the 'out' variable value in Dictionary<string, string>.TryGetValue(string key, out string value) is not null if the method returns ...
user3163495's user avatar
  • 2,685
3 votes
0 answers
581 views

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: https://learn.microsoft.com/en-us/dotnet/...
Programmable Physics's user avatar
1 vote
2 answers
127 views

Is it considered bad practice to use a "string?" parameter? [closed]

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==...
Urs Meili's user avatar
  • 750
0 votes
1 answer
969 views

Can't assign non-nullable type to a nullable one

error: The argument type 'Future<List<GalleryPictureInfo>>' can't be assigned to the parameter type 'Future<List<GalleryPictureInfo>>?'. Is this Dart Analysis or me? The ...
Selqet's user avatar
  • 1
0 votes
1 answer
60 views

Why does the compiler allow instantiation of generic class both with a nullable and non-nullable generic parameter?

I have a project in ASP.NET Core 6. I have the <Nullable>enable</Nullable> setting in the project. I have the following class: public class ResponseResult<T> { public T? Result{ ...
MiBuena's user avatar
  • 491
3 votes
0 answers
100 views

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 ...
mclayton's user avatar
  • 8,397
0 votes
2 answers
62 views

Difficulty defining nullability constraints

I have an extension function called TryGetValueAs which basically combines TryGetValue with a cast. The problem is that I keep getting nullability warnings and I can't seem to get it right. I have the ...
Rick de Water's user avatar
1 vote
1 answer
416 views

How to Determine Nullability (NRT) of Generic Type Parameter? [duplicate]

For example, I might have a method with the following signature: public async Task<ViewModel?> GetPersonUri() Using reflection I would like to determine if the type parameter of Task is a ...
Dejan's user avatar
  • 9,350
8 votes
2 answers
1k views

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,...
ruohola's user avatar
  • 22.2k
12 votes
3 answers
4k views

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 ...
Biasu's user avatar
  • 459
0 votes
4 answers
1k views

how to make thie non-pointer type property nullable in objc

hi iam learning IOS development & for learning porpoises i want to know how to make this property nullable in objc (this Four @property) @property (nonatomic) BOOL Hood; @property (nonatomic) BOOL ...
Saad's user avatar
  • 17
1 vote
1 answer
495 views

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: <Nullable>enable</...
Noggog's user avatar
  • 61
2 votes
3 answers
1k views

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 ...
Mark A. Donohoe's user avatar
-1 votes
1 answer
99 views

How to implement method accepting ICollection<T> and ICollection<T?>

Given the following method: public static void DisposeItems<T>(this ICollection<T?> collection) where T : class, IDisposable { foreach (var item in collection) { item?....
Manfred Brands's user avatar
2 votes
1 answer
761 views

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 ...
Anatolii Humennyi's user avatar
7 votes
1 answer
4k views

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 ...
Khan's user avatar
  • 195
1 vote
0 answers
75 views

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 ...
terrorrussia-keeps-killing's user avatar
3 votes
2 answers
3k views

Nullability warning for Task.FromResult

We have the following method in our code base (.NET Standard 2.0 library): public Task<T> GetDefaultTask<T>() { return Task.FromResult(default(T)); } We're currently trying to shift ...
D.R.'s user avatar
  • 20.5k