Oliver Dixon Oliver Dixon - 14 days ago 6
C# Question

C# and nullable flag/annotation?

Does C# have Nullable annotation's?

For example in Java

public @Nullable <T extends BaseJsonClass> T getModel(String saveKey, Class<T> type) {


Now when the developer tries to call

Object.getModel().someMethod()


The compiler will warn them that they are not checking for null.

I'm aware of the
?
mark at the end of var names in C# like
someObject?
but the problem with this is that it can be tricky/annoying/JSON models when vars are being used in many places.

Answer

C# does not have anything like that - such feature is not a part of the language (yet).

However, if you use Resharper, you can utilize its Value and Nullability Analysis feature, which allows you to add several attributes to your code: [CanBeNull], [NotNull], [ItemCanBeNull], [ItemNotNull]. The nullability analysis performed by the plugin will take these attributes into account, resulting in the compile-time warnings you ask for.

Comments