c# - How to check char array contains a char

Check char array contains a char
The following asp.net c# example code demonstrate us how can we determine/check whether a char array contains a specified character pogrammatically at run time in an asp.net application. .Net framework's char array each element value represent a Unicode character.

We can determine whether a specified character exists in a char array by using Contains() method. Enumerable.Contains() method allow us to determine whether a sequence contains a specified element by using the default equality comparer. This method exists in System.Linq namespace.

Contains() method need to pass two parameters named 'source' and 'value'. The 'source' parameter represent a sequence (array) in which to locate the 'value'. And the 'value' parameter represent the value to locate in the sequence.

Contains() method return a Boolean value. It return 'true' if the specified element is found; otherwise it return 'false'. We can call the method as this way to determine a character existence within a char array CharArray.Contains('Character').

<%@ Page Language="C#" AutoEventWireup="true"%>  
<!DOCTYPE html>  
<script runat="server">  
    protected void Button1_Click(object sender, System.EventArgs e)  
        char[] chararray = { 'a','b','c','d','e','f','g'};

        Label1.Text = "char array output.........<br />";

        foreach (char c in chararray)
            Label1.Text += c + "<br />";

        Boolean cExists = chararray.Contains('c');
        Boolean pExists = chararray.Contains('p');

        Label1.Text += "<br />[c] char contains in char array? " + cExists;
        Label1.Text += "<br /><br />[p] char contains in char array? " + pExists;
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# linq example - check char array contains a char</title>  
    <form id="form1" runat="server">  
        <h2 style="color:DarkBlue; font-style:italic;">  
            c# linq example - check char array contains a char
        <hr width="550" align="left" color="LightBlue" />    
        <br /><br />
            Text="check char array contains a char"  
More c# examples