Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Friday, 19 June 2015

STRINGS (FORMAT) 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 +LIV WIRE +ASP.NET SOLUTIONS


Click imaginationhunt.blogspot to see latest Blogs  

Format string is a concept of string in which, the content can be determined at runtime dynamically. Using place holders and String.Format() Method. All the values enclosed within braces of string are replaced at runtime.





1) Using Place Holders:
Place holders are nothing but curly braces { }. It provide short hint that describe a specific value. In the code we put our variables in those braces in some sequence using sequence number starting initially from zero (0). And as we run the code the value showed before us according to the sequence number.

Click imaginationhunt.blogspot to see latest Blogs   

Let's understand this by seeing an example-

Example- Suppose there is a rectangular shape tar coal container whose length = 40m, and breadth = 15m and height = 10m. You have to find out the capacity it can hold.

Practical implementation:

using System;

namespace AspnetSolutions

{
    class FormatString
    {
        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 = {0}m, and breadth = {1}m and height = {2}m", length, breadth, height);
           Console.WriteLine("Capacity it can hold = {0} cubic meter.", Volume);

        }
    }


Output



You can notice in the output window that, {0} refer to length, {1} refer to breadth and {2} refer to height as we have declared in our first overload of WriteLine() method.
Hence, with the help of placeholder our code looks much readable and easily understandable.

To read STRINGS (FORMAT) IN C#  Part - 2 Click here.

Related Question:

#newtoprgm try firstprogram

Q-1 Place holders are used to:
A) make string visible
B) make string readable
C) make string to provide hint about specific value
D) make string available globally
Ans- Option (B and C). 

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

using System;

namespace AspnetSolutions

{
    class FormatString
    {
        public static void
Main() 
        { 
                string name="Manan"; 
                int num=5; 
                string str= "Sara";       
                Console.WriteLine("Hello, {2} and {0}. You Id number is = {1} ",name,num,str); 

        }
    }
}


Click imaginationhunt.blogspot to see latest Blogs  
 
A) Hello, Sara and Manan. Your Id number is 5.
B) Error- Variable assigned but never used.
C) Error- Place holder are not sequential arrange.
Ans- Option(A).

Keep learning and coding...

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...