Aracılığıyla paylaş


Nasıl yapılır: birden çok dize (C# Programlama Kılavuzu) art arda eklemek

Bitiştirme , başka bir dizenin sonuna bir dize ekleme işlemidir.Ne zaman, arada dize hazır bilgi ya da dize sabitleri kullanarak + işleci, derleyici, tek bir dize oluşturur.Hayır çalışma zamanı bitiştirme oluşur.Ancak, dize değişkenleri yalnızca çalışma zamanında art arda eklenebilir.Bu durumda, çeşitli yaklaşımlar performans etkilerini anlamanız gerekir.

Örnek

Aşağıdaki örnek uzun bir dize literal daha küçük dizeleri kaynak kodundaki okunabilirliği artırmak için nasıl bölüneceği gösterilir.Bu parçaları tek bir dize içinde derleme zamanında birleştirilmiş.Hayır zaman performans dizeleri katılan sayısı ne olursa olsun maliyet çalıştırılır.

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);
}

Dize değişkenleri bağlamak için kullanabileceğiniz + veya += işleçleri, veya String.Concat, String.Format veya StringBuilder.Append yöntemleri.+ İşleci kullanımı kolay ve sezgisel kodunu getirir.Kullandığınız birkaç + bir ifade işleçleri, dize içerik bile yalnızca bir kez kopyalanır.Ancak bu işlemi birkaç kez yinelemeniz, örneğin bir döngüde verimlilik sorunları neden.Örneğin, aşağıdaki kod dikkat edin:

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.
//

[!NOT]

Dize bitiştirme işlemlerinde, C# derleyicisi boş bir dize aynı boş bir dize olarak değerlendirir, ancak özgün boş bir dize değeri dönüştürmez.

Çok sayıda dizeleri (örneğin bir daire) bitiştirme değil, bu kod performans maliyetini önemli olmayabilir.Aynı durum String.Concat ve String.Format yöntemleri.

Ancak, performansını önemli olduğunda daima StringBuilder dizeleri bitiştirmek için sınıf.Aşağıdaki kod Append yöntemi, StringBuilder zincirleme etkisi olmadan dizeleri bitiştirmek için sınıf + işleci.

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
// ...
//

Ayrıca bkz.

Başvuru

String

StringBuilder

Kavramlar

C# Programlama Kılavuzu

Diğer Kaynaklar

Dizeleri (C# Programlama Kılavuzu)