-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't make a Not-Null Type Parameter Nullable in an Abstract Class #3101
Comments
You have to apply some attributes and provide an initializer: using System.Diagnostics.CodeAnalysis;
public abstract class Foo<T> where T : notnull
{
[MaybeNull, AllowNull]
public T Value { get; set; } = default!;
} |
See #3035. |
My use case is specifically for type parameters constrained to notnull; it looks like this proposal is about type parameters that are not constrained? |
That doesn't produce concrete classes the way I would expect.
I would expect the type of |
When using nullable types with generics in such a manner you have to constraint |
Ah, so you're looking for |
I do wonder if the compiler could do something different when T is constrained to If Then And
I could definitely do this on my own, but writing |
Pretty sure |
From what I gather, But.. a nice benefit of using |
Closing as now valid in C# 9 |
Wait what?? :O |
Oh I see. It's not invalid syntax anymore. ( It doesn't do what I was hoping for, though :( ) |
Came across this while trying to implement an abstract base class to process entities with various types of keys (int, long, guid, etc). I constrained my keys with In my particular case I was able to work around the issue. I changed my processing code from |
With the introduction of non-nullable reference types, I've hit a case a few times where I want to create a class like this one:
But I cannot because the compiler complains:
A nullable type parameter must be known to be a value type or a non-nullable reference type.
Non-nullable property 'Value' is uninitialized. Consider declaring the property as nullable.
Are there any plans to allow this? Am I missing something? I have my suspicions on why it was left out, but it feels like this could be allowed to some degree.
The text was updated successfully, but these errors were encountered: