override ToString : ToString « Class « C# / CSharp Tutorial






using System;
using System.Collections;
   
struct RGB
{
    public int red;
    public int green;
    public int blue;
   
    public RGB(int red, int green, int blue)
    {
        this.red = red;
        this.green = green;
        this.blue = blue;
    }
   
    public override String ToString()
    {
        return red.ToString("X") 
            + green.ToString("X") 
            + blue.ToString("X");
    }
}
   
class BoxTest
{
    static ArrayList rgbValues;
   
    public static void Main()
    {
        RGB rgb = new RGB(255, 255, 255);
   
        rgbValues = new ArrayList();
        rgbValues.Add(rgb);
   
        Console.WriteLine("The RGB value is {0}", rgb);
    }
}








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