C# String Constructor

New keyword (constructor invocation)

String constructor calls are normally not needed. The C# language provides many string constructors for use in some situations. One possible use is creating a string containing only one character repeated many times. Another use is converting a character array into a string.

Tip: To create a new string, you usually should just modify an existing string or use a string literal.

Example

String type

Here we see the three safest and simplest string constructors. These can be used in any C# code, and are not unsafe. The example demonstrates making a string from a char array, from a series of repeated letters, and from a char array range.

This C# example program uses the string instance constructor. It creates new strings.

Program that uses string constructors [C#]

using System;

class Program
{
    static void Main()
    {
	@A();
	@B();
	@C();
    }

    static void @A()
    {
	//
	// Create new string from char array.
	//
	char[] charArray = new char[3];
	charArray[0] = 'a';
	charArray[1] = 'b';
	charArray[2] = 'c';

	string exampleString = new string(charArray);
	Console.WriteLine(exampleString);
	Console.WriteLine(exampleString == "abc");
    }

    static void @B()
    {
	//
	// Create new string of repeated characters.
	//
	string exampleString = new string('a', 10);
	Console.WriteLine(exampleString);
	Console.WriteLine(exampleString == "aaaaaaaaaa");
    }

    static void @C()
    {
	//
	// Create new string from range of characters in array.
	//
	char[] charArray = new char[6];
	charArray[0] = 'a';
	charArray[1] = 'B';
	charArray[2] = 'c';
	charArray[3] = 'D';
	charArray[4] = 'e';
	charArray[5] = 'F';

	string exampleString = new string(charArray, 0, 3);
	Console.WriteLine(exampleString);
	Console.WriteLine(exampleString == "aBc");
    }
}

Output

abc
True

aaaaaaaaaa
True

aBc
True
Note

Description of the program. The output of the program is shown in the bottom section. Each method displays the result string, and tests the string against a constant to see if the output is as expected, printing True.

Description of method @A. This method shows how you can use the string constructor that accepts a char[] array. This is an efficient way to perform this conversion.

Convert Char Array to String

@B. This method demonstrates the string constructor that creates a series of a single characters. This is much simpler than using PadRight or PadLeft methods to create repeated characters. The example creates a string of ten 'a' letters. Please see the optimization section below.

@C. Here, we see how you can create a string from a char array, as in method @A, but with only a range of the char array. The example takes the character in the array at index 0, and then adds three chars. This is the equivalent of Substring, but acts on char arrays.

Note: We used the @ symbol on the method names as a way to make them stand out. You can use @ to specify identifiers of any name.

Constructors

To continue, we look at the eight string constructors in the .NET Framework. You can see that five of the constructors are unsafe. The three other constructors are shown in the example above.

String constructors [C#]

unsafe public String(char*);
       public String(char[]);
unsafe public String(sbyte*);
       public String(char, int);
unsafe public String(char*, int, int);
       public String(char[], int, int);
unsafe public String(sbyte*, int, int);
unsafe public String(sbyte*, int, int, Encoding);

Unsafe string constructor methods. This article doesn't demonstrate the unsafe constructors, as your code shouldn't use unsafe methods unless absolutely necessary. You might need these five unsafe methods for DLL interop.

Programming tip

Tip: There is no difference between calling "new String" and "new string". The uppercase first letter makes no difference. The semantics (meaning) and performance are identical.

Optimizations

Performance optimization

You can use the string constructors as an impressive optimization. Sometimes, you can replace StringBuilder entirely. This is most useful for sorting algorithms or other lower-level operations on strings in C# programs.

Char Array Use StringBuilder Secrets

Single characters. I have found that using the string constructor that repeats characters N times (the second method @B above) can sometimes be optimized to a string literal. You can replace "return new string('a', 5');" with "return "aaaaa"".

Summary

The C# programming language

String constructors are occasionally very useful. We saw ways you can use the three string constructors in C# that are not unsafe. We looked at examples, and then examined the method signatures, discussed unsafe-ness, and found ways strings can be optimized with the constructors.

String Type
.NET