C# - Concatenate Two Strings

Given two strings, we have to concatenate two strings using a predefined method in C#.
Submitted by Nidhi, on October 12, 2020 [Last updated : March 21, 2023]

Here, we will read two strings, then concatenate both strings and assigned to another string.

C# program to concatenate two strings using a predefined method

The source code to concatenate the two strings using a predefined method is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to concatenate two strings 
//using the predefined method.

using System;

class Demo
{
    static void Main()
    {
        string str1 = "";
        string str2 = "";
        string str3 = "";

        Console.Write("Enter string1: ");
        str1 = Console.ReadLine();

        Console.Write("Enter string2: ");
        str2 = Console.ReadLine();

        str3=String.Concat(str1, str2);
        
        Console.WriteLine("Concatenated string is: {0}",str3);
    }
}

Output

Enter string1: Include
Enter string2: Help
Concatenated string is: IncludeHelp
Press any key to continue . . . 

Explanation

Here, we created a class Demo that contains the Main() method, The Main() method is the entry point for the program, here we created three strings str1, str2, and str3. Then read the value of str1 and str2. After that concatenated the string str1 with str2 and assign the result into str3 using Concat() method of string class. Then we printed the result on the console screen.

C# Basic Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.