Recently we started to use C# nullable reference types in our projects. Of course in our project, we use nuget libraries that don't support nullable reference types.
What is the best practice of using such libraries? I'm speaking of cases like this:
public class MyClass
{
public string myString { get; }
public MyClass(string myString)
{
this.myString = myString;
}
}
Property can be initialized with null value from external null-oblivious library. The analyzer does not warn you about this in any way. And code will throw NullReferenceException. That's why it seems to me that nullable reference types are a little bit useless. Previously we used guards to validate properties in the constructor like this:
public MyClass(string myString)
{
if (myString == null) throw new ArgumentNullException();
this.myString = myString;
}
Are there other defensive techniques and best practices to protect code from NullReferenceException?