开发者

How do i say is not, is not

i don't want to say:

(trsaz != v1) && (trsaz != v2) && ...

i want something like:

trsaz != (v1, v4, v7, v11)

Is this possible o开发者_运维百科r is there also something else besides !=.


var badList = new[] { v1, v4, v7, v11 };
var result = !badList.Contains(trsaz);


var excludeList = new[] { "v1", "v4", "v7", "v11" };
if(!excludeList.Contains(trsaz))
{
    ...
}


Actually, I prefer creating a little extension method for that:

public static bool IsIn<T>(this T obj, params T[] set) {
    return set.Any(el => element.Equals(obj));        
}

It encapsulates all the black magic and makes your code really concise which is your goal, obviously:

if (!trsaz.IsIn(v1, v4, v7, v11)) {
    // ...
}

It's always good to hide the mechanism if it's not important, especially in this case where the use of any mechanism is not necessary at all and will confuse some people who will maintain your code.


var result = (new[] {v1, v4, v7}).Every(o => o != trsaz);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜