5

In the below, C# is confident that 'property' will never be null, but I KNOW that it can be, because the external library is not produced with C#8/nullable types:

#nullable enable
SomeExternalClass obj = GetSomeExternalObjectFromAnOldAPI();
SomeExternalProperty property = obj.SomeProperty;
DoStuff(property); // C# is confident that property is not null, here

Here is a screenshot of the condition:

Failure 1

Doing the following doesn't provide a null warning either. C# still confidently assumes the external API is perfectly non-nullable:

#nullable enable
SomeExternalClass obj = GetSomeExternalObjectFromAnOldAPI();
SomeExternalProperty? property = obj.SomeProperty;
DoStuff(property); // C# is still confident that property is not null, here

Failure 2

Is there a way that I can force C# to acknowledge that a variable may be null, even if it is supposedly confident it won't be? The opposite of the ! symbol. 'Trust me, this garbage can be null..'

RESOLVED. ACCEPTED ANSWER:

#nullable enable
SomeExternalClass obj = GetSomeExternalObjectFromAnOldAPI();
SomeExternalProperty? property = obj.SomeProperty ?? null;
DoStuff(property); // C# is now warning about possible null
Triazic
  • 53
  • 4
  • Does this answer your question? [Can a non-nullable reference type in C# 8 be null in runtime?](https://stackoverflow.com/questions/59531048/can-a-non-nullable-reference-type-in-c-sharp-8-be-null-in-runtime) – CoolBots Apr 09 '20 at 02:13
  • 1
    Nope, I would like a syntax mechanism for forcing C# to acknowledge a possible null so that I get warned about it when consuming the value. Saying 'remember to do null checks, guys' defeats the purposes of the whole C#8 thing. Especially when VS explicitly tells me 'hey man, this will never be null' when it frequently is. False confidence and all that. Would be better off without the new features. – Triazic Apr 09 '20 at 02:33
  • don't be blaming c# when the problem is the external library. A library is supposed to report exceptions it can throw and return types. That's all c# has to work with. Note that you said in your question "the external library is not produced with C#8/nullable types:" which specifically is saying it can NOT be null. – John Lord Apr 09 '20 at 02:45
  • The ideal case would be that any external library not produced with the new C# syntax (I will frequently butcher the phrasing here, so trust we know what I am talking about) automatically attains ? at the end of every single function, method, return type, property, blah to reflect the fact that there are no guarantees from them. But I totally understand that this is probably not technically possible. I just wanted a mechanism to revoke the 'trust' C# was applying to the external library, which I now have. – Triazic Apr 09 '20 at 02:51

2 Answers2

1

Try:

ElementId? viewTemplateId = (revitView.ViewTemplateId ?? someNullTypeOfYourChoosing);

Alternatively, allow the View and revitView.ViewTemplateId structs to be nullable/return nullable types.

user544141
  • 90
  • 1
  • 6
  • ```SomeExternalProperty? property = obj.SomeProperty ?? null;``` succeeds in forcing C# to recognise a possible null. Cheers. – Triazic Apr 09 '20 at 02:36
  • Interesting. So what happens in this case if the referenced library is recompiled with `#nullable enable` and now guarantees no nulls? Does the behavior change, or is this method "overfit" for this specific case? – CoolBots Apr 09 '20 at 02:46
  • Not sure! Would be worth a test. My guess would be that nothing would change in terms of errors or warnings - the calling code would just be adding unnecessary null checks that never get executed at runtime. I know for a fact however that this particular API I'm referencing is not going to adopt ```#nullable enable``` anytime soon! They love returning nulls all over the shop in completely unexpected places. – Triazic Apr 09 '20 at 05:50
0

You can write a method ‘void MaybeNull([System.Diagnostics.CodeAnalysis.MaybeNull] T t)’ and use it.

Julien Couvreur
  • 4,473
  • 1
  • 30
  • 40