c-sharp
  1. c-sharp-program-to-concatenate-two-strings

Program to Concatenate Two Strings - (C# Basic Programs)

Concatenating two strings is a common programming exercise that is used to combine two strings together. In this tutorial, we will discuss a program to concatenate two strings using C# programming language.

Syntax

The syntax for concatenating two strings in C# is as follows:

string str1 = "Hello";
string str2 = "World";
string result = str1 + str2;
Console.WriteLine(result);

Example

using System;

namespace ConcatenateStrings
{
    class Program
    {
        static void Main(string[] args)
        {
            string str1 = "Hello";
            string str2 = "World";
            string result = str1 + str2;

            Console.WriteLine("The concatenated string is: {0}", result);
        }
    }
}

Output:

The concatenated string is: HelloWorld

Explanation

The above program uses the + operator to concatenate two strings. The strings are stored in two variables str1 and str2. These variables are then concatenated using + operator and the result is stored in result variable. The final result is displayed using Console.WriteLine() method.

Use

Concatenating two strings in C# is a basic programming concept that is used frequently in string manipulation. This can be used in a variety of applications such as web development, desktop application development, game development, and more.

Summary

In this tutorial, we have discussed a program to concatenate two strings using C# programming language. We covered the syntax, example, explanation, use, and importance of concatenating strings in a C# program. Concatenating strings is a fundamental part of string manipulation in programming, and learning how to do so is essential for any programmer.

Published on: