string concatenation

C# reuses the + for string concatenation.


using System;

class Program
{
    static void Main(string[] args)
    {
        string s1 = "abc";
        string s2 = "java2s.com";

        string s3 = s1 + s2;

        Console.WriteLine(s3);


    }
}

The output:


abcjava2s.com

We can use the operator + to append other types to a string type.


using System;

class Program
{
    static void Main(string[] args)
    {
        string s1 = "abc";

        string s2 = s1 + 5;

        Console.WriteLine(s2);

    }
}

The output:


abc5

Be careful when appending more than one variables to a string.


using System;

class Program
{
    static void Main(string[] args)
    {
        string s1 = "abc";

        string s2 = s1 + 5 + 5;

        Console.WriteLine(s2);

    }
}

The output:


abc55

To get the result we want, we have to do the math first.


using System;

class Program
{
    static void Main(string[] args)
    {
        string s1 = "abc";

        string s2 = s1 + (5 + 5);

        Console.WriteLine(s2);

    }
}

The output:


abc10
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.