c# - How to create a string by repeating a character

String repeat character
The following asp.net c# example code demonstrate us how can we create a string by repeating a character programmatically at run time in an asp.net application. .Net framework's String Class represent text as a series of Unicode characters. We can initialize a new string object using several constructors.

String constructor String(Char, Int32) initialize a new instance of the String class to the value indicated by a specified Unicode character repeated a specified number of times. String(Char, Int32) constructor has two required parameters named 'c' and 'count'.

The 'c' parameter value data type is System.Char which represent a Unicode character to repeat. The 'count' property value represent the number of times specified character repeat. After passing both parameters value to this constructor, we get a string where a specified character repeated specified number of times.

String(Char, Int32) constructor throw ArgumentOutOfRangeException, if the 'count' parameter value is less than zero (0).

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

<!DOCTYPE html>  
<script runat="server"> 
    protected void Button1_Click(object sender, System.EventArgs e)  
        //this line create a string by repeated characters.
        string repeatedCharacters = new string('M',10);
        Label1.Text += "<br />string of repeated characters........<br />";
        Label1.Text += repeatedCharacters;
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - string repeat character</title>  
    <form id="form1" runat="server">  
        <h2 style="color:MidnightBlue; font-style:italic;">  
            c# example - string repeat character
        <hr width="550" align="left" color="Gainsboro" />  
        <br /><br />
            Text="string repeat character"  
More c# examples