Reputation: 37
I'm attempting to understand the syntax of a piece of code someone else wrote.
The method is returning a bool, and the return statement looks like this:
return user.UserStatus == Models.User.UserStatuses.Inactive && user.IsLocked;
UserStatuses is a enum.
So it appears to me it returns the property of object user called UserStatus, however UserStatus is an enum, not a bool, and then && adds the bool as user.IsLocked, where is a bool.
I can't seem to understand how this is legal in c#, since it appears to return two parameters.
Upvotes: 1
Views: 106
Reputation: 4883
It is just returning a boolean condition. Hypothetically, you could also check that same code on a IF statement like:
if(user.UserStatus == Models.User.UserStatuses.Inactive && user.IsLocked)
Upvotes: 0
Reputation: 4777
UserStatus is an enum but the base type of an enum is int. This is compared to the specific enum type. So the first part is basically (int == int) which yields a bool. The bool is then conditionally and'ed with the IsLocked value (bool && bool) to yield the final result.
Upvotes: -1
Reputation: 61379
Add some parenthesis, or separate your line into multiple statements, and it makes sense. The compiler is just doing it for you (more or less). Your statement is equivalent to:
return ((user.UserStatus == Models.User.UserStatuses.Inactive) && user.IsLocked);
or
bool inactive = user.UserStatus == Models.User.UserStatuses.Inactive;
bool inactiveAndLocked = inactive && user.isLocked;
return inactiveAndLocked;
The key here is that return is taking an expression (not a parameter) and using the result of that expression, which is just one "thing" as the C# spec dictates.
Upvotes: 4
Reputation: 15893
bool result = (user.UserStatus == Models.User.UserStatuses.Inactive) &&
user.IsLocked;
return result;
Upvotes: 0