It seems to me there is really no guarantee that a non-nullable variable won't ever have null. Imagine I have a class that has one property that is not nullable:
public class Foo
{
public Foo(string test)
{
Test = test;
}
public string Test {get;set;}
}
Now that might seem like it's now cannot be null. But if we reference this class with another library that does not use nullable context, nothing stops it from sending null in there.
Is that correct or there are some runtime checks as well perhaps that ensure this?
Even in your own code, if you choose to do so, you can pass null
, using the null-forgiving operator. null!
is considered to be not-null so far as the compiler's nullability analysis is concerned.
This is what MS says about (https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/upgrade-to-nullable-references#interfaces-with-external-code):
The compiler can't validate all calls to your public APIs, even if your code is compiled with nullable annotation contexts enabled. Furthermore, your libraries may be consumed by projects that have not yet opted into using nullable reference types. Validate inputs to public APIs even though you've declared them as nonnullable types.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With