
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
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
- Related Articles
- Java Program to Check if An Array Contains a Given Value
- Golang Program to Check if An Array Contains a Given Value
- Haskell Program to Check if An Array Contains a Given Value
- Java Program to Check if An Array Contains the Given Value
- Check if a value is present in an Array in Java
- Swift Program to Check if an array is empty
- C Program to check if an Array is Palindrome or not
- Swift Program to check if an Array is Palindrome or not
- Check if a value exists in an array and get the next value JavaScript
- How to check if a variable is an array in JavaScript?
- Is there any way to check if there is a null value in an object or array in JavaScript?
- Program to check if an Array is Palindrome or not using STL in C++
- C Program to check if an array is palindrome or not using Recursion
- Program to check if an array is sorted or not (Iterative and Recursive) in C
- How to check if a Perl array contains a particular value?

Advertisements