Hi guys i have a question which is : how to directly execute a function or delegate as well as returning a value from a method using one single Expressional return statement in C#
*For reference, I am using C#7
In this context I need to return to the user an Object which has been generated like this:
object obj = cmd.ExecuteScalar();
I also have a logging function which I want to call:
DoLog(cmd.CommandText)
PS: I am 100% aware that I can achieve this non-conditionally using a couple of if statements in a much easier way - i'm simply asking if anyone can provide me with the most elegant "one liner" approach possible.
Here is a standard example of how conditional return expressions usually work
return (someBool)
? value1
: value2;
this will return 'value1' if someBool is TRUE;
Basically my question is can i do something like this
return (chkStatus)
? (obj & DoLog(cmd.CommandText))
: null;
In response to some of you who have suggested that I can use a single pipe operator to split the return, while your suggestions are in good faith, I assume you never tried to compile them
If you had you would see that it doesn't work as the types: Object and Void are NOT directly comparable thus causing a compile time error.
My request for a more refined / elegant method still stands.