0

Is it possible to execute a function within a short statement if the statement is true?

Something like that:

myObject.subObject != null ?? Db.LoadReferences(myObject.subObject); // ORMLite function
Astrophage
  • 1,231
  • 1
  • 12
  • 38
  • 5
    What about `if (myObject.subObject != null) Db.LoadReferences(myObject.subObject);`? – Ackdari Feb 12 '21 at 13:50
  • Does this answer your question? [shorthand If Statements: C#](https://stackoverflow.com/questions/6073563/shorthand-if-statements-c-sharp) – Toni Feb 12 '21 at 13:51
  • 1
    Since C# 8: `myObject.subObject ??= Db...` – CodeCaster Feb 12 '21 at 13:51
  • @CodeCaster It seems, that OP doesn't need an assignment here – Pavel Anikhouski Feb 12 '21 at 13:53
  • 1
    @Pavel I highly doubt that, because if `myObject.subObject` is null and you pass it to a method without `ref`, the method can't do anything useful with it. Edit: or it's the other way around, then you're right. – CodeCaster Feb 12 '21 at 13:54

1 Answers1

2

yes:

if(myObject.subObject != null) Db.LoadReferences(myObject.subObject);

At just 1 single character more than your original.

Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • Mind the possibility to use [` is not null`](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-9#pattern-matching-enhancements) in C# 9 – Fildor Feb 12 '21 at 13:55