c# - How to concatenate multiple strings

String concatenation
The following asp.net c# example code demonstrate us how can we concatenate string objects programmatically at run time in an asp.net application. We can concatenates string objects by various way.

Simply, we can use plus sign (+) to concatenates two or more string objects. If we have three strings, such as 'first string', 'second string' and 'third string', then we can concatenates them as this way "first string" + "second string" + "third string".

Next, we can concatenate string objects by using .Net framework's String Class String.Concat() method. This String.Concat() method concatenates one or more instances of String, or the String representations of the values of one or more instances of Object. This method has many overloaded members.

In this example, we uses String.Concat(String, String, String) overloaded method to concatenates three String instances. This overloaded method need to pass three String parameters to concatenates them.

<%@ Page Language="C#" AutoEventWireup="true"%>

<!DOCTYPE html>  
<script runat="server"> 
    protected void Button1_Click(object sender, System.EventArgs e)  
        //this section create string variables.
        string stringPlants = "Red Birch. River Birch.";
        string stringPlants2 = " Silver Birch. Spice Birch.";
        string stringPlants3 = " Sweet Birch.";
        Label1.Text = "string of plants..................<br />";
        Label1.Text += stringPlants;
        Label1.Text += "<br />" + stringPlants2;
        Label1.Text += "<br />" + stringPlants3;

        //this line concat 3 string.
        string all = string.Concat(stringPlants, stringPlants2, stringPlants3);
        //another way to concat strings
        //string all = stringPlants + stringPlants2 + stringPlants3;
        Label1.Text += "<br /><br />string after concat 3 string...........<br />";
        Label1.Text += all;
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - string concatenation</title>  
    <form id="form1" runat="server">  
        <h2 style="color:MidnightBlue; font-style:italic;">  
            c# example - string concatenation
        <hr width="550" align="left" color="Gainsboro" />  
        <br /><br />
            Text="string concatenation"  
More c# examples