view raw
Oliver Dixon Oliver Dixon - 6 months ago 44
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


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
but the problem with this is that it can be tricky/annoying/JSON models when vars are being used in many places.


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.