How do I cut if still

My code looks like

private bool IsUserAditya(string username) { return username == "Aditya"? true : false; } 

Is it possible to shorten it?

I would appreciate any help with this.

+4
source share
4 answers

Is it possible to shorten it?

Yes a little

return username == "Aditya";

Any comparison in C # returns a bool , so there is no need to use a conditional operator.

+6
source
 private bool IsUserAditya(string username) { return username == "Aditya"; } 
+3
source

Not directly related to abbreviation (even longer), but if you are comparing user input, such as username, use string.Equals , which takes a StringComparison object:

 private bool IsUserAditya(string username) { return username.Equals("Aditya", StringComparison.OrdinalIgnoreCase); } 
+1
source

Even shorter ...

 private bool IsUserAditya(string u){return u=="Aditya";} 

but only "shortens" the source code. The generated binary will be the same size.

0
source

All Articles