Format value in ToString method : ToString « Class « C# / CSharp Tutorial






using System;

public class Temperature
{
   private decimal temp;

   public Temperature(decimal temperature)
   {
      this.temp = temperature;   
   }

   public override string ToString()
   {
      return this.temp.ToString("N1") + " C";
   }
}

public class Example
{
   public static void Main()
   {
      Temperature currentTemperature = new Temperature(23.6m);
      Console.WriteLine("The current temperature is {0}.", currentTemperature);
   }
}








7.48.ToString
7.48.1.How to override the ToString() method
7.48.2.mark override when overriding ToString
7.48.3.Demonstrate ToString()
7.48.4.override ToString
7.48.5.Format value in ToString method
7.48.6.override ToString method twices