TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# StringBuilder Append

These C# examples show ways you can use the StringBuilder Append method.

Append method calls can be chained.

This makes your StringBuilder code more succinct and easier to read. Instead of using dozens of Append lines, we can have just one. And in some program contexts this is ideal.

StringBuilder Append Performance

Example. To start, we see how many developers use StringBuilder. Remember that StringBuilder often improves your application's performance, and is an important part of your tool belt as a developer.

In the second part, we combine multiple Append calls into a single statement. StringBuilder's Append() method returns a reference to itself. The designers of C# foresaw the problem with repetitive StringBuilder appends.

Note: Excessive method calls, on separate lines, are ugly and verbose—and thus prone to errors.

C# program that uses Append syntax

using System.Text;

class Program
{
    static void Main()
    {
	// Conventional StringBuilder syntax.
	const string s = "Value";

	StringBuilder builder = new StringBuilder();
	for (int i = 0; i < 1000; i++)
	{
	    builder.Append("One string ");
	    builder.Append(s);
	    builder.Append("Another string");
	}
    }
}

C# program that uses altenative Append syntax

using System.Text;

class Program
{
    static void Main()
    {
	// This is a fluent interface for StringBuilder.
	const string s = "Value";

	StringBuilder builder = new StringBuilder();
	for (int i = 0; i < 1000; i++)
	{
	    builder.Append("One string ").Append(s).Append("Another string");
	}
    }
}

Concat. Another option when you want to Append strings together is to use the plus operator. Note that this approach has different performance characteristics, but is useful in many situations when you are not looping over strings.

String AppendConcat

Summary. We saw some StringBuilder code. You can use this syntax to chain your StringBuilders in cases where you call Append multiple times. It retains the enormous performance advantage of StringBuilder.

And: It approximates the simple syntax of regular strings. This yields the best of both worlds.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf