C# | String Concat with examples | Set-2 - GeeksforGeeks

01 Feb, 2019
String.Concat Method is used to concatenate one or more instances of String or the String representations of the values of one or more instances of Object. It always returns a concatenated string.This method can be overloaded by passing different types and number of parameters to it. There are total 11 methods in the overload list of the Concat method in which first 3 are discussed in Set-1 and remaining are discussed in Set-2, Set-3, and Set-4.
This method is used to concatenate two different instances of the string. You can also use Concatenation operator(+)to concatenate strings.
Note: If an array contains a null object, then the Empty string is used in place of a null object.
Syntax:
public static string Concat (string strA, string strB);
Parameters:
strA: First string to concatenate.strB: Second string to concatenate.
Return Value: The return type of this method is System.String. This method returns a string as an result of concatenation of two strings, i.e. strA, and strB.
Example:
// C# program to illustrate the use // of Concat(String, String ) Methodusing System;  public class GFG {          // Main Method    static public void Main()    {        string strA = "Hello! ";        string strB = "Geeks.";        string str;          // print all strings        Console.WriteLine("String A is: {0}", strA);        Console.WriteLine("String B is: {0}", strB);          // Concatenate two different strings        // into a single String        // using  Concat(String, String ) Method        str = String.Concat(strA, strB);          Console.WriteLine("Concatenated string is: {0}", str);    }}
Output:
String A is: Hello! 
String B is: Geeks.
Concatenated string is: Hello! Geeks.

This method is used to concatenate three different string into a single string. You can also use Concatenation operator(+)to concatenate strings.
Note: If an array contains a null object, then Empty string is used in place of a null object.
Syntax:
public static string Concat (string strA, string strB, string strC);
Parameters:
strA: First string to concatenate.strB: Second string to concatenate.strC: Third string to concatenate.
Return Value:The return type of this method is System.String. This method returns a string which is created from the concatenation of three strings, i.e. strA, strB, and strC.
Example:
// C# program to illustrate the // Concat(String, String, String) Methodusing System;  class GFG {          // Main Method    static public void Main()    {        string strA = "Welcome ";        string strB = "to ";        string strC = "GFG. ";        string str;          // print all strings        Console.WriteLine("String A is: {0}", strA);        Console.WriteLine("String B is: {0}", strB);        Console.WriteLine("String C is: {0}", strC);           // Concatenate three different strings         // into a single String using the         // Concat(String, String, String ) Method        str = String.Concat(strA, strB, strC);          Console.WriteLine("Concatenated string is: {0}", str);    }}
Output:
String A is: Welcome 
String B is: to 
String C is: GFG.
Concatenated string is: Welcome to GFG. 

This method is used to concatenates the elements of a specified String array.
Syntax:
public static string Concat (params string[] items);
Here, items are the array of string instance.
Return Value: The return type of this method is System.String. This method returns the concatenated elements of items.
Exceptions:
If the value of the given string items is null then this method will give ArgumentNullException.
If the array is out of memory, then this method will give OutOfMemoryException .
Example:
// C# program to illustrate the // Concat(string[]) Methodusing System;  class GFG {          // Main method    public static void Main()    {          // array         string[] strA = {"Hey, ", "This ","is ", "C# ", "Tutorial."};          // print elements of array        foreach(string elements in strA)        {            Console.WriteLine("Elements of strA array : {0}", elements);        }                  // concatenate the element of array        // into single string        // using Concat(string[]) Method        Console.WriteLine("After Concatenation: {0}",                                 string.Concat(strA));    }}
Output:
Elements of strA array : Hey, 
Elements of strA array : This 
Elements of strA array : is 
Elements of strA array : C# 
Elements of strA array : Tutorial.
After Concatenation: Hey, This is C# Tutorial.

Next : Set 3
Reference: https://docs.microsoft.com/en-us/dotnet/api/system.string.concat?view=netframework-4.7.2
CSharp-string
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# Dictionary with examples
C# | Method Overriding
Difference between Ref and Out keywords in C#
C# | Delegates
Destructors in C#
Extension Method in C#
C# | Constructors
Introduction to .NET Framework
C# | Abstract Classes
C# | Class and Object