c# - How to create a string from a char array

Create string from char array
The following asp.net c# example code demonstrate us how can we create a string object from a char array programmatically at run time in an asp.net application. Char array represent an array whose each element value represent a single Unicode character.

.Net framework's String Class has several constructors to initialize a new String Class (string object). String Class String(Char[]) constructor allow us to initialize a new instance of the String Class to the value indicated by an array of Unicode characters (char array). We just need to pass a char array object as a parameter of this constructor.

Finally, we can create a string object from a char array by this way String Newstring = new String(CharArray). The newly created string contains the value populated from specified char array values (Unicode characters).

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

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

        string txt = new string(chararray);
        Label1.Text += "<br /><br />char array to string: " + txt;
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - create string from char array</title>  
    <form id="form1" runat="server">  
        <h2 style="color:DarkBlue; font-style:italic;">  
            c# example - create string from char array
        <hr width="550" align="left" color="LightBlue" />    
        <br /><br />
            Text="create string from char array"  
More c# examples