User Tools

Site Tools


notes:csharp:formatting

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
notes:csharp:formatting [2016/11/28]
admin [Formatting in C#]
notes:csharp:formatting [2016/11/28] (current)
admin
Line 64: Line 64:
 </​code>​ </​code>​
  
- 
-====Custom formatting==== 
- 
-**Example**:​ Custom formatting using a //​ToString//​ method that accepts a format string: 
-<code csharp> 
-class Book 
-{ 
-    public string ToString(string format) 
-    { 
-        // "​G"​ represents a common format. It is the same as calling ToString(). 
-        // A null value for the format string should also display the common format. 
-        if (String.IsNullOrWhiteSpace(format) || format == "​G"​) 
-            format = "​F";​ 
- 
-        switch (format) 
-        { 
-            case "​F":​ // full 
-                return Title + "; " + Authors + "; " + ISBN + "; " + String.Format("​{0:​C}",​ Price); 
-            case "​T":​ // title only 
-                return Title; 
-            case "​TA":​ // title and authors 
-                return Title + ", " + Authors; 
-            case "​I":​ // isbn only 
-                return ISBN; 
-            default: 
-                throw new FormatException(String.Format("​The '​{0}'​ format is not supported.",​ format)); 
-        } 
-    } 
- 
-    public string Title { get; set; } 
-    public string Authors { get; set; } 
-    public decimal Price { get; set; } 
-    public float Rating { get; set; } 
-    public string ISBN { get; set; } 
-} 
- 
-// Testing... 
-Book book = new Book(); 
-book.Title = "​Programming Windows";​ 
-book.Authors = "​Charles Petzold";​ 
-book.Price = 44.30M; 
-book.Rating = 4.6f; 
-book.ISBN = "​978-0735671768";​ 
- 
-Console.WriteLine(book.ToString("​F"​));​ // Programming Windows; Charles Petzold; 978-0735671768;​ $44.30 
-Console.WriteLine(book.ToString("​I"​));​ // 978-0735671768 
-Console.WriteLine(book.ToString("​T"​));​ // Programming Windows 
- 
-... but the overloaded ToString(string format, IFormatProvider provider) method does not work as expected: 
-Console.WriteLine("​{0:​F}",​ book); // returns the fully-qualified type name 
-Console.WriteLine("​{0:​I}",​ book); // returns the fully-qualified type name 
-Console.WriteLine("​{0:​T}",​ book); // returns the fully-qualified type name 
- 
- 
-</​code>​ 
- 
- 
-**Example**:​ Custom formatting using the //​IFormattable.ToString//​ method. This method accepts two parameters: 
-  * a format string 
-  * a format provider - a class implementing //​IFormatProvider//​ 
- 
-<code chsarp> 
-// [1] Derive your class from IFormattable. 
-class Book : IFormattable 
-{ 
-    // ... 
-} 
-</​code>​ 
-<code csharp> 
-// [2] Implement the IFormattable interface by providing the ToString method with two parameters: 
-// The '​format'​ parameter is a string that specifies the requested format. 
-// The '​formatProvider'​ parameter is a reference to an object that implements IFormatProvider used 
-// to provide details such as culture-specific formatting. If this parameter is null, the culture ​ 
-// specified in the system settings is used. 
-public string ToString(string format, IFormatProvider provider) 
-{ 
-   // ... 
-} 
-</​code>​ 
- 
-[3] Now you can call the overloaded //​ToString//​ method. The output is the same as in the previous example. 
-<code csharp> 
-Console.WriteLine("​{0:​F}",​ book); 
-Console.WriteLine("​{0:​I}",​ book); 
-Console.WriteLine("​{0:​T}",​ book); 
-</​code>​ 
  
notes/csharp/formatting.txt ยท Last modified: 2016/11/28 by admin