C# | String Concat with examples | Set-3
Last Updated :
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.
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:
using System;
class EmptyA { }
class GFG {
public static void Main()
{
EmptyA g1 = new EmptyA();
string strA = " GeeksforGeeks " ;
object [] ob = {21, " Hello!" , strA, g1};
Console.WriteLine( "Elements of object array : {0}" ,
string .Concat(ob));
}
}
|
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:
using System;
class GFG {
public static void Main()
{
string strA = "Geeks" ;
object ob = strA;
Object[] objs = new Object[] { "1" , "2" };
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
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:
using System;
class GFG {
public static void Main()
{
string strA = "50" ;
object ob = strA;
Object[] objs = new Object[] { "34" , "87" };
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
Share your thoughts in the comments
Please Login to comment...