Can someone tell me if this is valid code:
get
{
return string.IsNullOrEmpty(TopicID) ? null :
TopicID == "00" ? "All Topics" :
int.Parse(TopicID).ToString();
}
I want the get to return a null, return the words “All Topics” if it is “00” or return the number without leading zeros.
The code looks messy but I am not sure if there’s a more clean way for me to code it.
The compiler will tell you whether or not it’s valid code 🙂
Others may suggest using
ifstatements instead of the conditional operator. I’m happy enough with the conditional operator here, but I’d change the formatting for clarity:This is one example of this pattern:
I’ve found this pattern to be quite helpful and readable. The fact that it looks a little like F# is entirely coincidental 🙂