Use the IFORMATTABLE interface to customize format strings for custom class

xiaoxiao2021-03-06  99

///

/// "Click" class definition. /// public class point: system.iformtable {/// /// Point class horizontal length coordinates. /// private int m_x, m_y;

///

/// Point class configuration (parameter is a horizontal unit value). /// /// /// public point (int x, int y) {m_x = x ; M_y = y;}

#Region iFormatTable member

///

/// Used to generate a function of a format string. /// /// format string. /// area format information object. /// ", m_x, m_y); // "Point" object string format is: "". Break; default: // Customize the default format. Retstring = string.format (formatProvider, "({0: x}, {1: x}", m_x, m_y); // "Point" object string format is: "(hexadecimal number, ten Six reformed numbers). Break;

}}} Catch (System.NullReferenceException) {// Format string is empty, return to the universal format. Retstring = string.format (formatProvider, "({0}, {1})", m_x, m_y);} return rettstring;

#ndregion

}

///

/// TEST summary description. /// public class test {public static void main () {// Defines a point. Point P = New Point (13, 10); // Print the point of the default format. Console.writeline ("{0}", p); // prints the point of the standard format. Console.WriteLine ("{0: S}", P); / * * Output: (13, 10) * <13, 10> * /}}

/ * * Note: If you do not implement iformattable interfaces, you can also print the custom class object with String.Format, but string.format method * just calls the Object.toString method to print the class name. Below is String.Format These methods call TOSTRING processing sequence: * * 1. Return empty string (") if the value of the object to format is NULL. * 2. If the class to format the object belonging to implement the ICUSTOMMATTER interface, call the icustomatter.format method. * 3. If the previous icomformatter.format method is not called, and the class implements the IFORMATTABLE interface, call the * iformattable.toString method. * 4. If the previous step is not formatted, call the TSTRING method of this type (inherited from the Object class). * * However, only the IFORMATTABLE or ICUSTOMMATTER is implemented to identify the format string we define, print out the results you want. * /

转载请注明原文地址:https://www.9cbs.com/read-104184.html

New Post(0)