- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# program to check if a value is in an array
Use the Array.Exists method to check if a value is in an array or not.
Set a string array −
string[] strArray = new string[] {"keyboard", "screen", "mouse", "charger" };
Let’s say you need to find the value “keyboard” in the array. For that, use Array.Exists() −
Array.Exists(strArray, ele => ele == "keyboard");
It returns a true value if element exists as shown below −
Example
using System; using System.Text; public class Demo { public static void Main() { string[] strArray = new string[] {"keyboard", "screen", "mouse", "charger" }; bool res1 = Array.Exists(strArray, ele => ele == "harddisk"); Console.WriteLine(res1); bool res2 = Array.Exists(strArray, ele => ele == "keyboard"); Console.WriteLine(res2); } }
Output
False True
Advertisements