C
C#2y ago
Hugh

✅ Calling a nullable Action or Func

In my class I have the following:
private Action<bool>? _myAction;
private Action<bool>? _myAction;
Is there a way of calling it if it isn't null in a single line? Something like (and this doesn't work):
_myAction?(true);
_myAction?(true);
Similarly with a Func, can I do something like:
private Func<bool, bool>? _myFunc;

bool result = _myFunc?(true) ?? false;
private Func<bool, bool>? _myFunc;

bool result = _myFunc?(true) ?? false;
5 Replies
MODiX
MODiX2y ago
Angius#1586
REPL Result: Success
Func<int>? foo = () => 8;
var x = foo is {} f ? f() : 0;
x
Func<int>? foo = () => 8;
var x = foo is {} f ? f() : 0;
x
Result: int
8
8
Compile: 502.186ms | Execution: 72.240ms | React with ❌ to remove this embed.
Angius
Angius2y ago
This works
Saber
Saber2y ago
disgusting.
var foo = func?.Invoke(args) ?? default;
action?.Invoke(args);
var foo = func?.Invoke(args) ?? default;
action?.Invoke(args);
Angius
Angius2y ago
Ah, completely forgot about .Invoke() You're right
Hugh
HughOP2y ago
Ah - nice - Invoke sounds good to me Thanks
Want results from more Discord servers?
Add your server