Oliver Dixon
Oliver Dixon

Reputation: 7405

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.

Upvotes: 3

Views: 4845

Answers (2)

pmbanka
pmbanka

Reputation: 1962

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.

Upvotes: 10

David Pilkington
David Pilkington

Reputation: 13620

In short, no.

But it is currently a proposal for future development

Proposal: Nullable reference types and nullability checking

Upvotes: 6

Related Questions