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:
CSharp
// 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));
}
}
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:
CSharp
// 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));
}
}
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:
CSharp
// 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));
}
}
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
Similar Reads
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 tota
4 min read
C# | String Concat with examples | Set-2
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 tota
4 min read
C# String Join() Method | Set - 1
In C#, Join() is a string method. This method is used to concatenate the members of a collection or the elements of the specified array, using the specified separator between each member or element. This method can be overloaded by passing different parameters to it.Concatenation: This method is use
5 min read
C# String Join Method | Set - 2
The C# Join() method is a method that is present in the String class. This method is used to concatenate or combine different kinds of collections, such as an array, list or set. With the specified separator between each member or element. Key Features:Concatenation: The Join() method is used to com
4 min read
C# Verbatim String Literal - @
In C#, a verbatim string is created using a special symbol @. The symbol(@) is known as a verbatim identifier. If a string contains @ as a prefix followed by double quotes, then compiler identifies that string as a verbatim string and compile that string. The main advantage of @ symbol is to tell th
5 min read
C# StringBuilder
StringBuilder is a Dynamic Object. It doesnât create a new object in the memory but dynamically expands the needed memory to accommodate the modified or new string.A String object is immutable, i.e. a String cannot be changed once created. To avoid string replacing, appending, removing or inserting
4 min read
How to Concatenate Multiple Strings in C?
In C, concatenating strings means joining two or more strings end-to-end to form a new string. In this article, we will learn how to concatenate multiple strings in C. Example: Input:char str1[50] = "Hello";char str2[50] = " geeksforgeeks";Output:Hello geeksforgeeks!Concatenating Strings in CTo conc
1 min read
Concatenating Two Strings in C
Concatenating two strings means appending one string at the end of another string. In this article, we will learn how to concatenate two strings in C.The most straightforward method to concatenate two strings is by using strcat() function. Let's take a look at an example:C#include <stdio.h> #i
2 min read
Shell Script to Concatenate Two Strings
String concatenation is the process of appending a string to the end of another string. This can be done with shell scripting using two methods: using the += operator, or simply writing strings one after the other. The examples below show some shell scripts that can be used to concatenate strings. E
3 min read
How to Concatenate Multiple C++ Strings on One Line?
In C++, strings are used to store a sequence of characters. The combination of multiple strings into a single one is called Concatenation. In this article, we will learn how to concatenate multiple strings in C++. Example Input: str1="Hello!"str2="Geek"str3="Welcome to GfG"Output:Hello! Geek Welcome
1 min read