C# | String Concat with examples | Set-1

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 3 are discussed in this article and remaining are discussed in Set-2, Set-3, and Set-4.

  1. Concat(String, String, String, String)
  2. Concat(Object, Object, Object, Object)
  3. Concat(Object, Object, Object)
  4. Concat(String, String)
  5. Concat(String, String, String)
  6. Concat(String[])
  7. Concat(Object[])
  8. Concat(Object)
  9. Concat(Object, Object)
  10. Concat(IEnumerable<String>)
  11. Concat<T>(IEnumerable<T>)
  12. 1. Concat(String, String, String, String)

    This method is used to concatenate four different string into a single 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, string strC, string strD);

    Parameters:

    strA: First string to concatenate.

    strB: Second string to concatenate.

    strC: Third string to concatenate.

    strD: Fourth string to concatenate.

    The type of all these parameters is System.String.

    Return Value: The return type of this method is System.String. This method returns a string which is created from the concatenation of four strings, i.e. strA, strB, strC, and strD.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# program to illustrate the use of 
    // Concat(String, String, String, String) Method
    using System;
      
    class GFG {
          
        // Main Method
        static public void Main()
        {
            string strA = "Welcome ";
            string strB = "to ";
            string strC = "GFG ";
            string strD = "Portal.";
            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);
            Console.WriteLine("String D is: {0}", strD);
      
            // Concatenate four different strings
            // into a single String using the 
            // Concat(String, String, String, String) Method
            str = String.Concat(strA, strB, strC, strD);
      
            Console.WriteLine("Concatenated string is: {0}", str);
        }
    }

    chevron_right

    
    

    Output:

    String A is: Welcome 
    String B is: to 
    String C is: GFG 
    String D is: Portal.
    Concatenated string is: Welcome to GFG Portal.
    
    2. Concat(Object, Object, Object, Object)

    This method is used to concatenate the string representations of four specified objects and any objects specified in an optional variable length parameter list.

    Note: If a null argument is present in this method then String.Empty is used in place of a null argument.

    Syntax:

    public static string Concat (object argA, object argB, object argC, object argD);

    Parameters:


    argA: First object to concatenate.
    argB: Second object to concatenate.
    argC: Third object to concatenate.
    argD: Fourth object to concatenate.

    Return Value: The return type of this method is System.String. It returns the concatenated string which 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 the use of 
    // Concat(object, object, object, object) Method
    using System;
      
    class GFG {
          
        // Main method
        public static void Main()
        {
      
            // string
            string strA = "Hello! ";
      
            // object
            object ob = strA;
      
            // object array
            Object[] objs = new Object[] {"Black", " Blue"};
      
            // Concatenating four objects by using the
            // Concat(object, object, object, object) method
            Console.WriteLine("Concatenate four objects: {0}",
                                 String.Concat(ob, ob, ob, ob));
                                   
            Console.WriteLine("Concatenate two element object array: {0}"
                                                    String.Concat(objs));
        }
    }

    chevron_right

    
    

    Output:

    Concatenate four objects: Hello! Hello! Hello! Hello! 
    Concatenate two element object array: Black Blue
    
    3. Concat(Object, Object, Object)

    This method is used to concatenates the string representations of three specified objects. If a null argument is present in this method then String.Empty is used in place of null argument.

    Syntax:

    public static string Concat (object argA, object argB, object argC);

    Note: The method concatenates argA, argB, and argC by calling the parameterless ToString method of each object and it does not add any delimiters.

    Parameters:

    argA: First object to concatenate.
    argB: Second object to concatenate.
    argC: Third object to concatenate.

    Return Value: The return type of this method is System.String. This method returns a concatenated string, which 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 the use of 
    // Concat(object, object, object) Method
    using System;
      
    class GFG {
          
        // Main method
        public static void Main()
        {
      
            // string
            string strA = "GFG ";
      
            // object
            object ob = strA;
      
            // Concatenating three objects by using
            // Concat(object, object, object) method
            Console.WriteLine("Concatenate three objects : {0}",
                                     String.Concat(ob, ob, ob));
        }
    }

    chevron_right

    
    

    Output:

    Concatenate three objects : GFG GFG GFG 
    

    Next : Set 2, Set 3

    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.