当前位置: 动力学知识库 > 问答 > 编程问答 >

C# - Assigning the result of a boolean expression

问题描述:

I'm not sure if this is a feature that c# supports, but it is a construct that I use in javascript programming and I am hoping to use the construct with c# if it supports it (.net Framework 3.5).

I have the following working code:

if (user.Status == AccountStatus.Active) {

status = true;

} else {

status = false;

}

I would like to shorten it as follows:

status = (user.Status == AccountStatus.Active);

In Javascript this will assign the true / false result of the evaluation within the parenthesis. This appears not to work for me in c#, and results in a generic compilation error.

Is there an alternative way to achieve this in a way that is compact?

网友答案:

That should work fine.

As should:

status = user.Status == AccountStatus.Active;

Your syntax is correct.

网友答案:

I was able to successfully execute following

bool a = (1 == 0); //returns false

and

bool a = (1 == 1); //returns true
分享给朋友:
您可能感兴趣的文章:
随机阅读: