4
Reply

string.Format()

Maha

Maha

Feb 10 2012 5:17 PM
1.4k
string s = name + " " + balanceDue + " " + discountRate + " " + sinceYear;
return s;

In the above code a value is assigned to variable s which type is string then s is returned.

But in the following statement string.Format() method is directly returned. Why we can't do above similar thing when we use string.Format() method that means assigned to variable and that variable is returned. I wish to know the reason for the differences in both cases. 

return string.Format("{0} {1} {2} {3}", name, balanceDue, discountRate, sinceYear);

using System;
class Customer
{
protected string name;
protected double balanceDue;

public Customer()
{
name = "Maha";
balanceDue = 0;
}
}
class PreferredCustomer : Customer
{
private double discountRate;
private int sinceYear;

public PreferredCustomer()
{
discountRate = 0.08;
sinceYear = 1997;
}
public new string ToString()
{
//string s = name + " " + balanceDue + " " + discountRate + " " + sinceYear;
//return s;

return string.Format("{0} {1} {2} {3}", name, balanceDue, discountRate, sinceYear);
}
}
class DemoPreferredCustomerConstructor2
{
public static void Main()
{
PreferredCustomer pc = new PreferredCustomer();
Console.WriteLine("The preferred customer is {0}", pc.ToString());

Console.ReadKey();
}
}
/*
The preferred customer is Maha 0 0.08 1997
*/



Answers (4)
Next Recommended Forum