Hi all,
I am going through the concepts of CSharp and I could not understand.. and get enough info on net about String.Format().
Can someone please explain how is it useful?!

I got this example.. but i see no use of using String.Format() here as.. use it or not.. the result would be same.. then why did the author use it here.

using System;

public class Exercise
{
    static int Main(string[] args)
    {
        var wage = 22.45;
        var strDisplay = string.Format("Hourly Salary: {0}", wage);

        Console.WriteLine(strDisplay);

        return 0;
    }
}

Output:
Hourly Salary: 22.45
Also, any info about this String.Format() will be helpful.
Thankyou.

In your example it has little use. But what if you wanted to display a message in a MessageBox in fixed point format,say 3 places after the decimal sign?
You would do something like this:
string message = string.Format("f3 format is {0:f3}",123456);
System.Windows.Forms.MessageBox.Show(message);

This would give you a window with an OK-button and the message 123.456 in it.

In your example it has little use. But what if you wanted to display a message in a MessageBox in fixed point format,say 3 places after the decimal sign?
You would do something like this:
string message = string.Format("f3 format is {0:f3}",123456);
System.Windows.Forms.MessageBox.Show(message);

This would give you a window with an OK-button and the message 123.456 in it.

Thankyou fr the quick reply.

In the above example you have used,
can u please let me know what is {0:f3} is ??? what is f3?? Does it mean that decimal point after first 3digits? If so why is ':' colon used? Is that also a part of the syntax? What is f3 format? Is it some kind of format? or user declaration...

The braces {}with a number are a placeholder for variables.
a=11;
b=22;
Console.WriteLIne("{0} + {1} = 33", a, b); would print 11 + 22 = 33
After each number between braces you can place a colon followed by a format specifier. I used f3(or F3 it's the same) This means format my number or variable so that I always have 3 places after the decimal. Try f4! Other codes are e,E d,D x,X c,C g,G n,N. Experiment!

In your example it has little use. But what if you wanted to display a message in a MessageBox in fixed point format,say 3 places after the decimal sign?
You would do something like this:
string message = string.Format("f3 format is {0:f3}",123456);
System.Windows.Forms.MessageBox.Show(message);

This would give you a window with an OK-button and the message 123.456 in it.

When i used 0:f3 output is three zeroes after lastdigit.

i.e., 123456.000

int i = 654321;
Console.WriteLine("{0:E}", i); // 6.543210E+005
Console.WriteLine("{0:F}", i); // 654321.00
Console.WriteLine("{0:E6}", 123); // 1.230000E+002

Can someone explain how the above ouputs came:(

message 123.456 in it.

I was wrong here, since 123456 has nothing behind the decimal and we want a format of 0:f3 WrieLine prints the number with a decimal and 3 zeros. Sorry bout that.
0:E is scientific notation. It prints a number as a digit followed by a decimal point followed by the remaining numbers, followed by E and a number. E and a number meaning 10 raised to power number.
So 6.543210E+005 means 6.543210 multipied by 10 power 5 wich is 100000 so your original number still is 654321 only the format is different.
0:F defaults to 2. It is the same as 0:F2.

To learn more about String.Format - press F1 on it, it has examples and a lot of information on other formats and how to use it.

Thank you ddanbe and LizR

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.