C# | String Concat with examples | Set-3 - 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 6 are discussed in Set-1 & Set-2 and remaining are discussed in Set-3 and Set-4.
This method is used to concatenate the string representations of the elements in a specified Object array.
Note: If an array contains a null object, then String.Empty is used in place of a null object.
Syntax:
public static string Concat (params object[] arg);
Here, arg is an object array that contains the elements to concatenate.
Return Value: The return type of this method is System.String. This method returns the concatenated string that represents the values of the elements present in arg.
Exception:
If the value of the given arg 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(object[]) Methodusing System;  // class declarationclass EmptyA { }  class GFG {          // Main method    public static void Main()    {        // creating object of EmptyA class        EmptyA g1 = new EmptyA();          string strA = " GeeksforGeeks ";                  object[] ob = {21, " Hello!", strA, g1};          // print elements of object array        // using Concat(object[]) Method        Console.WriteLine("Elements of object array : {0}",                                        string.Concat(ob));    }}
Output:
Elements of object array : 21 Hello! GeeksforGeeks EmptyA
This method is used to create the string representation of a specified object.
Syntax:
public static string Concat (object argA);
Here, argA is the object to represent, or null.
Return Value: The return type of this method is System.String. This method returns the concatenated string that represents the values of the elements present in argA, or Empty if the argA is null.
Example:
// C# program to illustrate the// Concat(object) Methodusing System;  class GFG {          // Main method    public static void Main()    {          // string        string strA = "Geeks";          // assigning string to object        object ob = strA;          // object array        Object[] objs = new Object[] {"1", "2"};          // using Concat(object) method       Console.WriteLine("Concatenate 1, 2, and 3 objects:");       Console.WriteLine("1: {0}", String.Concat(ob));       Console.WriteLine("2: {0}", String.Concat(ob, ob));       Console.WriteLine("3: {0}", String.Concat(ob, ob, ob));                Console.WriteLine("Concatenate two element object array: {0}", String.Concat(objs));    }}
Output:
Concatenate 1, 2, and 3 objects:
1: Geeks
2: GeeksGeeks
3: GeeksGeeksGeeks
Concatenate two element object array: 12

This method is used to Concatenates the string representations of two specified objects.
Syntax:
public static string Concat (object argA, object argB);
Parameters:
argA: First object to concatenate.argB: Second object to concatenate.
Return Value: The return type of this method is System.String. The concatenated string is the representation of each value present in the parameter list.
Example:
// C# program to illustrate// Concat(object, object) Methodusing System;  class GFG {          // Main method    public static void Main()    {          // string        string strA = "50";          // object        object ob = strA;          // object array        Object[] objs = new Object[] {"34", "87"};          // Concatenating two objects        // using Concat(object, object) method        Console.WriteLine("Concatenate two objects: {0}",                                  String.Concat(ob, ob));                                            Console.WriteLine("Concatenate two element object array: {0}",                                                 String.Concat(objs));    }}
Output:
Concatenate two objects: 5050
Concatenate two element object array: 3487

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.
Destructors in C#
Extension Method in C#
HashSet in C# with Examples
Top 50 C# Interview Questions & Answers
C# | How to insert an element in an Array?
Partial Classes in C#
C# | Inheritance
C# | List Class
Difference between Hashtable and Dictionary in C#
Lambda Expressions in C#