|
3 | 3 |
|
4 | 4 | public class Temperature |
5 | 5 | { |
6 | | - private decimal temp; |
| 6 | + private decimal temp; |
7 | 7 |
|
8 | | - public Temperature(decimal temperature) |
9 | | - { |
10 | | - this.temp = temperature; |
11 | | - } |
| 8 | + public Temperature(decimal temperature) => this.temp = temperature; |
12 | 9 |
|
13 | | - public override string ToString() |
14 | | - { |
15 | | - return ToString("C"); |
16 | | - } |
| 10 | + public override string ToString() => ToString("C"); |
17 | 11 |
|
18 | | - public string ToString(string format) |
19 | | - { |
20 | | - if (String.IsNullOrEmpty(format)) |
21 | | - format = "G"; |
| 12 | + public string ToString(string format) |
| 13 | + { |
| 14 | + if (string.IsNullOrEmpty(format)) |
| 15 | + format = "G"; |
22 | 16 |
|
23 | | - switch (format.ToUpper()) |
24 | | - { |
25 | | - case "G": |
26 | | - case "C": |
27 | | - return temp.ToString("N") + " °C"; |
28 | | - case "F": |
29 | | - return (9 * temp / 5 + 32).ToString("N") + " °F"; |
30 | | - case "K": |
31 | | - return (temp + 273.15m).ToString("N") + " °K"; |
32 | | - default: |
33 | | - throw new FormatException(String.Format("The '{0}' format specifier is not supported", |
34 | | - format)); |
35 | | - } |
36 | | - } |
| 17 | + switch (format.ToUpperInvariant()) |
| 18 | + { |
| 19 | + case "G": |
| 20 | + case "C": |
| 21 | + return temp.ToString("N") + " °C"; |
| 22 | + case "F": |
| 23 | + return (9 * temp / 5 + 32).ToString("N") + " °F"; |
| 24 | + case "K": |
| 25 | + return (temp + 273.15m).ToString("N") + " °K"; |
| 26 | + default: |
| 27 | + throw new FormatException($"The '{format}' format specifier is not supported"); |
| 28 | + } |
| 29 | + } |
37 | 30 |
|
38 | | - public void Display(params string []formats) |
39 | | - { |
40 | | - if (formats.Length == 0) |
41 | | - { |
42 | | - Console.WriteLine(this.ToString("G")); |
43 | | - } |
44 | | - else |
45 | | - { |
46 | | - foreach (string format in formats) |
47 | | - { |
48 | | - try { |
49 | | - Console.WriteLine(this.ToString(format)); |
| 31 | + public void Display(params string[] formats) |
| 32 | + { |
| 33 | + if (formats.Length == 0) |
| 34 | + { |
| 35 | + Console.WriteLine(this.ToString("G")); |
| 36 | + } |
| 37 | + else |
| 38 | + { |
| 39 | + foreach (string format in formats) |
| 40 | + { |
| 41 | + try |
| 42 | + { |
| 43 | + Console.WriteLine(this.ToString(format)); |
| 44 | + } |
| 45 | + // If there is an exception, do nothing. |
| 46 | + catch { } |
50 | 47 | } |
51 | | - // If there is an exception, do nothing. |
52 | | - catch { } |
53 | | - } |
54 | | - } |
55 | | - } |
| 48 | + } |
| 49 | + } |
56 | 50 | } |
57 | 51 | // </Snippet1> |
58 | 52 |
|
59 | 53 | // <Snippet2> |
60 | 54 | public class Class1 |
61 | 55 | { |
62 | | - public static void Main() |
63 | | - { |
64 | | - Temperature temp1 = new Temperature(100); |
65 | | - string[] formats = { "C", "G", "F", "K" }; |
| 56 | + public static void Main() |
| 57 | + { |
| 58 | + Temperature temp1 = new(100); |
| 59 | + string[] formats = [ "C", "G", "F", "K" ]; |
66 | 60 |
|
67 | | - // Call Display method with a string array. |
68 | | - Console.WriteLine("Calling Display with a string array:"); |
69 | | - temp1.Display(formats); |
70 | | - Console.WriteLine(); |
| 61 | + // Call Display method with a string array. |
| 62 | + Console.WriteLine("Calling Display with a string array:"); |
| 63 | + temp1.Display(formats); |
| 64 | + Console.WriteLine(); |
71 | 65 |
|
72 | | - // Call Display method with individual string arguments. |
73 | | - Console.WriteLine("Calling Display with individual arguments:"); |
74 | | - temp1.Display("C", "F", "K", "G"); |
75 | | - Console.WriteLine(); |
| 66 | + // Call Display method with individual string arguments. |
| 67 | + Console.WriteLine("Calling Display with individual arguments:"); |
| 68 | + temp1.Display("C", "F", "K", "G"); |
| 69 | + Console.WriteLine(); |
76 | 70 |
|
77 | | - // Call parameterless Display method. |
78 | | - Console.WriteLine("Calling Display with an implicit parameter array:"); |
79 | | - temp1.Display(); |
80 | | - } |
| 71 | + // Call parameterless Display method. |
| 72 | + Console.WriteLine("Calling Display with an implicit parameter array:"); |
| 73 | + temp1.Display(); |
| 74 | + } |
81 | 75 | } |
82 | 76 | // The example displays the following output: |
83 | 77 | // Calling Display with a string array: |
|
0 commit comments