Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Thursday 18 June 2015

STRINGS (CONCATENATION) IN C#


Today in Engineer's World, we are discussing a very important topic of C# -String in very easy way. Posted By- +Manish Kumar Gautam +LIVE VIAR +ASP.NET SOLUTIONS 

Click imaginationhunt.blogspot to see latest Blogs


Regular Method:




To append strings and variable by using regular method. We use the '+' operator.

Let's understand this by taking an example
Example- Suppose there is a rectangular shape tar coal container whose length = 40m, and breadth = 15m and height = 10m. Now, you have to find out the capacity of tar coal it can hold.

Practical Implementation:

using System;

namespace AspnetSolutions
{
    class ConcatenateString
    {
        public static void Main()
        {
            int length = 40;
            int breadth = 15;
            int height = 10;

            int Volume = length * breadth * height;

           Console.WriteLine("Tar coal container whose length = " + length + "m, and breadth = " + breadth + "m and height = " + height + "m");   //Line 1
           Console.WriteLine("Capacity it can hold = " + Volume + " cubic meter.");   //Line 2
        }
    }
}

Output




In Line1, one overload of the WriteLine() method accept the default string in which the specification of the container is printed. And in Line2, overload of WriteLine() method the capacity it can hold is printed. And the variables (i.e., height, length, breadth, volume) within the string are filled at runtime. Hence, to concatenate the variables with string we use the '+' operator.

This is the most common and simple way to code this problem.
But this string looks confusing for beginners as they might think why we put so many '+' sign in this string. Is there any other way so that this string can be formatted? Same, thing came in mind when C# developer create string, so they develop a method and something called as place holders to format the string hence to make the string readable and attractive.

We would read about Format string in next Blog.



Related Questions:

Q-1 What will be the output of the code:



using System;

namespace AspnetSolutions
{
    class ConcatenatetString
    {
        public static void Main()
        {
            string str1 = "imaginationhunt.";
            string str2 = "blogspot.in";
            string str3;
            str3 = str2;
            Console.WriteLine(str1+str3); 
        }
    }
}

A) imaginationhunt+str3
B) Compile time error
C) Run time error
D) imaginationhunt.blogspot.in
Ans- Option (D).

Q-2 What will be the output of the code:

using System;

namespace AspnetSolutions
{
    class ConcatenatetString 
    {
        public static void Main()
        {
            string str1 = "imaginationhunt.";
            string str2 = "blogspot.in";
            string str3;            str3= String.Copy(str2);
            Console.WriteLine(str1+str3);
        }
    }
}


Click imaginationhunt.blogspot to see latest Blogs

A) imaginationhunt+str3
B) Compile time error (invalid use of str2)
C) Run time error
D) imaginationhunt.blogspot.in
Ans- Option (D).

Keep learning and sharing...

No comments:

Post a Comment

Featured post

Think that makes you rich and richer

 Napolean said: “You can think and grow rich, but if you can be brought up like most people with work and you won't starve, this wil...