Jak: wielu ciągów (Podręcznik programowania C#)
Konkatenacja jest dołączenie jeden ciąg na końcu ciągu innego procesu.Kiedy złączyć literały ciągów znaków lub stałe w postaci ciągów za pomocą + operatora, kompilator utworzy jeden ciąg znaków.Nie można uruchomić razem, kiedy nastąpi złączenia.Jednakże zmienne typu string, może zostać dołączona tylko w czasie wykonywania.W takim przypadku należy przeanalizować wpływ na wydajność różnych podejść.
Przykład
Poniższy przykład pokazuje, jak podzielić ciąg literału na mniejsze ciągi w celu poprawienia czytelności w kodzie źródłowym.Te części, będą łączone w jeden ciąg znaków w czasie kompilacji.Nie jest uruchamiany kosztów niezależnie od liczby zaangażowanych ciągi wydajności czasu.
static void Main()
{
// Concatenation of literals is performed at compile time, not run time.
string text = "Historically, the world of data and the world of objects " +
"have not been well integrated. Programmers work in C# or Visual Basic " +
"and also in SQL or XQuery. On the one side are concepts such as classes, " +
"objects, fields, inheritance, and .NET Framework APIs. On the other side " +
"are tables, columns, rows, nodes, and separate languages for dealing with " +
"them. Data types often require translation between the two worlds; there are " +
"different standard functions. Because the object world has no notion of query, a " +
"query can only be represented as a string without compile-time type checking or " +
"IntelliSense support in the IDE. Transferring data from SQL tables or XML trees to " +
"objects in memory is often tedious and error-prone.";
Console.WriteLine(text);
}
Aby złączyć ciąg zmiennych, można użyć + lub += podmiotów gospodarczych, lub String.Concat, String.Format lub StringBuilder.Append metody.+ Operator jest łatwy w użyciu i sprawia, że kod intuicyjne.Nawet jeśli używasz kilku + operatorów w jednej instrukcji, zawartość ciąg jest kopiowany tylko raz.Ale powtórzyć tę czynność wielokrotnie, na przykład w pętli, to może spowodować problemy wydajności.Uwaga Jeśli, na przykład, poniższy kod:
static void Main(string[] args)
{
// To run this program, provide a command line string.
// In Visual Studio, see Project > Properties > Debug.
string userName = args[0];
string date = DateTime.Today.ToShortDateString();
// Use the + and += operators for one-time concatenations.
string str = "Hello " + userName + ". Today is " + date + ".";
System.Console.WriteLine(str);
str += " How are you today?";
System.Console.WriteLine(str);
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
// Example output:
// Hello Alexander. Today is 1/22/2008.
// Hello Alexander. Today is 1/22/2008. How are you today?
// Press any key to exit.
//
[!UWAGA]
W operacji konkatenacji ciąg kompilator C# traktuje ciąg null takie same jak pusty ciąg znaków, ale nie konwertuje wartość oryginalnego ciąg null.
Jeśli nie są konkatenację dużej liczby ciągów znaków (na przykład w pętli), koszt wykonania niniejszego Kodeksu prawdopodobnie nie ma znaczenia.To samo dotyczy dla String.Concat i String.Format metody.
Jednakże, gdy wydajność jest ważne, należy zawsze używać StringBuilder klasy łączenia ciągów.Następujący kod dodaje do zastosowań Append metoda StringBuilder klasy do ciągów bez łańcuchowy efekt + operatora.
class StringBuilderTest
{
static void Main()
{
string text = null;
// Use StringBuilder for concatenation in tight loops.
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = 0; i < 100; i++)
{
sb.AppendLine(i.ToString());
}
System.Console.WriteLine(sb.ToString());
// Keep the console window open in debug mode.
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
// Output:
// 0
// 1
// 2
// 3
// 4
// ...
//