C#如果项目不在数组中 [英] C# If item is not in array

查看:127
本文介绍了C#如果项目不在数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含这三个项目的数组:

I have an array with these 3 items:

string[] departmentArray = {
    "Warranty Service Representative",
    "Warranty Service Administrative Manager",
    "Warranty and Site Administrator"
};

我有这个字符串

var department = "Warranty Service Representative"

我有这个条件,可以测试字符串部门是否不在departmentArray

I have this condition that is suppose to test if the string department is not in the departmentArray

if (Array.Exists(departmentArray, element => element != department)){
}

很明显,字符串在数组中,因此它应该返回false,但是对于我的字符串,它返回true.我在做什么错了?

Clearly the string is in the array, so it should return false, but this returns true for my string. What Am I doing wrong?

推荐答案

这会更简单吗?

string[] departmentArray = { 
    "Warranty Service Representative", 
    "Warranty Service Administrative Manager", 
    "Warranty and Site Administrator" };

String department = "Warranty Service Representative";

if (departmentArray.Contains(department) == false)
{
}

这篇关于C#如果项目不在数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆