C# | String Concat with examples | Set-3

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.

7. Concat(Object[])

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate 
// the Concat(object[]) Method
using System;
  
// class declaration
class 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));
    }
}

chevron_right


Output:

Elements of object array : 21 Hello! GeeksforGeeks EmptyA
8. Concat(Object)

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// Concat(object) Method
using 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));
    }
}

chevron_right


Output:

Concatenate 1, 2, and 3 objects:
1: Geeks
2: GeeksGeeks
3: GeeksGeeksGeeks
Concatenate two element object array: 12
9. Concat(Object, Object)

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate
// Concat(object, object) Method
using 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));
    }
}

chevron_right


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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.