C# Program to Demonstrate StringBuilder
Posted by Superadmin on August 13 2022 08:00:02

C# Program to Demonstrate StringBuilder

 

 

This is a C# Program to illustrate stringbuilder.

Problem Description

This C# Program Illustrates StringBuilder.

Problem Solution

Here ToString method is used on the StringBuilder type. This method is used to convert a StringBuilder’s character buffer into an actual string reference.

Program/Source Code

Here is source code of the C# Program to Illustrate StringBuilder. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Illustrate StringBuilder
 */
using System;
using System.Text;
class Program
{
    static void Main()
    {
        StringBuilder bd = new StringBuilder();
        bd.Append("1 ");
        bd.Append("2 ");
        bd.Append("3 ");
        for (int i = 0; i < 5; i++)
        {
            bd.Append("z ");
        }
        string result = bd.ToString();
        Console.WriteLine(result);
        Console.ReadLine();
    }
}
Program Explanation

This C# program is used to illustrate StringBuilder. The Append() method is used to append a copy of the specified string to this instance. Using for loop append the value of ‘z’ character value to the remaining values in for loop.

 

ToString() method is used on the StringBuilder type. This method is used to convert a StringBuilder’s character buffer into an actual string reference.

Runtime Test Cases
 
1 2 3 z z z z z