My code looks like
private bool IsUserAditya(string username)
{
return username == "Aditya"? true : false;
}
Can I shorten it further?
I would appreciate any help on this.
Can I shorten it further?
Yes, a little bit
return username == "Aditya";
Any comparison in C# returns a bool
, so no need to use the conditional operator.
private bool IsUserAditya(string username)
{
return username == "Aditya";
}
Not directly related to shortening (properly even longer), but if you're comparing an input from a user, such as a username, use string.Equals
which takes a StringComparison
object:
private bool IsUserAditya(string username)
{
return username.Equals("Aditya", StringComparison.OrdinalIgnoreCase);
}
Even shorter...
private bool IsUserAditya(string u){return u=="Aditya";}
but that only "shortens" the source code. Generated binary will be same size.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.