System.String.Concat 方法 (Object, Object, Object, Object, ...)

方法描述

将四个指定对象的字符串表示形式与可选变量长度参数列表中指定的任何对象串联起来。

语法定义(C# System.String.Concat 方法 (Object, Object, Object, Object, ...) 的用法)

C#不支持使用可变长度参数 (varargs) 的方法。
编译器会自动将对此方法的调用解析为使用参数数据的同一方法。

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
arg0 System-Object 要连接的第一个对象。
arg1 System-Object 要连接的第二个对象。
arg2 System-Object 要连接的第三个对象。
arg3 System-Object 要连接的第四个对象。
返回值 System.String 参数列表中的每个值的连接字符串表示形式。

提示和注释

方法连接参数列表中的每个对象,方法是调用其无参数 ToString 方法;它不会添加任何分隔符。

使用 String.Empty 字符串替代任何 null 参数。

注意

Concat 方法的最后一个参数是要串联的一个或多个附加对象的逗号分隔的可选列表。

对调用者的说明

此方法标有 vararg 关键字,这意味着它支持的参数数目可变。 可以从 Visual C++ 中调用该方法,但不能从 C# 或 Visual Basic 代码调用。 C# 和 Visual Basic 编译器将对 Concat(Object, Object, Object, Object) 的调用解析为对 Concat(Object[]) 的调用。

System.String.Concat 方法 (Object, Object, Object, Object, ...)例子

在此情况下,使用 9 个参数调用该方法。

using System;
using System.Collections;

public class Example
{
   public static void Main()
   {
      const int WORD_SIZE = 4;

      // Define some 4-letter words to be scrambled.
      string[] words = { "home", "food", "game", "rest" };
      // Define two arrays equal to the number of letters in each word.
      double[] keys = new double[WORD_SIZE];
      string[] letters = new string[WORD_SIZE];
      // Initialize the random number generator.
      Random rnd = new Random();

      // Scramble each word.
      foreach (string word in words)
      {
         for (int ctr = 0; ctr < word.Length; ctr++)
         {
            // Populate the array of keys with random numbers.
            keys[ctr] = rnd.NextDouble();
            // Assign a letter to the array of letters.
            letters[ctr] = word[ctr].ToString();
         }   
         // Sort the array. 
         Array.Sort(keys, letters, 0, WORD_SIZE, Comparer.Default);      
         // Display the scrambled word.
         string scrambledWord = String.Concat(letters[0], letters[1], 
                                              letters[2], letters[3]);
         Console.WriteLine("{0} --> {1}", word, scrambledWord);
      } 
   }
}
// The example displays output like the following:
//       home --> mheo
//       food --> oodf
//       game --> aemg
//       rest --> trse

异常

异常 异常描述

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。