A small question about the ToString () method

Source: Internet
Author: User

When I wrote C # code yesterday, I encountered a small problem about formatting strings. I retrieve the complete date (including time) from the database, but I only want to display the time on the interface, not the date. So I want to use the ToString ("HH: mm") method. The specific statement is as follows: Table. rows [0] ["Time"]. toString ("HH: mm") but the problem arises. The Compiler prompts that the "ToString" method does not use any overload parameter. But I have seen such a usage. How can I change it? So I checked the materials for a while and finally found the answer to the question. In fact, the problem lies in my lack of solid knowledge about C #. in C #, all classes inherited from objects have tostring () methods. Without special declarations, classes in C # are inherited from objects by default, so the tostring () method is available. However, not all classes have the same tostring () method, because there are overwrites and reloads, which leads to different functions of the toString method. I mistakenly thought that the toString methods of all classes are identical. The basic knowledge is not solid enough to be further consolidated! The following is an example of class rewriting and reload ToString methods on MSDN. You can refer to this example to help us understand the nature of ToString methods. [Csharp] public class Temperature: IFormattable {private decimal temp; public Temperature (decimal temperature) {if (temperature <-273.15 m) throw new ArgumentOutOfRangeException (String. format ("{0} is less than absolute zero. ", temperature); this. temp = temperature;} public decimal Celsius {get {return temp;} public decimal Fahrenheit {get {return temp * 9/5 + 32 ;}} public decimal Kelvin {get {return temp + 273.15 m;} public override string ToString () {return this. toString ("G", CultureInfo. currentCulture);} public string ToString (string format) {return this. toString (format, CultureInfo. currentCulture);} public string ToString (string format, IFormatProvider provider) {if (String. isNullOrEmpty (format) format = "G"; if (provider = null) provider = CultureInfo. currentCulture; switch (format. toUpperInvariant () {case "G": case "C": return temp. toString ("F2", provider) + "°C"; case "F": return Fahrenheit. toString ("F2", provider) + "°F"; case "K": return Kelvin. toString ("F2", provider) + "° K"; default: www.2cto.com throw new FormatException (String. format ("The {0} format string is not supported. ", format ));}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.