重复的“If语句”保存并重用结果

时间:2018-04-11 13:24:58

标签: c# if-statement razor save code-reuse

我在MVC(剃刀代码)的Visual Studio视图中。我有相同的bool“if statemant”在代码的不同区域中被多次使用。有人告诉我,我可以保存第一个if语句的结果并重用结果,所以我不必多次写“if语句”。

如何保存第一个if语句的结果并重用下面的结果?

var IsManager = someRole;
var IsTeamLead= someOtherRole;

if(IsManager || ITeamLead)
{
 //Veiew this resticted content on the application;
}

//Code;
//Code;

if(IsManager || IsTeamLead)
{
 //Veiew more resticted content on the application;
}

//Little more code;
//Little more Code;

if(IsManager || Is TeamLead)
{
 //Veiew this content too;
}

//Little more code
//Little more code
//Little more code

if(IsManager || IsTeamLead)
{
 //Veiew this content;
}

1 个答案:

答案 0 :(得分:1)

bool isManagerOrTeamLead = IsManager || ITeamLead;

则...

if(isManagerOrTeamLead)
{
 //Veiew this resticted content on the application;
}