Instead of writing something like this
if(param!=null){
string message = String.Format("Time: {0}, Action: {1}, Param: {2}" time,someaction,param)
}else{
string message = String.Format("Time:{0},Action:{1}" time,someaction);
can i write
string message = String.Format(("Time: {0}, Action: {1}, Param: {2}!=null" time,someaction,param)
No, but you can write this
string message = param != null
? String.Format("Time: {0}, Action: {1}, Param: {2}" time, someaction, param)
: String.Format("Time: {0}, Action: {1}" time, someaction);
It's called ternary operator and is a nice way to shorten if else statements.
In C# 6 you can shorten your String.Format
as well; for example
$"Time: {time}, Action: {someaction}, Param: {param}"
instead of
String.Format("Time: {0}, Action: {1}, Param: {2}" time, someaction, param)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With