From f2d748c4c1ed3285cf7c8e4b59c4d95682969a5a Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Thu, 20 Mar 2025 19:55:50 -0700 Subject: [PATCH 1/3] use interpolated strings --- .../AppContext/Overview/csharp/V1/Example4.cs | 7 +- .../AppContext/Overview/csharp/V2/Example6.cs | 7 +- .../AppContext/Overview/csharp/V3/Example8.cs | 5 +- .../System/Boolean/Overview/csharp/binary1.cs | 9 +- .../Boolean/Overview/csharp/conversion3.cs | 10 +- .../Boolean/Overview/csharp/operations2.cs | 5 +- .../System/Boolean/Overview/csharp/parse2.cs | 10 +- .../System/Boolean/Overview/csharp/parse3.cs | 6 +- .../System/Boolean/Overview/csharp/size1.cs | 14 +- .../Boolean/Overview/csharp/tostring1.cs | 6 +- .../Boolean/Overview/csharp/tostring2.cs | 8 +- .../System/Byte/Overview/csharp/bitwise1.cs | 5 +- .../System/Byte/Overview/csharp/bitwise2.cs | 9 +- .../Overview/csharp/byteinstantiation1.cs | 16 +- .../System/Byte/Overview/csharp/tobyte1.cs | 75 ++--- .../Overview/csharp/GetUnicodeCategory3.cs | 4 +- .../Char/Overview/csharp/textelements2.cs | 4 +- .../Char/Overview/csharp/textelements2a.cs | 5 +- .../Console/Overview/csharp/example3.cs | 305 +++++++++--------- .../Console/Overview/csharp/unicode1.cs | 5 +- .../Convert/Overview/csharp/NonDecimal1.cs | 5 +- .../Convert/Overview/csharp/converter.cs | 5 +- .../Convert/ToInt32/csharp/toint32_1.cs | 94 ++---- .../Convert/ToInt64/csharp/toint64_1.cs | 85 ++--- .../DateTime/FromBinary/csharp/frombinary1.cs | 9 +- .../DateTime/Overview/csharp/Persistence.cs | 13 +- .../CreateDelegate/csharp/openClosedOver.cs | 4 +- .../Delegate/CreateDelegate/csharp/source.cs | 15 +- .../Double/CompareTo/csharp/compareto2.cs | 8 +- .../Double/CompareTo/csharp/compareto3.cs | 8 +- .../System/Double/Epsilon/csharp/epsilon.cs | 6 +- .../System/Double/Overview/csharp/Program.cs | 17 + .../Double/Overview/csharp/Project.csproj | 5 +- .../Double/Overview/csharp/comparison1.cs | 4 +- .../Double/Overview/csharp/comparison2.cs | 7 +- .../Double/Overview/csharp/comparison3.cs | 4 +- .../Double/Overview/csharp/comparison4.cs | 8 +- .../System/Double/Overview/csharp/convert1.cs | 60 ++-- .../System/Double/Overview/csharp/convert2.cs | 26 +- .../Double/Overview/csharp/exceptional1.cs | 6 +- .../Double/Overview/csharp/exceptional2.cs | 14 +- .../Double/Overview/csharp/precisionlist1.cs | 5 +- .../Double/Overview/csharp/precisionlist3.cs | 5 +- .../Double/Overview/csharp/precisionlist4.cs | 29 +- .../Double/Overview/csharp/precisionlist5.cs | 33 -- .../Double/Overview/csharp/representation1.cs | 6 +- .../Double/Overview/csharp/representation2.cs | 5 +- .../System/Enum/Overview/csharp/EnumMain.cs | 4 +- .../System/Enum/Overview/csharp/Extensions.cs | 8 +- .../System/Enum/Overview/csharp/class1.cs | 4 +- .../System/Enum/Overview/csharp/class2.cs | 6 +- .../Enum/Overview/csharp/classbitwise1.cs | 4 +- .../Enum/Overview/csharp/classconversion1.cs | 4 +- .../Enum/Overview/csharp/classconversion2.cs | 4 +- .../Enum/Overview/csharp/classiterate.cs | 10 +- .../Enum/Overview/csharp/classparse1.cs | 12 +- .../Exception/HelpLink/csharp/properties.cs | 15 +- .../Exception/Overview/csharp/example.cs | 9 +- .../Exception/Overview/csharp/rethrow1.cs | 9 +- .../Exception/Overview/csharp/rethrow3.cs | 15 +- .../Exception/Overview/csharp/usageerrors1.cs | 4 +- .../Exception/Overview/csharp/usageerrors2.cs | 4 +- .../FlagsAttribute/Overview/csharp/Program.cs | 1 + .../Overview/csharp/Project.csproj | 2 +- .../FlagsAttribute/Overview/csharp/flags1.cs | 21 +- .../FormatException/Overview/csharp/qa11.cs | 4 +- .../FormatException/Overview/csharp/qa21.cs | 4 +- .../IDisposable/Overview/csharp/calling1.cs | 5 +- .../IDisposable/Overview/csharp/calling2.cs | 5 +- .../Int32/Overview/csharp/Instantiate1.cs | 18 +- .../Int64/Overview/csharp/formatting1.cs | 10 +- .../Int64/Overview/csharp/instantiate1.cs | 18 +- .../Overview/csharp/Other/Enumerable2.cs | 5 +- .../Overview/csharp/Other/Enumerable3.cs | 5 +- .../Overview/csharp/Other/Enumerable4.cs | 5 +- .../Overview/csharp/Other/Enumerable5.cs | 4 +- .../Overview/csharp/Other/Enumerable6.cs | 5 +- .../Overview/csharp/Other/Iterating1.cs | 6 +- .../Overview/csharp/Other/Iterating2.cs | 6 +- .../Overview/csharp/Other/List_Sort1.cs | 4 +- .../Overview/csharp/Other/List_Sort2.cs | 4 +- .../Overview/csharp/Other/List_Sort3.cs | 4 +- .../Overview/csharp/Other/List_Sort4.cs | 4 +- .../System/Object/Equals/csharp/equals2.cs | 14 +- .../System/Object/Equals/csharp/equals3.cs | 8 +- .../System/Object/Equals/csharp/equals4.cs | 10 +- .../System/Object/Equals/csharp/equals_ref.cs | 10 +- .../Object/Equals/csharp/equals_val1.cs | 7 +- .../System/Object/Equals/csharp/equalssb1.cs | 12 +- .../Object/Finalize/csharp/finalize_safe.cs | 4 +- .../ToString/csharp/tostringoverload2.cs | 5 +- .../System/Random/Overview/csharp/array1.cs | 5 +- .../Random/Overview/csharp/threadsafeex1.cs | 19 +- .../Random/Overview/csharp/threadsafeex2.cs | 20 +- .../System/Random/Overview/csharp/unique.cs | 6 +- .../Random/Overview/csharp/uniquearray1.cs | 7 +- .../Single/CompareTo/csharp/compareto2.cs | 8 +- .../Single/CompareTo/csharp/compareto3.cs | 8 +- .../System/Single/Epsilon/csharp/epsilon.cs | 6 +- .../System/Single/Equals/csharp/equalsabs1.cs | 5 +- .../Single/Overview/csharp/PrecisionList5a.cs | 33 -- .../System/Single/Overview/csharp/Program.cs | 1 + .../Single/Overview/csharp/Project.csproj | 2 +- .../Single/Overview/csharp/comparison1.cs | 4 +- .../Single/Overview/csharp/comparison2.cs | 7 +- .../Single/Overview/csharp/comparison3.cs | 4 +- .../Single/Overview/csharp/comparison4.cs | 8 +- .../System/Single/Overview/csharp/convert1.cs | 6 +- .../System/Single/Overview/csharp/convert2.cs | 24 +- .../Single/Overview/csharp/exceptional1.cs | 6 +- .../Single/Overview/csharp/exceptional2.cs | 14 +- .../Single/Overview/csharp/precisionlist1.cs | 5 +- .../Single/Overview/csharp/precisionlist3.cs | 5 +- .../Single/Overview/csharp/precisionlist4a.cs | 29 +- .../Single/Overview/csharp/representation1.cs | 6 +- .../IsNullOrEmpty/csharp/NullString1.cs | 8 +- .../System/String/Overview/csharp/case2.cs | 7 +- .../System/String/Overview/csharp/compare3.cs | 8 +- .../System/String/Overview/csharp/compare4.cs | 28 +- .../String/Overview/csharp/equality1.cs | 10 +- .../System/String/Overview/csharp/index11.cs | 5 +- .../System/String/Overview/csharp/index2.cs | 5 +- .../System/String/Overview/csharp/search1.cs | 8 +- .../System/String/Overview/csharp/sort1.cs | 4 +- .../String/Overview/csharp/surrogate1.cs | 5 +- .../TimeSpan/Overview/csharp/instantiate1.cs | 10 +- .../TimeSpan/Overview/csharp/legacycode1.cs | 8 +- .../System/TimeSpan/Overview/csharp/zero1.cs | 4 +- .../TimeSpan/Parse/csharp/parsefailure1.cs | 8 +- .../TryParse/csharp/tryparsefailure1.cs | 6 +- .../System/Type/Overview/csharp/Equals1.cs | 11 +- .../System/Type/Overview/csharp/GetType1.cs | 5 +- .../Overview/csharp/Regex1.cs | 7 +- .../Overview/csharp/GettingVersions1.cs | 6 +- .../Version/Overview/csharp/clickonce.cs | 4 +- .../Version/Overview/csharp/comparisons1.cs | 4 +- .../Version/Overview/csharp/comparisons2.cs | 5 +- .../runtime-libraries/system-double.md | 13 +- .../runtime-libraries/system-single.md | 10 +- 139 files changed, 737 insertions(+), 1013 deletions(-) create mode 100644 docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/Program.cs delete mode 100644 docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/precisionlist5.cs create mode 100644 docs/fundamentals/runtime-libraries/snippets/System/FlagsAttribute/Overview/csharp/Program.cs delete mode 100644 docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/PrecisionList5a.cs create mode 100644 docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/Program.cs diff --git a/docs/fundamentals/runtime-libraries/snippets/System/AppContext/Overview/csharp/V1/Example4.cs b/docs/fundamentals/runtime-libraries/snippets/System/AppContext/Overview/csharp/V1/Example4.cs index 892091ce53d87..7b86a80ab5eb9 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/AppContext/Overview/csharp/V1/Example4.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/AppContext/Overview/csharp/V1/Example4.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Reflection; @@ -26,10 +26,9 @@ public static void Main() string substring = "archæ"; int position = StringLibrary1.SubstringStartsAt(value, substring); if (position >= 0) - Console.WriteLine("'{0}' found in '{1}' starting at position {2}", - substring, value, position); + Console.WriteLine($"'{substring}' found in '{value}' starting at position {position}"); else - Console.WriteLine("'{0}' not found in '{1}'", substring, value); + Console.WriteLine($"'{substring}' not found in '{value}'"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/AppContext/Overview/csharp/V2/Example6.cs b/docs/fundamentals/runtime-libraries/snippets/System/AppContext/Overview/csharp/V2/Example6.cs index 5f0178fdbbc98..c31bea12489a1 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/AppContext/Overview/csharp/V2/Example6.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/AppContext/Overview/csharp/V2/Example6.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Reflection; @@ -26,10 +26,9 @@ public static void Main() string substring = "archæ"; int position = StringLibrary2.SubstringStartsAt(value, substring); if (position >= 0) - Console.WriteLine("'{0}' found in '{1}' starting at position {2}", - substring, value, position); + Console.WriteLine($"'{substring}' found in '{value}' starting at position {position}"); else - Console.WriteLine("'{0}' not found in '{1}'", substring, value); + Console.WriteLine($"'{substring}' not found in '{value}'"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/AppContext/Overview/csharp/V3/Example8.cs b/docs/fundamentals/runtime-libraries/snippets/System/AppContext/Overview/csharp/V3/Example8.cs index 61c6bc08f0267..585f7e8eaec95 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/AppContext/Overview/csharp/V3/Example8.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/AppContext/Overview/csharp/V3/Example8.cs @@ -30,10 +30,9 @@ public static void Main() string substring = "archæ"; int position = StringLibrary.SubstringStartsAt(value, substring); if (position >= 0) - Console.WriteLine("'{0}' found in '{1}' starting at position {2}", - substring, value, position); + Console.WriteLine($"'{substring}' found in '{value}' starting at position {position}"); else - Console.WriteLine("'{0}' not found in '{1}'", substring, value); + Console.WriteLine($"'{substring}' not found in '{value}'"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/binary1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/binary1.cs index d8e080d73cd32..f0658a31294d0 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/binary1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/binary1.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example1 @@ -10,12 +10,11 @@ public static void Main() { // Get binary representation of flag. Byte value = BitConverter.GetBytes(flag)[0]; - Console.WriteLine("Original value: {0}", flag); - Console.WriteLine("Binary value: {0} ({1})", value, - GetBinaryString(value)); + Console.WriteLine($"Original value: {flag}"); + Console.WriteLine($"Binary value: {value} ({GetBinaryString(value)})"); // Restore the flag from its binary representation. bool newFlag = BitConverter.ToBoolean(new Byte[] { value }, 0); - Console.WriteLine("Restored value: {0}\n", flag); + Console.WriteLine($"Restored value: {flag}\n"); } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/conversion3.cs b/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/conversion3.cs index 3275d9c73d3a5..5cc0408da7fef 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/conversion3.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/conversion3.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example3 @@ -9,19 +9,19 @@ public static void Main() byte byteValue; byteValue = Convert.ToByte(flag); - Console.WriteLine("{0} -> {1}", flag, byteValue); + Console.WriteLine($"{flag} -> {byteValue}"); sbyte sbyteValue; sbyteValue = Convert.ToSByte(flag); - Console.WriteLine("{0} -> {1}", flag, sbyteValue); + Console.WriteLine($"{flag} -> {sbyteValue}"); double dblValue; dblValue = Convert.ToDouble(flag); - Console.WriteLine("{0} -> {1}", flag, dblValue); + Console.WriteLine($"{flag} -> {dblValue}"); int intValue; intValue = Convert.ToInt32(flag); - Console.WriteLine("{0} -> {1}", flag, intValue); + Console.WriteLine($"{flag} -> {intValue}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/operations2.cs b/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/operations2.cs index b9512e22ac45f..d64b1de060221 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/operations2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/operations2.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example6 @@ -13,8 +13,7 @@ public static void Main() foreach (var hasServiceCharge in hasServiceCharges) { Decimal total = subtotal + shippingCharge + (hasServiceCharge ? serviceCharge : 0); - Console.WriteLine("hasServiceCharge = {1}: The total is {0:C2}.", - total, hasServiceCharge); + Console.WriteLine($"hasServiceCharge = {hasServiceCharge}: The total is {total:C2}."); } } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/parse2.cs b/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/parse2.cs index b1a6478ca0dd0..6aa174bbb7562 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/parse2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/parse2.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example7 @@ -13,13 +13,13 @@ public static void Main() foreach (var value in values) { try { bool flag = Boolean.Parse(value); - Console.WriteLine("'{0}' --> {1}", value, flag); + Console.WriteLine($"'{value}' --> {flag}"); } catch (ArgumentException) { Console.WriteLine("Cannot parse a null string."); } catch (FormatException) { - Console.WriteLine("Cannot parse '{0}'.", value); + Console.WriteLine($"Cannot parse '{value}'."); } } Console.WriteLine(); @@ -27,9 +27,9 @@ public static void Main() foreach (var value in values) { bool flag = false; if (Boolean.TryParse(value, out flag)) - Console.WriteLine("'{0}' --> {1}", value, flag); + Console.WriteLine($"'{value}' --> {flag}"); else - Console.WriteLine("Unable to parse '{0}'", value); + Console.WriteLine($"Unable to parse '{value}'"); } } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/parse3.cs b/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/parse3.cs index a5eaa6d332f0c..b4b08281b353b 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/parse3.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/parse3.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example8 @@ -13,10 +13,10 @@ public static void Main() if (success) { // The method throws no exceptions. result = Convert.ToBoolean(number); - Console.WriteLine("Converted '{0}' to {1}", value, result); + Console.WriteLine($"Converted '{value}' to {result}"); } else { - Console.WriteLine("Unable to convert '{0}'", value); + Console.WriteLine($"Unable to convert '{value}'"); } } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/size1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/size1.cs index d5330f0f2f3d8..bafa129c0ee8e 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/size1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/size1.cs @@ -1,4 +1,4 @@ -// +// using System; public struct BoolStruct @@ -17,13 +17,13 @@ public static void Main() unsafe { BoolStruct b = new BoolStruct(); bool* addr = (bool*) &b; - Console.WriteLine("Size of BoolStruct: {0}", sizeof(BoolStruct)); + Console.WriteLine($"Size of BoolStruct: {sizeof(BoolStruct)}"); Console.WriteLine("Field offsets:"); - Console.WriteLine(" flag1: {0}", (bool*) &b.flag1 - addr); - Console.WriteLine(" flag1: {0}", (bool*) &b.flag2 - addr); - Console.WriteLine(" flag1: {0}", (bool*) &b.flag3 - addr); - Console.WriteLine(" flag1: {0}", (bool*) &b.flag4 - addr); - Console.WriteLine(" flag1: {0}", (bool*) &b.flag5 - addr); + Console.WriteLine($" flag1: {(bool*) &b.flag1 - addr}"); + Console.WriteLine($" flag1: {(bool*) &b.flag2 - addr}"); + Console.WriteLine($" flag1: {(bool*) &b.flag3 - addr}"); + Console.WriteLine($" flag1: {(bool*) &b.flag4 - addr}"); + Console.WriteLine($" flag1: {(bool*) &b.flag5 - addr}"); } } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/tostring1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/tostring1.cs index 3224625befc3e..09cfd324cd89c 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/tostring1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/tostring1.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example10 @@ -8,8 +8,8 @@ public static void Main() bool raining = false; bool busLate = true; - Console.WriteLine("It is raining: {0}", raining); - Console.WriteLine("The bus is late: {0}", busLate); + Console.WriteLine($"It is raining: {raining}"); + Console.WriteLine($"The bus is late: {busLate}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/tostring2.cs b/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/tostring2.cs index 6d527abb669bb..c691140aa0595 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/tostring2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/tostring2.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example11 @@ -8,10 +8,8 @@ public static void Main() bool raining = false; bool busLate = true; - Console.WriteLine("It is raining: {0}", - raining ? "Yes" : "No"); - Console.WriteLine("The bus is late: {0}", - busLate ? "Yes" : "No"); + Console.WriteLine($"It is raining: {(raining ? "Yes" : "No")}"); + Console.WriteLine($"The bus is late: {(busLate ? "Yes" : "No")}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Byte/Overview/csharp/bitwise1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Byte/Overview/csharp/bitwise1.cs index a866deaa853a9..5257ff862bfeb 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Byte/Overview/csharp/bitwise1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Byte/Overview/csharp/bitwise1.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Globalization; @@ -13,8 +13,7 @@ public static void Main() byte mask = 0xFE; foreach (string value in values) { Byte byteValue = Byte.Parse(value, NumberStyles.AllowHexSpecifier); - Console.WriteLine("{0} And {1} = {2}", byteValue, mask, - byteValue & mask); + Console.WriteLine($"{byteValue} And {mask} = {byteValue & mask}"); } } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Byte/Overview/csharp/bitwise2.cs b/docs/fundamentals/runtime-libraries/snippets/System/Byte/Overview/csharp/bitwise2.cs index e8283ef04d29c..010bb8c2004f2 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Byte/Overview/csharp/bitwise2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Byte/Overview/csharp/bitwise2.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Collections.Generic; using System.Globalization; @@ -20,12 +20,7 @@ public static void Main() foreach (ByteString strValue in values) { byte byteValue = Byte.Parse(strValue.Value, NumberStyles.AllowHexSpecifier); - Console.WriteLine("{0} ({1}) And {2} ({3}) = {4} ({5})", - strValue.Sign * byteValue, - Convert.ToString(byteValue, 2), - mask, Convert.ToString(mask, 2), - (strValue.Sign & Math.Sign(mask)) * (byteValue & mask), - Convert.ToString(byteValue & mask, 2)); + Console.WriteLine($"{strValue.Sign * byteValue} ({Convert.ToString(byteValue, 2)}) And {mask} ({Convert.ToString(mask, 2)}) = {(strValue.Sign & Math.Sign(mask)) * (byteValue & mask)} ({Convert.ToString(byteValue & mask, 2)})"); } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Byte/Overview/csharp/byteinstantiation1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Byte/Overview/csharp/byteinstantiation1.cs index d6040bc76b6c7..1879982e3db82 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Byte/Overview/csharp/byteinstantiation1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Byte/Overview/csharp/byteinstantiation1.cs @@ -1,4 +1,4 @@ -using System; +using System; public class Example2 { @@ -15,7 +15,7 @@ private static void InstantiateByAssignment() byte value1 = 64; byte value2 = 255; // - Console.WriteLine("{0} {1}", value1, value2); + Console.WriteLine($"{value1} {value2}"); } private static void InstantiateByNarrowingConversion() @@ -29,7 +29,7 @@ private static void InstantiateByNarrowingConversion() } catch (OverflowException) { - Console.WriteLine("{0} is out of range of a byte.", int1); + Console.WriteLine($"{int1} is out of range of a byte."); } double dbl2 = 3.997; @@ -40,7 +40,7 @@ private static void InstantiateByNarrowingConversion() } catch (OverflowException) { - Console.WriteLine("{0} is out of range of a byte.", dbl2); + Console.WriteLine($"{dbl2} is out of range of a byte."); } // The example displays the following output: // 128 @@ -59,11 +59,11 @@ private static void Parse() } catch (OverflowException) { - Console.WriteLine("'{0}' is out of range of a byte.", string1); + Console.WriteLine($"'{string1}' is out of range of a byte."); } catch (FormatException) { - Console.WriteLine("'{0}' is out of range of a byte.", string1); + Console.WriteLine($"'{string1}' is out of range of a byte."); } string string2 = "F9"; @@ -75,11 +75,11 @@ private static void Parse() } catch (OverflowException) { - Console.WriteLine("'{0}' is out of range of a byte.", string2); + Console.WriteLine($"'{string2}' is out of range of a byte."); } catch (FormatException) { - Console.WriteLine("'{0}' is out of range of a byte.", string2); + Console.WriteLine($"'{string2}' is out of range of a byte."); } // The example displays the following output: // 244 diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Byte/Overview/csharp/tobyte1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Byte/Overview/csharp/tobyte1.cs index 335666b9a0f1b..c5a27d237fcb2 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Byte/Overview/csharp/tobyte1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Byte/Overview/csharp/tobyte1.cs @@ -1,4 +1,4 @@ -using System; +using System; public class Example3 { @@ -30,10 +30,8 @@ private static void ConvertBoolean() bool falseFlag = false; bool trueFlag = true; - Console.WriteLine("{0} converts to {1}.", falseFlag, - Convert.ToByte(falseFlag)); - Console.WriteLine("{0} converts to {1}.", trueFlag, - Convert.ToByte(trueFlag)); + Console.WriteLine($"{falseFlag} converts to {Convert.ToByte(falseFlag)}."); + Console.WriteLine($"{trueFlag} converts to {Convert.ToByte(trueFlag)}."); // The example displays the following output: // False converts to 0. // True converts to 1. @@ -49,12 +47,11 @@ private static void ConvertChar() try { byte result = Convert.ToByte(ch); - Console.WriteLine("{0} is converted to {1}.", ch, result); + Console.WriteLine($"{ch} is converted to {result}."); } catch (OverflowException) { - Console.WriteLine("Unable to convert u+{0} to a byte.", - Convert.ToInt16(ch).ToString("X4")); + Console.WriteLine($"Unable to convert u+{Convert.ToInt16(ch).ToString("X4")} to a byte."); } } // The example displays the following output: @@ -75,14 +72,11 @@ private static void ConvertInt16() try { result = Convert.ToByte(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); } catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Byte type.", - number.GetType().Name, number); + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the Byte type."); } } // The example displays the following output: @@ -105,14 +99,11 @@ private static void ConvertInt32() try { result = Convert.ToByte(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); } catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Byte type.", - number.GetType().Name, number); + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the Byte type."); } } // The example displays the following output: @@ -135,14 +126,11 @@ private static void ConvertInt64() try { result = Convert.ToByte(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); } catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Byte type.", - number.GetType().Name, number); + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the Byte type."); } } // The example displays the following output: @@ -166,24 +154,19 @@ private static void ConvertObject() try { result = Convert.ToByte(value); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - value.GetType().Name, value, - result.GetType().Name, result); + Console.WriteLine($"Converted the {value.GetType().Name} value {value} to the {result.GetType().Name} value {result}."); } catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Byte type.", - value.GetType().Name, value); + Console.WriteLine($"The {value.GetType().Name} value {value} is outside the range of the Byte type."); } catch (FormatException) { - Console.WriteLine("The {0} value {1} is not in a recognizable format.", - value.GetType().Name, value); + Console.WriteLine($"The {value.GetType().Name} value {value} is not in a recognizable format."); } catch (InvalidCastException) { - Console.WriteLine("No conversion to a Byte exists for the {0} value {1}.", - value.GetType().Name, value); + Console.WriteLine($"No conversion to a Byte exists for the {value.GetType().Name} value {value}."); } } // The example displays the following output: @@ -211,14 +194,11 @@ private static void ConvertSByte() try { result = Convert.ToByte(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); } catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Byte type.", - number.GetType().Name, number); + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the Byte type."); } } // The example displays the following output: @@ -240,14 +220,11 @@ private static void ConvertUInt16() try { result = Convert.ToByte(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); } catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Byte type.", - number.GetType().Name, number); + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the Byte type."); } } // The example displays the following output: @@ -268,14 +245,11 @@ private static void ConvertUInt32() try { result = Convert.ToByte(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); } catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Byte type.", - number.GetType().Name, number); + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the Byte type."); } } // The example displays the following output: @@ -296,14 +270,11 @@ private static void ConvertUInt64() try { result = Convert.ToByte(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); } catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Byte type.", - number.GetType().Name, number); + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the Byte type."); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Char/Overview/csharp/GetUnicodeCategory3.cs b/docs/fundamentals/runtime-libraries/snippets/System/Char/Overview/csharp/GetUnicodeCategory3.cs index 54907ae9df9ff..a41456fc514a0 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Char/Overview/csharp/GetUnicodeCategory3.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Char/Overview/csharp/GetUnicodeCategory3.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Globalization; @@ -10,7 +10,7 @@ public static void Main() String s = "The red car drove down the long, narrow, secluded road."; // Determine the category of each character. foreach (var ch in s) - Console.WriteLine("'{0}': {1}", ch, Char.GetUnicodeCategory(ch)); + Console.WriteLine($"'{ch}': {Char.GetUnicodeCategory(ch)}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Char/Overview/csharp/textelements2.cs b/docs/fundamentals/runtime-libraries/snippets/System/Char/Overview/csharp/textelements2.cs index 5ffa26eb505ca..c039c6183736f 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Char/Overview/csharp/textelements2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Char/Overview/csharp/textelements2.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example5 @@ -9,7 +9,7 @@ public static void Main() for (int ctr = 0x10107; ctr <= 0x10110; ctr++) // Range of Aegean numbers. result += Char.ConvertFromUtf32(ctr); - Console.WriteLine("The string contains {0} characters.", result.Length); + Console.WriteLine($"The string contains {result.Length} characters."); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Char/Overview/csharp/textelements2a.cs b/docs/fundamentals/runtime-libraries/snippets/System/Char/Overview/csharp/textelements2a.cs index 73e2c2a0c9e32..e6bcb56f7d7cf 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Char/Overview/csharp/textelements2a.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Char/Overview/csharp/textelements2a.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Globalization; @@ -11,8 +11,7 @@ public static void Main() result += Char.ConvertFromUtf32(ctr); StringInfo si = new StringInfo(result); - Console.WriteLine("The string contains {0} characters.", - si.LengthInTextElements); + Console.WriteLine($"The string contains {si.LengthInTextElements} characters."); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Console/Overview/csharp/example3.cs b/docs/fundamentals/runtime-libraries/snippets/System/Console/Overview/csharp/example3.cs index b802d1f046c05..1e977ce6ccef8 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Console/Overview/csharp/example3.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Console/Overview/csharp/example3.cs @@ -7,159 +7,170 @@ public static class DisplayChars { - private static void Main(string[] args) - { - uint rangeStart = 0; - uint rangeEnd = 0; - bool setOutputEncodingToUnicode = true; - // Get the current encoding so we can restore it. - Encoding originalOutputEncoding = Console.OutputEncoding; - - try - { - switch(args.Length) - { - case 2: - rangeStart = uint.Parse(args[0], NumberStyles.HexNumber); - rangeEnd = uint.Parse(args[1], NumberStyles.HexNumber); - setOutputEncodingToUnicode = true; - break; - case 3: - if (! uint.TryParse(args[0], NumberStyles.HexNumber, null, out rangeStart)) - throw new ArgumentException(String.Format("{0} is not a valid hexadecimal number.", args[0])); - - if (!uint.TryParse(args[1], NumberStyles.HexNumber, null, out rangeEnd)) - throw new ArgumentException(String.Format("{0} is not a valid hexadecimal number.", args[1])); - - bool.TryParse(args[2], out setOutputEncodingToUnicode); - break; - default: - Console.WriteLine("Usage: {0} <{1}> <{2}> [{3}]", - Environment.GetCommandLineArgs()[0], - "startingCodePointInHex", - "endingCodePointInHex", - ""); - return; - } - - if (setOutputEncodingToUnicode) { - // This won't work before .NET Framework 4.5. - try { - // Set encoding using endianness of this system. - // We're interested in displaying individual Char objects, so - // we don't want a Unicode BOM or exceptions to be thrown on - // invalid Char values. - Console.OutputEncoding = new UnicodeEncoding(! BitConverter.IsLittleEndian, false); - Console.WriteLine("\nOutput encoding set to UTF-16"); + private static void Main(string[] args) + { + uint rangeStart = 0; + uint rangeEnd = 0; + bool setOutputEncodingToUnicode = true; + // Get the current encoding so we can restore it. + Encoding originalOutputEncoding = Console.OutputEncoding; + + try + { + switch (args.Length) + { + case 2: + rangeStart = uint.Parse(args[0], NumberStyles.HexNumber); + rangeEnd = uint.Parse(args[1], NumberStyles.HexNumber); + setOutputEncodingToUnicode = true; + break; + case 3: + if (!uint.TryParse(args[0], NumberStyles.HexNumber, null, out rangeStart)) + throw new ArgumentException(string.Format("{0} is not a valid hexadecimal number.", args[0])); + + if (!uint.TryParse(args[1], NumberStyles.HexNumber, null, out rangeEnd)) + throw new ArgumentException(string.Format("{0} is not a valid hexadecimal number.", args[1])); + + _ = bool.TryParse(args[2], out setOutputEncodingToUnicode); + break; + default: + Console.WriteLine("Usage: {0} <{1}> <{2}> [{3}]", + Environment.GetCommandLineArgs()[0], + "startingCodePointInHex", + "endingCodePointInHex", + ""); + return; } - catch (IOException) { - Console.OutputEncoding = new UTF8Encoding(); - Console.WriteLine("Output encoding set to UTF-8"); - } - } - else { - Console.WriteLine("The console encoding is {0} (code page {1})", - Console.OutputEncoding.EncodingName, - Console.OutputEncoding.CodePage); - } - DisplayRange(rangeStart, rangeEnd); - } - catch (ArgumentException ex) { - Console.WriteLine(ex.Message); - } - finally { - // Restore console environment. - Console.OutputEncoding = originalOutputEncoding; - } - } - - public static void DisplayRange(uint start, uint end) - { - const uint upperRange = 0x10FFFF; - const uint surrogateStart = 0xD800; - const uint surrogateEnd = 0xDFFF; - - if (end <= start) { - uint t = start; - start = end; - end = t; - } - - // Check whether the start or end range is outside of last plane. - if (start > upperRange) - throw new ArgumentException(String.Format("0x{0:X5} is outside the upper range of Unicode code points (0x{1:X5})", - start, upperRange)); - if (end > upperRange) - throw new ArgumentException(String.Format("0x{0:X5} is outside the upper range of Unicode code points (0x{0:X5})", - end, upperRange)); - - // Since we're using 21-bit code points, we can't use U+D800 to U+DFFF. - if ((start < surrogateStart & end > surrogateStart) || (start >= surrogateStart & start <= surrogateEnd )) - throw new ArgumentException(String.Format("0x{0:X5}-0x{1:X5} includes the surrogate pair range 0x{2:X5}-0x{3:X5}", - start, end, surrogateStart, surrogateEnd)); - uint last = RoundUpToMultipleOf(0x10, end); - uint first = RoundDownToMultipleOf(0x10, start); - - uint rows = (last - first) / 0x10; - - for (uint r = 0; r < rows; ++r) { - // Display the row header. - Console.Write("{0:x5} ", first + 0x10 * r); - - for (uint c = 0; c < 0x10; ++c) { - uint cur = (first + 0x10 * r + c); - if (cur < start) { - Console.Write($" {(char)(0x20)} "); - } - else if (end < cur) { - Console.Write($" {(char)(0x20)} "); + + if (setOutputEncodingToUnicode) + { + try + { + // Set encoding using endianness of this system. + // We're interested in displaying individual Char objects, so + // we don't want a Unicode BOM or exceptions to be thrown on + // invalid Char values. + Console.OutputEncoding = new UnicodeEncoding(!BitConverter.IsLittleEndian, false); + Console.WriteLine("\nOutput encoding set to UTF-16"); + } + catch (IOException) + { + Console.OutputEncoding = new UTF8Encoding(); + Console.WriteLine("Output encoding set to UTF-8"); + } } - else { - // the cast to int is safe, since we know that val <= upperRange. - String chars = Char.ConvertFromUtf32( (int) cur); - // Display a space for code points that are not valid characters. - if (CharUnicodeInfo.GetUnicodeCategory(chars[0]) == - UnicodeCategory.OtherNotAssigned) - Console.Write($" {(char)(0x20)} "); - // Display a space for code points in the private use area. - else if (CharUnicodeInfo.GetUnicodeCategory(chars[0]) == - UnicodeCategory.PrivateUse) - Console.Write($" {(char)(0x20)} "); - // Is surrogate pair a valid character? - // Note that the console will interpret the high and low surrogate - // as separate (and unrecognizable) characters. - else if (chars.Length > 1 && CharUnicodeInfo.GetUnicodeCategory(chars, 0) == - UnicodeCategory.OtherNotAssigned) - Console.Write($" {(char)(0x20)} "); - else - Console.Write($" {chars} "); + else + { + Console.WriteLine($"The console encoding is {Console.OutputEncoding.EncodingName} (code page {Console.OutputEncoding.CodePage})"); } - - switch (c) { - case 3: case 11: - Console.Write("-"); - break; - case 7: - Console.Write("--"); - break; + DisplayRange(rangeStart, rangeEnd); + } + catch (ArgumentException ex) + { + Console.WriteLine(ex.Message); + } + finally + { + // Restore console environment. + Console.OutputEncoding = originalOutputEncoding; + } + } + + public static void DisplayRange(uint start, uint end) + { + const uint upperRange = 0x10FFFF; + const uint surrogateStart = 0xD800; + const uint surrogateEnd = 0xDFFF; + + if (end <= start) + { + uint t = start; + start = end; + end = t; + } + + // Check whether the start or end range is outside of last plane. + if (start > upperRange) + throw new ArgumentException(string.Format("0x{0:X5} is outside the upper range of Unicode code points (0x{1:X5})", + start, upperRange)); + if (end > upperRange) + throw new ArgumentException(string.Format("0x{0:X5} is outside the upper range of Unicode code points (0x{0:X5})", + end, upperRange)); + + // Since we're using 21-bit code points, we can't use U+D800 to U+DFFF. + if ((start < surrogateStart & end > surrogateStart) || (start >= surrogateStart & start <= surrogateEnd)) + throw new ArgumentException(string.Format("0x{0:X5}-0x{1:X5} includes the surrogate pair range 0x{2:X5}-0x{3:X5}", + start, end, surrogateStart, surrogateEnd)); + uint last = RoundUpToMultipleOf(0x10, end); + uint first = RoundDownToMultipleOf(0x10, start); + + uint rows = (last - first) / 0x10; + + for (uint r = 0; r < rows; ++r) + { + // Display the row header. + Console.Write("{0:x5} ", first + 0x10 * r); + + for (uint c = 0; c < 0x10; ++c) + { + uint cur = (first + 0x10 * r + c); + if (cur < start) + { + Console.Write($" {(char)(0x20)} "); + } + else if (end < cur) + { + Console.Write($" {(char)(0x20)} "); + } + else + { + // the cast to int is safe, since we know that val <= upperRange. + String chars = Char.ConvertFromUtf32((int)cur); + // Display a space for code points that are not valid characters. + if (CharUnicodeInfo.GetUnicodeCategory(chars[0]) == + UnicodeCategory.OtherNotAssigned) + Console.Write($" {(char)(0x20)} "); + // Display a space for code points in the private use area. + else if (CharUnicodeInfo.GetUnicodeCategory(chars[0]) == + UnicodeCategory.PrivateUse) + Console.Write($" {(char)(0x20)} "); + // Is surrogate pair a valid character? + // Note that the console will interpret the high and low surrogate + // as separate (and unrecognizable) characters. + else if (chars.Length > 1 && CharUnicodeInfo.GetUnicodeCategory(chars, 0) == + UnicodeCategory.OtherNotAssigned) + Console.Write($" {(char)(0x20)} "); + else + Console.Write($" {chars} "); + } + + switch (c) + { + case 3: + case 11: + Console.Write("-"); + break; + case 7: + Console.Write("--"); + break; + } } - } - Console.WriteLine(); - if (0 < r && r % 0x10 == 0) Console.WriteLine(); - } - } - - private static uint RoundUpToMultipleOf(uint b, uint u) - { - return RoundDownToMultipleOf(b, u) + b; - } - - private static uint RoundDownToMultipleOf(uint b, uint u) - { - return u - (u % b); - } + if (0 < r && r % 0x10 == 0) + Console.WriteLine(); + } + } + + private static uint RoundUpToMultipleOf(uint b, uint u) + { + return RoundDownToMultipleOf(b, u) + b; + } + + private static uint RoundDownToMultipleOf(uint b, uint u) + { + return u - (u % b); + } } // If the example is run with the command line // DisplayChars 0400 04FF true diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Console/Overview/csharp/unicode1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Console/Overview/csharp/unicode1.cs index c0cab16eacc9e..bb493473c6ecc 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Console/Overview/csharp/unicode1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Console/Overview/csharp/unicode1.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example3 @@ -16,8 +16,7 @@ public static void Main() codePoint++; } - Console.WriteLine("Current code page: {0}\n", - Console.OutputEncoding.CodePage); + Console.WriteLine($"Current code page: {Console.OutputEncoding.CodePage}\n"); // Display the characters. foreach (var ch in chars) { diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Convert/Overview/csharp/NonDecimal1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Convert/Overview/csharp/NonDecimal1.cs index 98820cc225fbe..1947fda619195 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Convert/Overview/csharp/NonDecimal1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Convert/Overview/csharp/NonDecimal1.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example @@ -11,8 +11,7 @@ public static void Main() String s = Convert.ToString(value, baseValue); short value2 = Convert.ToInt16(s, baseValue); - Console.WriteLine("{0} --> {1} (base {2}) --> {3}", - value, s, baseValue, value2); + Console.WriteLine($"{value} --> {s} (base {baseValue}) --> {value2}"); } } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Convert/Overview/csharp/converter.cs b/docs/fundamentals/runtime-libraries/snippets/System/Convert/Overview/csharp/converter.cs index 6b63efc93a520..3711b03f21bca 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Convert/Overview/csharp/converter.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Convert/Overview/csharp/converter.cs @@ -1,4 +1,4 @@ -using System; +using System; namespace ConvertSnippet { @@ -55,8 +55,7 @@ static void Main(string[] args) "Overflow in string to int conversion."); } - System.Console.WriteLine("Your integer as a double is {0}", - System.Convert.ToDouble(newInteger)); + System.Console.WriteLine($"Your integer as a double is {System.Convert.ToDouble(newInteger)}"); // } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Convert/ToInt32/csharp/toint32_1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Convert/ToInt32/csharp/toint32_1.cs index 1f54fdec6e496..050eb2bb9a995 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Convert/ToInt32/csharp/toint32_1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Convert/ToInt32/csharp/toint32_1.cs @@ -1,4 +1,4 @@ -using System; +using System; public class Example { @@ -39,10 +39,8 @@ private static void ConvertBoolean() bool falseFlag = false; bool trueFlag = true; - Console.WriteLine("{0} converts to {1}.", falseFlag, - Convert.ToInt32(falseFlag)); - Console.WriteLine("{0} converts to {1}.", trueFlag, - Convert.ToInt32(trueFlag)); + Console.WriteLine($"{falseFlag} converts to {Convert.ToInt32(falseFlag)}."); + Console.WriteLine($"{trueFlag} converts to {Convert.ToInt32(trueFlag)}."); // The example displays the following output: // False converts to 0. // True converts to 1. @@ -58,9 +56,7 @@ private static void ConvertByte() foreach (byte byteValue in bytes) { result = Convert.ToInt32(byteValue); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - byteValue.GetType().Name, byteValue, - result.GetType().Name, result); + Console.WriteLine($"Converted the {byteValue.GetType().Name} value {byteValue} to the {result.GetType().Name} value {result}."); } // The example displays the following output: // Converted the Byte value 0 to the Int32 value 0. @@ -81,13 +77,10 @@ private static void ConvertChar() { try { result = Convert.ToInt32(ch); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - ch.GetType().Name, ch, - result.GetType().Name, result); + Console.WriteLine($"Converted the {ch.GetType().Name} value '{ch}' to the {result.GetType().Name} value {result}."); } catch (OverflowException) { - Console.WriteLine("Unable to convert u+{0} to an Int32.", - ((int)ch).ToString("X4")); + Console.WriteLine($"Unable to convert u+{((int)ch).ToString("X4")} to an Int32."); } } // The example displays the following output: @@ -111,13 +104,10 @@ private static void ConvertDecimal() { try { result = Convert.ToInt32(value); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - value.GetType().Name, value, - result.GetType().Name, result); + Console.WriteLine($"Converted the {value.GetType().Name} value '{value}' to the {result.GetType().Name} value {result}."); } catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the Int32 type.", - value); + Console.WriteLine($"{value} is outside the range of the Int32 type."); } } // The example displays the following output: @@ -143,12 +133,10 @@ private static void ConvertDouble() { try { result = Convert.ToInt32(value); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - value.GetType().Name, value, - result.GetType().Name, result); + Console.WriteLine($"Converted the {value.GetType().Name} value '{value}' to the {result.GetType().Name} value {result}."); } catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the Int32 type.", value); + Console.WriteLine($"{value} is outside the range of the Int32 type."); } } // -1.79769313486232E+308 is outside the range of the Int32 type. @@ -172,9 +160,7 @@ private static void ConvertInt16() foreach (short number in numbers) { result = Convert.ToInt32(number); - Console.WriteLine("Converted the {0} value {1} to a {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to a {result.GetType().Name} value {result}."); } // The example displays the following output: // Converted the Int16 value -32768 to a Int32 value -32768. @@ -195,13 +181,10 @@ private static void ConvertInt64() { try { result = Convert.ToInt32(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); } catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Int32 type.", - number.GetType().Name, number); + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the Int32 type."); } } // The example displays the following output: @@ -226,21 +209,16 @@ private static void ConvertObject() { try { result = Convert.ToInt32(value); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - value.GetType().Name, value, - result.GetType().Name, result); + Console.WriteLine($"Converted the {value.GetType().Name} value {value} to the {result.GetType().Name} value {result}."); } catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Int32 type.", - value.GetType().Name, value); + Console.WriteLine($"The {value.GetType().Name} value {value} is outside the range of the Int32 type."); } catch (FormatException) { - Console.WriteLine("The {0} value {1} is not in a recognizable format.", - value.GetType().Name, value); + Console.WriteLine($"The {value.GetType().Name} value {value} is not in a recognizable format."); } catch (InvalidCastException) { - Console.WriteLine("No conversion to an Int32 exists for the {0} value {1}.", - value.GetType().Name, value); + Console.WriteLine($"No conversion to an Int32 exists for the {value.GetType().Name} value {value}."); } } // The example displays the following output: @@ -269,9 +247,7 @@ private static void ConvertSByte() foreach (sbyte number in numbers) { result = Convert.ToInt32(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); } // The example displays the following output: // Converted the SByte value -128 to the Int32 value -128. @@ -293,11 +269,10 @@ private static void ConvertSingle() { try { result = Convert.ToInt32(value); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - value.GetType().Name, value, result.GetType().Name, result); + Console.WriteLine($"Converted the {value.GetType().Name} value {value} to the {result.GetType().Name} value {result}."); } catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the Int32 type.", value); + Console.WriteLine($"{value} is outside the range of the Int32 type."); } } // The example displays the following output: @@ -324,15 +299,13 @@ private static void ConvertString() { try { result = Convert.ToInt32(value); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - value.GetType().Name, value, result.GetType().Name, result); + Console.WriteLine($"Converted the {value.GetType().Name} value '{value}' to the {result.GetType().Name} value {result}."); } catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the Int32 type.", value); + Console.WriteLine($"{value} is outside the range of the Int32 type."); } catch (FormatException) { - Console.WriteLine("The {0} value '{1}' is not in a recognizable format.", - value.GetType().Name, value); + Console.WriteLine($"The {value.GetType().Name} value '{value}' is not in a recognizable format."); } } // The example displays the following output: @@ -357,13 +330,10 @@ private static void ConvertUInt16() { try { result = Convert.ToInt32(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); } catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Int32 type.", - number.GetType().Name, number); + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the Int32 type."); } } // The example displays the following output: @@ -383,13 +353,10 @@ private static void ConvertUInt32() { try { result = Convert.ToInt32(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); } catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Int32 type.", - number.GetType().Name, number); + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the Int32 type."); } } // The example displays the following output: @@ -409,13 +376,10 @@ private static void ConvertUInt64() { try { result = Convert.ToInt32(number); - Console.WriteLine("Converted the {0} value {1} to a {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to a {result.GetType().Name} value {result}."); } catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Int32 type.", - number.GetType().Name, number); + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the Int32 type."); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Convert/ToInt64/csharp/toint64_1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Convert/ToInt64/csharp/toint64_1.cs index 4a8d98221e433..2e0b517ad28bb 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Convert/ToInt64/csharp/toint64_1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Convert/ToInt64/csharp/toint64_1.cs @@ -1,4 +1,4 @@ -using System; +using System; public class Example { @@ -39,10 +39,8 @@ private static void ConvertBoolean() bool falseFlag = false; bool trueFlag = true; - Console.WriteLine("{0} converts to {1}.", falseFlag, - Convert.ToInt64(falseFlag)); - Console.WriteLine("{0} converts to {1}.", trueFlag, - Convert.ToInt64(trueFlag)); + Console.WriteLine($"{falseFlag} converts to {Convert.ToInt64(falseFlag)}."); + Console.WriteLine($"{trueFlag} converts to {Convert.ToInt64(trueFlag)}."); // The example displays the following output: // False converts to 0. // True converts to 1. @@ -58,9 +56,7 @@ private static void ConvertByte() foreach (byte byteValue in bytes) { result = Convert.ToInt64(byteValue); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - byteValue.GetType().Name, byteValue, - result.GetType().Name, result); + Console.WriteLine($"Converted the {byteValue.GetType().Name} value {byteValue} to the {result.GetType().Name} value {result}."); } // The example displays the following output: // Converted the Byte value 0 to the Int64 value 0. @@ -80,9 +76,7 @@ private static void ConvertChar() foreach (char ch in chars) { result = Convert.ToInt64(ch); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - ch.GetType().Name, ch, - result.GetType().Name, result); + Console.WriteLine($"Converted the {ch.GetType().Name} value '{ch}' to the {result.GetType().Name} value {result}."); } // The example displays the following output: // Converted the Char value 'a' to the Int64 value 97. @@ -105,13 +99,10 @@ private static void ConvertDecimal() { try { result = Convert.ToInt64(value); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - value.GetType().Name, value, - result.GetType().Name, result); + Console.WriteLine($"Converted the {value.GetType().Name} value '{value}' to the {result.GetType().Name} value {result}."); } catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the Int64 type.", - value); + Console.WriteLine($"{value} is outside the range of the Int64 type."); } } // The example displays the following output: @@ -137,12 +128,10 @@ private static void ConvertDouble() { try { result = Convert.ToInt64(value); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - value.GetType().Name, value, - result.GetType().Name, result); + Console.WriteLine($"Converted the {value.GetType().Name} value '{value}' to the {result.GetType().Name} value {result}."); } catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the Int64 type.", value); + Console.WriteLine($"{value} is outside the range of the Int64 type."); } } // -1.7976931348623157E+308 is outside the range of the Int64 type. @@ -166,9 +155,7 @@ private static void ConvertInt16() foreach (short number in numbers) { result = Convert.ToInt64(number); - Console.WriteLine("Converted the {0} value {1} to a {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to a {result.GetType().Name} value {result}."); } // The example displays the following output: // Converted the Int16 value -32768 to a Int32 value -32768. @@ -188,9 +175,7 @@ private static void ConvertInt32() foreach (int number in numbers) { result = Convert.ToInt64(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); } // The example displays the following output: // Converted the Int32 value -2147483648 to the Int64 value -2147483648. @@ -214,21 +199,16 @@ private static void ConvertObject() { try { result = Convert.ToInt64(value); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - value.GetType().Name, value, - result.GetType().Name, result); + Console.WriteLine($"Converted the {value.GetType().Name} value {value} to the {result.GetType().Name} value {result}."); } catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Int64 type.", - value.GetType().Name, value); + Console.WriteLine($"The {value.GetType().Name} value {value} is outside the range of the Int64 type."); } catch (FormatException) { - Console.WriteLine("The {0} value {1} is not in a recognizable format.", - value.GetType().Name, value); + Console.WriteLine($"The {value.GetType().Name} value {value} is not in a recognizable format."); } catch (InvalidCastException) { - Console.WriteLine("No conversion to an Int64 exists for the {0} value {1}.", - value.GetType().Name, value); + Console.WriteLine($"No conversion to an Int64 exists for the {value.GetType().Name} value {value}."); } } // The example displays the following output: @@ -257,9 +237,7 @@ private static void ConvertSByte() foreach (sbyte number in numbers) { result = Convert.ToInt64(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); } // The example displays the following output: // Converted the SByte value -128 to the Int64 value -128. @@ -281,11 +259,10 @@ private static void ConvertSingle() { try { result = Convert.ToInt64(value); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - value.GetType().Name, value, result.GetType().Name, result); + Console.WriteLine($"Converted the {value.GetType().Name} value {value} to the {result.GetType().Name} value {result}."); } catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the Int64 type.", value); + Console.WriteLine($"{value} is outside the range of the Int64 type."); } } // The example displays the following output: @@ -312,15 +289,13 @@ private static void ConvertString() { try { result = Convert.ToInt64(value); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - value.GetType().Name, value, result.GetType().Name, result); + Console.WriteLine($"Converted the {value.GetType().Name} value '{value}' to the {result.GetType().Name} value {result}."); } catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the Int64 type.", value); + Console.WriteLine($"{value} is outside the range of the Int64 type."); } catch (FormatException) { - Console.WriteLine("The {0} value '{1}' is not in a recognizable format.", - value.GetType().Name, value); + Console.WriteLine($"The {value.GetType().Name} value '{value}' is not in a recognizable format."); } } // The example displays the following output: @@ -345,13 +320,10 @@ private static void ConvertUInt16() { try { result = Convert.ToInt64(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); } catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Int64 type.", - number.GetType().Name, number); + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the Int64 type."); } } // The example displays the following output: @@ -370,9 +342,7 @@ private static void ConvertUInt32() foreach (uint number in numbers) { result = Convert.ToInt64(number); - Console.WriteLine("Converted the {0} value {1:N0} to the {2} value {3:N0}.", - number.GetType().Name, number, - result.GetType().Name, result); + Console.WriteLine($"Converted the {number.GetType().Name} value {number:N0} to the {result.GetType().Name} value {result:N0}."); } // The example displays the following output: // Converted the UInt32 value 0 to the Int64 value 0. @@ -391,13 +361,10 @@ private static void ConvertUInt64() { try { result = Convert.ToInt64(number); - Console.WriteLine("Converted the {0} value {1} to a {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to a {result.GetType().Name} value {result}."); } catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Int64 type.", - number.GetType().Name, number); + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the Int64 type."); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/DateTime/FromBinary/csharp/frombinary1.cs b/docs/fundamentals/runtime-libraries/snippets/System/DateTime/FromBinary/csharp/frombinary1.cs index d84eeb42a6297..7a1e3c7fe5812 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/DateTime/FromBinary/csharp/frombinary1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/DateTime/FromBinary/csharp/frombinary1.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example @@ -8,13 +8,10 @@ public static void Main() DateTime localDate = new DateTime(2010, 3, 14, 2, 30, 0, DateTimeKind.Local); long binLocal = localDate.ToBinary(); if (TimeZoneInfo.Local.IsInvalidTime(localDate)) - Console.WriteLine("{0} is an invalid time in the {1} zone.", - localDate, - TimeZoneInfo.Local.StandardName); + Console.WriteLine($"{localDate} is an invalid time in the {TimeZoneInfo.Local.StandardName} zone."); DateTime localDate2 = DateTime.FromBinary(binLocal); - Console.WriteLine("{0} = {1}: {2}", - localDate, localDate2, localDate.Equals(localDate2)); + Console.WriteLine($"{localDate} = {localDate2}: {localDate.Equals(localDate2)}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/DateTime/Overview/csharp/Persistence.cs b/docs/fundamentals/runtime-libraries/snippets/System/DateTime/Overview/csharp/Persistence.cs index 16585f5f3268b..954339cd52531 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/DateTime/Overview/csharp/Persistence.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/DateTime/Overview/csharp/Persistence.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Globalization; using System.IO; @@ -66,8 +66,7 @@ private static void RestoreLocalDatesFromString() string[] inputValues = sr.ReadToEnd().Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries); sr.Close(); - Console.WriteLine("The dates on an {0} system:", - Thread.CurrentThread.CurrentCulture.Name); + Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:"); foreach (var inputValue in inputValues) { DateTime dateValue; @@ -136,15 +135,13 @@ private static void SaveDatesAsInvariantStrings() private static void RestoreDatesAsInvariantStrings() { TimeZoneInfo.ClearCachedData(); - Console.WriteLine("Current Time Zone: {0}", - TimeZoneInfo.Local.DisplayName); + Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}"); Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB"); StreamReader sr = new StreamReader(filenameTxt); string[] inputValues = sr.ReadToEnd().Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries); sr.Close(); - Console.WriteLine("The dates on an {0} system:", - Thread.CurrentThread.CurrentCulture.Name); + Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:"); foreach (var inputValue in inputValues) { DateTime dateValue; @@ -155,7 +152,7 @@ private static void RestoreDatesAsInvariantStrings() } else { - Console.WriteLine("Cannot parse '{0}'", inputValue); + Console.WriteLine($"Cannot parse '{inputValue}'"); } } Console.WriteLine("Restored dates..."); diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Delegate/CreateDelegate/csharp/openClosedOver.cs b/docs/fundamentals/runtime-libraries/snippets/System/Delegate/CreateDelegate/csharp/openClosedOver.cs index 9b7de194634a9..41d6b617f5774 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Delegate/CreateDelegate/csharp/openClosedOver.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Delegate/CreateDelegate/csharp/openClosedOver.cs @@ -1,4 +1,4 @@ -// All four permutations of instance/static with open/closed. +// All four permutations of instance/static with open/closed. // // using System; @@ -27,7 +27,7 @@ public void M1(string s) public static void M2(string s) { - Console.WriteLine("Static method M2 on C: s = {0}", s); + Console.WriteLine($"Static method M2 on C: s = {s}"); } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Delegate/CreateDelegate/csharp/source.cs b/docs/fundamentals/runtime-libraries/snippets/System/Delegate/CreateDelegate/csharp/source.cs index b3177b94dfd4c..715f8658cad22 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Delegate/CreateDelegate/csharp/source.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Delegate/CreateDelegate/csharp/source.cs @@ -1,4 +1,4 @@ -// Showing all the things D(A) can bind to. +// Showing all the things D(A) can bind to. // // using System; @@ -26,13 +26,12 @@ public void M1(C1 c) public void M2() { - Console.WriteLine("Instance method M2() on C1: this.id = {0}", - this.id); + Console.WriteLine($"Instance method M2() on C1: this.id = {this.id}"); } public static void M3(C1 c) { - Console.WriteLine("Static method M3(C1 c) on C1: c.ID = {0}", c.ID); + Console.WriteLine($"Static method M3(C1 c) on C1: c.ID = {c.ID}"); } public static void M4(C1 c1, C1 c2) @@ -46,19 +45,17 @@ public class F { public void M1(C1 c) { - Console.WriteLine("Instance method M1(C1 c) on F: c.ID = {0}", - c.ID); + Console.WriteLine($"Instance method M1(C1 c) on F: c.ID = {c.ID}"); } public static void M3(C1 c) { - Console.WriteLine("Static method M3(C1 c) on F: c.ID = {0}", c.ID); + Console.WriteLine($"Static method M3(C1 c) on F: c.ID = {c.ID}"); } public static void M4(F f, C1 c) { - Console.WriteLine("Static method M4(F f, C1 c) on F: c.ID = {0}", - c.ID); + Console.WriteLine($"Static method M4(F f, C1 c) on F: c.ID = {c.ID}"); } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Double/CompareTo/csharp/compareto2.cs b/docs/fundamentals/runtime-libraries/snippets/System/Double/CompareTo/csharp/compareto2.cs index 2b9edd2fe8e2f..11c020825add0 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Double/CompareTo/csharp/compareto2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Double/CompareTo/csharp/compareto2.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example @@ -7,10 +7,8 @@ public static void Main() { double value1 = 6.185; double value2 = value1 * .1 / .1; - Console.WriteLine("Comparing {0} and {1}: {2}\n", - value1, value2, value1.CompareTo(value2)); - Console.WriteLine("Comparing {0:R} and {1:R}: {2}", - value1, value2, value1.CompareTo(value2)); + Console.WriteLine($"Comparing {value1} and {value2}: {value1.CompareTo(value2)}\n"); + Console.WriteLine($"Comparing {value1:R} and {value2:R}: {value1.CompareTo(value2)}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Double/CompareTo/csharp/compareto3.cs b/docs/fundamentals/runtime-libraries/snippets/System/Double/CompareTo/csharp/compareto3.cs index a7d96b9c614fa..2865d8ce522b2 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Double/CompareTo/csharp/compareto3.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Double/CompareTo/csharp/compareto3.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example3 @@ -7,10 +7,8 @@ public static void Main() { double value1 = 6.185; object value2 = value1 * .1 / .1; - Console.WriteLine("Comparing {0} and {1}: {2}\n", - value1, value2, value1.CompareTo(value2)); - Console.WriteLine("Comparing {0:R} and {1:R}: {2}", - value1, value2, value1.CompareTo(value2)); + Console.WriteLine($"Comparing {value1} and {value2}: {value1.CompareTo(value2)}\n"); + Console.WriteLine($"Comparing {value1:R} and {value2:R}: {value1.CompareTo(value2)}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Double/Epsilon/csharp/epsilon.cs b/docs/fundamentals/runtime-libraries/snippets/System/Double/Epsilon/csharp/epsilon.cs index dbd908c4be847..4cc778bc8545d 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Double/Epsilon/csharp/epsilon.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Double/Epsilon/csharp/epsilon.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example @@ -11,9 +11,7 @@ public static void Main() { for (int ctr2 = ctr + 1; ctr2 <= values.Length - 1; ctr2++) { - Console.WriteLine("{0:r} = {1:r}: {2}", - values[ctr], values[ctr2], - values[ctr].Equals(values[ctr2])); + Console.WriteLine($"{values[ctr]:r} = {values[ctr2]:r}: {values[ctr].Equals(values[ctr2])}"); } Console.WriteLine(); } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/Program.cs b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/Program.cs new file mode 100644 index 0000000000000..92a6d0b62f4f0 --- /dev/null +++ b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/Program.cs @@ -0,0 +1,17 @@ +class Program +{ + static void Main(string[] args) + { + //Example1.Main(); + //Example2.Main(); + //Example3.Main(); + //Example4.Main(); + //Example5.Main(); + //Example6.Main(); + //Example7.Main(); + //Example8.Main(); + //Example9.Main(); + //Example10.Main(); + Example11.Main(); + } +} diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/Project.csproj b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/Project.csproj index 251a8cb33ffed..3e363434650ea 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/Project.csproj +++ b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/Project.csproj @@ -1,8 +1,9 @@ - Library - net8 + Exe + net9.0 + Program diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/comparison1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/comparison1.cs index 446393419b122..b3e50e1a74b65 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/comparison1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/comparison1.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example @@ -7,7 +7,7 @@ public static void Main() { double value1 = .333333333333333; double value2 = 1.0/3; - Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2, value1.Equals(value2)); + Console.WriteLine($"{value1:R} = {value2:R}: {value1.Equals(value2)}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/comparison2.cs b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/comparison2.cs index 6fd92fe3c0ba1..1c2f14e286a09 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/comparison2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/comparison2.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example1 @@ -9,9 +9,8 @@ public static void Main() value1 = Math.Sqrt(Math.Pow(value1, 2)); double value2 = Math.Pow(value1 * 3.51, 2); value2 = Math.Sqrt(value2) / 3.51; - Console.WriteLine("{0} = {1}: {2}\n", - value1, value2, value1.Equals(value2)); - Console.WriteLine("{0:R} = {1:R}", value1, value2); + Console.WriteLine($"{value1} = {value2}: {value1.Equals(value2)}\n"); + Console.WriteLine($"{value1:R} = {value2:R}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/comparison3.cs b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/comparison3.cs index 5af50093085a2..5e18d90ba6034 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/comparison3.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/comparison3.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example2 @@ -10,7 +10,7 @@ public static void Main() int precision = 7; value1 = Math.Round(value1, precision); value2 = Math.Round(value2, precision); - Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2, value1.Equals(value2)); + Console.WriteLine($"{value1:R} = {value2:R}: {value1.Equals(value2)}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/comparison4.cs b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/comparison4.cs index 175e4e6a8062f..a652380e23253 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/comparison4.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/comparison4.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example3 @@ -10,10 +10,8 @@ public static void Main() for (int ctr = 1; ctr <= 10; ctr++) one2 += .1; - Console.WriteLine("{0:R} = {1:R}: {2}", one1, one2, one1.Equals(one2)); - Console.WriteLine("{0:R} is approximately equal to {1:R}: {2}", - one1, one2, - IsApproximatelyEqual(one1, one2, .000000001)); + Console.WriteLine($"{one1:R} = {one2:R}: {one1.Equals(one2)}"); + Console.WriteLine($"{one1:R} is approximately equal to {one2:R}: {IsApproximatelyEqual(one1, one2, .000000001)}"); } static bool IsApproximatelyEqual(double value1, double value2, double epsilon) diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/convert1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/convert1.cs index 537fa65d8166e..dee3e3e473f07 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/convert1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/convert1.cs @@ -1,10 +1,10 @@ -// -using System; +using System; public class Example4 { public static void Main() { + // dynamic[] values = { Byte.MinValue, Byte.MaxValue, Decimal.MinValue, Decimal.MaxValue, Int16.MinValue, Int16.MaxValue, Int32.MinValue, Int32.MaxValue, Int64.MinValue, @@ -13,37 +13,37 @@ public static void Main() UInt16.MaxValue, UInt32.MinValue, UInt32.MaxValue, UInt64.MinValue, UInt64.MaxValue }; double dblValue; - foreach (var value in values) + foreach (dynamic value in values) { - if (value.GetType() == typeof(Decimal)) - dblValue = (Double)value; + if (value.GetType() == typeof(decimal)) + dblValue = (double)value; else dblValue = value; - Console.WriteLine("{0} ({1}) --> {2:R} ({3})", - value, value.GetType().Name, - dblValue, dblValue.GetType().Name); + Console.WriteLine($"{value} ({value.GetType().Name}) --> " + + $"{dblValue:R} ({dblValue.GetType().Name})"); } + + // The example displays the following output: + // 0 (Byte) --> 0 (Double) + // 255 (Byte) --> 255 (Double) + // -79228162514264337593543950335 (Decimal) --> -7.9228162514264338E+28 (Double) + // 79228162514264337593543950335 (Decimal) --> 7.9228162514264338E+28 (Double) + // -32768 (Int16) --> -32768 (Double) + // 32767 (Int16) --> 32767 (Double) + // -2147483648 (Int32) --> -2147483648 (Double) + // 2147483647 (Int32) --> 2147483647 (Double) + // -9223372036854775808 (Int64) --> -9.2233720368547758E+18 (Double) + // 9223372036854775807 (Int64) --> 9.2233720368547758E+18 (Double) + // -128 (SByte) --> -128 (Double) + // 127 (SByte) --> 127 (Double) + // -3.402823E+38 (Single) --> -3.4028234663852886E+38 (Double) + // 3.402823E+38 (Single) --> 3.4028234663852886E+38 (Double) + // 0 (UInt16) --> 0 (Double) + // 65535 (UInt16) --> 65535 (Double) + // 0 (UInt32) --> 0 (Double) + // 4294967295 (UInt32) --> 4294967295 (Double) + // 0 (UInt64) --> 0 (Double) + // 18446744073709551615 (UInt64) --> 1.8446744073709552E+19 (Double) + // } } -// The example displays the following output: -// 0 (Byte) --> 0 (Double) -// 255 (Byte) --> 255 (Double) -// -79228162514264337593543950335 (Decimal) --> -7.9228162514264338E+28 (Double) -// 79228162514264337593543950335 (Decimal) --> 7.9228162514264338E+28 (Double) -// -32768 (Int16) --> -32768 (Double) -// 32767 (Int16) --> 32767 (Double) -// -2147483648 (Int32) --> -2147483648 (Double) -// 2147483647 (Int32) --> 2147483647 (Double) -// -9223372036854775808 (Int64) --> -9.2233720368547758E+18 (Double) -// 9223372036854775807 (Int64) --> 9.2233720368547758E+18 (Double) -// -128 (SByte) --> -128 (Double) -// 127 (SByte) --> 127 (Double) -// -3.402823E+38 (Single) --> -3.4028234663852886E+38 (Double) -// 3.402823E+38 (Single) --> 3.4028234663852886E+38 (Double) -// 0 (UInt16) --> 0 (Double) -// 65535 (UInt16) --> 65535 (Double) -// 0 (UInt32) --> 0 (Double) -// 4294967295 (UInt32) --> 4294967295 (Double) -// 0 (UInt64) --> 0 (Double) -// 18446744073709551615 (UInt64) --> 1.8446744073709552E+19 (Double) -// diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/convert2.cs b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/convert2.cs index 46d66e56f290d..efd4c149833ee 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/convert2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/convert2.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example5 @@ -16,46 +16,38 @@ public static void Main() try { Int64 lValue = (long)value; - Console.WriteLine("{0} ({1}) --> {2} (0x{2:X16}) ({3})", - value, value.GetType().Name, - lValue, lValue.GetType().Name); + Console.WriteLine($"{value} ({value.GetType().Name}) --> {lValue} (0x{lValue:X16}) ({lValue.GetType().Name})"); } catch (OverflowException) { - Console.WriteLine("Unable to convert {0} to Int64.", value); + Console.WriteLine($"Unable to convert {value} to Int64."); } try { UInt64 ulValue = (ulong)value; - Console.WriteLine("{0} ({1}) --> {2} (0x{2:X16}) ({3})", - value, value.GetType().Name, - ulValue, ulValue.GetType().Name); + Console.WriteLine($"{value} ({value.GetType().Name}) --> {ulValue} (0x{ulValue:X16}) ({ulValue.GetType().Name})"); } catch (OverflowException) { - Console.WriteLine("Unable to convert {0} to UInt64.", value); + Console.WriteLine($"Unable to convert {value} to UInt64."); } try { Decimal dValue = (decimal)value; - Console.WriteLine("{0} ({1}) --> {2} ({3})", - value, value.GetType().Name, - dValue, dValue.GetType().Name); + Console.WriteLine($"{value} ({value.GetType().Name}) --> {dValue} ({dValue.GetType().Name})"); } catch (OverflowException) { - Console.WriteLine("Unable to convert {0} to Decimal.", value); + Console.WriteLine($"Unable to convert {value} to Decimal."); } try { Single sValue = (float)value; - Console.WriteLine("{0} ({1}) --> {2} ({3})", - value, value.GetType().Name, - sValue, sValue.GetType().Name); + Console.WriteLine($"{value} ({value.GetType().Name}) --> {sValue} ({sValue.GetType().Name})"); } catch (OverflowException) { - Console.WriteLine("Unable to convert {0} to Single.", value); + Console.WriteLine($"Unable to convert {value} to Single."); } Console.WriteLine(); } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/exceptional1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/exceptional1.cs index 1985c123a3807..73330fc6ca42a 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/exceptional1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/exceptional1.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example6 @@ -8,8 +8,8 @@ public static void Main() Double value1 = 1.1632875981534209e-225; Double value2 = 9.1642346778e-175; Double result = value1 * value2; - Console.WriteLine("{0} * {1} = {2}", value1, value2, result); - Console.WriteLine("{0} = 0: {1}", result, result.Equals(0.0)); + Console.WriteLine($"{value1} * {value2} = {result}"); + Console.WriteLine($"{result} = 0: {result.Equals(0.0)}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/exceptional2.cs b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/exceptional2.cs index e6e7493bd0b40..b4358bdc1ea2f 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/exceptional2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/exceptional2.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example7 @@ -8,17 +8,13 @@ public static void Main() Double value1 = 4.565e153; Double value2 = 6.9375e172; Double result = value1 * value2; - Console.WriteLine("PositiveInfinity: {0}", - Double.IsPositiveInfinity(result)); - Console.WriteLine("NegativeInfinity: {0}\n", - Double.IsNegativeInfinity(result)); + Console.WriteLine($"PositiveInfinity: {Double.IsPositiveInfinity(result)}"); + Console.WriteLine($"NegativeInfinity: {Double.IsNegativeInfinity(result)}\n"); value1 = -value1; result = value1 * value2; - Console.WriteLine("PositiveInfinity: {0}", - Double.IsPositiveInfinity(result)); - Console.WriteLine("NegativeInfinity: {0}", - Double.IsNegativeInfinity(result)); + Console.WriteLine($"PositiveInfinity: {Double.IsPositiveInfinity(result)}"); + Console.WriteLine($"NegativeInfinity: {Double.IsNegativeInfinity(result)}"); } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/precisionlist1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/precisionlist1.cs index 83e6b7d4a1983..f76727e6cb899 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/precisionlist1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/precisionlist1.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example9 @@ -8,8 +8,7 @@ public static void Main() Double value1 = 1 / 3.0; Single sValue2 = 1 / 3.0f; Double value2 = (Double)sValue2; - Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2, - value1.Equals(value2)); + Console.WriteLine($"{value1:R} = {value2:R}: {value1.Equals(value2)}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/precisionlist3.cs b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/precisionlist3.cs index 5ba1e16df2dde..11e92c872245c 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/precisionlist3.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/precisionlist3.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example10 @@ -14,8 +14,7 @@ public static void Main() if (total.Equals(result)) Console.WriteLine("The sum of the values equals the total."); else - Console.WriteLine("The sum of the values ({0}) does not equal the total ({1}).", - total, result); + Console.WriteLine($"The sum of the values ({total}) does not equal the total ({result})."); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/precisionlist4.cs b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/precisionlist4.cs index 500213f945757..b049845d3115f 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/precisionlist4.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/precisionlist4.cs @@ -1,13 +1,13 @@ -// -using System; +using System; using System.IO; public class Example11 { public static void Main() { - StreamWriter sw = new StreamWriter(@".\Doubles.dat"); - Double[] values = { 2.2 / 1.01, 1.0 / 3, Math.PI }; + // + StreamWriter sw = new(@".\Doubles.dat"); + double[] values = [2.2 / 1.01, 1.0 / 3, Math.PI]; for (int ctr = 0; ctr < values.Length; ctr++) { sw.Write(values[ctr].ToString()); @@ -16,21 +16,20 @@ public static void Main() } sw.Close(); - Double[] restoredValues = new Double[values.Length]; - StreamReader sr = new StreamReader(@".\Doubles.dat"); + double[] restoredValues = new double[values.Length]; + StreamReader sr = new(@".\Doubles.dat"); string temp = sr.ReadToEnd(); string[] tempStrings = temp.Split('|'); for (int ctr = 0; ctr < tempStrings.Length; ctr++) - restoredValues[ctr] = Double.Parse(tempStrings[ctr]); + restoredValues[ctr] = double.Parse(tempStrings[ctr]); for (int ctr = 0; ctr < values.Length; ctr++) - Console.WriteLine("{0} {2} {1}", values[ctr], - restoredValues[ctr], - values[ctr].Equals(restoredValues[ctr]) ? "=" : "<>"); + Console.WriteLine($"{values[ctr]} {(values[ctr].Equals(restoredValues[ctr]) ? "=" : "<>")} {restoredValues[ctr]}"); + + // For .NET Framework only, the example displays the following output: + // 2.17821782178218 <> 2.17821782178218 + // 0.333333333333333 <> 0.333333333333333 + // 3.14159265358979 <> 3.14159265358979 + // } } -// The example displays the following output: -// 2.17821782178218 <> 2.17821782178218 -// 0.333333333333333 <> 0.333333333333333 -// 3.14159265358979 <> 3.14159265358979 -// diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/precisionlist5.cs b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/precisionlist5.cs deleted file mode 100644 index 17a805b4f553c..0000000000000 --- a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/precisionlist5.cs +++ /dev/null @@ -1,33 +0,0 @@ -// -using System; -using System.IO; - -public class Example12 -{ - public static void Main() - { - StreamWriter sw = new StreamWriter(@".\Doubles.dat"); - Double[] values = { 2.2 / 1.01, 1.0 / 3, Math.PI }; - for (int ctr = 0; ctr < values.Length; ctr++) - sw.Write("{0:G17}{1}", values[ctr], ctr < values.Length - 1 ? "|" : ""); - - sw.Close(); - - Double[] restoredValues = new Double[values.Length]; - StreamReader sr = new StreamReader(@".\Doubles.dat"); - string temp = sr.ReadToEnd(); - string[] tempStrings = temp.Split('|'); - for (int ctr = 0; ctr < tempStrings.Length; ctr++) - restoredValues[ctr] = Double.Parse(tempStrings[ctr]); - - for (int ctr = 0; ctr < values.Length; ctr++) - Console.WriteLine("{0} {2} {1}", values[ctr], - restoredValues[ctr], - values[ctr].Equals(restoredValues[ctr]) ? "=" : "<>"); - } -} -// The example displays the following output: -// 2.17821782178218 = 2.17821782178218 -// 0.333333333333333 = 0.333333333333333 -// 3.14159265358979 = 3.14159265358979 -// diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/representation1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/representation1.cs index 6a6feccc6c73c..52b1d272ab169 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/representation1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/representation1.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example13 @@ -11,8 +11,8 @@ public static void Main() for (int ctr = 1; ctr <= 10; ctr++) result2 += value; - Console.WriteLine(".1 * 10: {0:R}", result1); - Console.WriteLine(".1 Added 10 times: {0:R}", result2); + Console.WriteLine($".1 * 10: {result1:R}"); + Console.WriteLine($".1 Added 10 times: {result2:R}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/representation2.cs b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/representation2.cs index ecd9fd0cb3dbd..d4ab7b394b68b 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/representation2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/representation2.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example14 @@ -7,8 +7,7 @@ public static void Main() { Double value = 123456789012.34567; Double additional = Double.Epsilon * 1e15; - Console.WriteLine("{0} + {1} = {2}", value, additional, - value + additional); + Console.WriteLine($"{value} + {additional} = {value + additional}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/EnumMain.cs b/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/EnumMain.cs index 2dcab459ead92..11ea8acf442a2 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/EnumMain.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/EnumMain.cs @@ -1,4 +1,4 @@ -// +// using System; public class EnumTest { @@ -26,7 +26,7 @@ public static void Main() { Colors myColors = Colors.Red | Colors.Blue | Colors.Yellow; Console.WriteLine(); - Console.WriteLine("myColors holds a combination of colors. Namely: {0}", myColors); + Console.WriteLine($"myColors holds a combination of colors. Namely: {myColors}"); } } // diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/Extensions.cs b/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/Extensions.cs index 74bb407228211..39b41c6f3caed 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/Extensions.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/Extensions.cs @@ -21,13 +21,13 @@ static void Main() { Grades g1 = Grades.D; Grades g2 = Grades.F; - Console.WriteLine("{0} {1} a passing grade.", g1, g1.Passing() ? "is" : "is not"); - Console.WriteLine("{0} {1} a passing grade.", g2, g2.Passing() ? "is" : "is not"); + Console.WriteLine($"{g1} {(g1.Passing() ? "is" : "is not")} a passing grade."); + Console.WriteLine($"{g2} {(g2.Passing() ? "is" : "is not")} a passing grade."); Extensions.minPassing = Grades.C; Console.WriteLine("\nRaising the bar!\n"); - Console.WriteLine("{0} {1} a passing grade.", g1, g1.Passing() ? "is" : "is not"); - Console.WriteLine("{0} {1} a passing grade.", g2, g2.Passing() ? "is" : "is not"); + Console.WriteLine($"{g1} {(g1.Passing() ? "is" : "is not")} a passing grade."); + Console.WriteLine($"{g2} {(g2.Passing() ? "is" : "is not")} a passing grade."); } } // The exmaple displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/class1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/class1.cs index 41dfecf96ebb0..d0dc14f938725 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/class1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/class1.cs @@ -1,4 +1,4 @@ -using System; +using System; // public enum ArrivalStatus { Unknown=-3, Late=-1, OnTime=0, Early=1 }; @@ -10,7 +10,7 @@ public class Example public static void Main() { ArrivalStatus status = ArrivalStatus.OnTime; - Console.WriteLine("Arrival Status: {0} ({0:D})", status); + Console.WriteLine($"Arrival Status: {status} ({status:D})"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/class2.cs b/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/class2.cs index 13ec47a420f35..0e3d0d01c4c0c 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/class2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/class2.cs @@ -1,4 +1,4 @@ -using System; +using System; public class Example1 { @@ -6,14 +6,14 @@ public static void Main() { // ArrivalStatus status1 = new ArrivalStatus(); - Console.WriteLine("Arrival Status: {0} ({0:D})", status1); + Console.WriteLine($"Arrival Status: {status1} ({status1:D})"); // The example displays the following output: // Arrival Status: OnTime (0) // // ArrivalStatus status2 = (ArrivalStatus)1; - Console.WriteLine("Arrival Status: {0} ({0:D})", status2); + Console.WriteLine($"Arrival Status: {status2} ({status2:D})"); // The example displays the following output: // Arrival Status: Early (1) // diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/classbitwise1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/classbitwise1.cs index e8ace8849c060..ed2365a2484d3 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/classbitwise1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/classbitwise1.cs @@ -1,4 +1,4 @@ -using System; +using System; // [Flags] @@ -16,7 +16,7 @@ public static void Main() { // Pets familyPets = Pets.Dog | Pets.Cat; - Console.WriteLine("Pets: {0:G} ({0:D})", familyPets); + Console.WriteLine($"Pets: {familyPets:G} ({familyPets:D})"); // The example displays the following output: // Pets: Dog, Cat (3) // diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/classconversion1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/classconversion1.cs index 92295906813f9..0165fd21373cb 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/classconversion1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/classconversion1.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example3 @@ -13,7 +13,7 @@ public static void Main() status = (ArrivalStatus)value; else status = ArrivalStatus.Unknown; - Console.WriteLine("Converted {0:N0} to {1}", value, status); + Console.WriteLine($"Converted {value:N0} to {status}"); } } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/classconversion2.cs b/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/classconversion2.cs index 3f37a286f7b09..0ecfe35cc3647 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/classconversion2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/classconversion2.cs @@ -1,4 +1,4 @@ -using System; +using System; public class Example4 { @@ -7,7 +7,7 @@ public static void Main() // ArrivalStatus status = ArrivalStatus.Early; var number = Convert.ChangeType(status, Enum.GetUnderlyingType(typeof(ArrivalStatus))); - Console.WriteLine("Converted {0} to {1}", status, number); + Console.WriteLine($"Converted {status} to {number}"); // The example displays the following output: // Converted Early to 1 // diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/classiterate.cs b/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/classiterate.cs index 9be33c280ae91..f9c3667b299b6 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/classiterate.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/classiterate.cs @@ -1,4 +1,4 @@ -using System; +using System; public class Example6 { @@ -13,12 +13,12 @@ private static void GetEnumByName() { // string[] names = Enum.GetNames(typeof(ArrivalStatus)); - Console.WriteLine("Members of {0}:", typeof(ArrivalStatus).Name); + Console.WriteLine($"Members of {typeof(ArrivalStatus).Name}:"); Array.Sort(names); foreach (var name in names) { ArrivalStatus status = (ArrivalStatus)Enum.Parse(typeof(ArrivalStatus), name); - Console.WriteLine(" {0} ({0:D})", status); + Console.WriteLine($" {status} ({status:D})"); } // The example displays the following output: // Members of ArrivalStatus: @@ -33,10 +33,10 @@ private static void GetEnumByValue() { // var values = Enum.GetValues(typeof(ArrivalStatus)); - Console.WriteLine("Members of {0}:", typeof(ArrivalStatus).Name); + Console.WriteLine($"Members of {typeof(ArrivalStatus).Name}:"); foreach (ArrivalStatus status in values) { - Console.WriteLine(" {0} ({0:D})", status); + Console.WriteLine($" {status} ({status:D})"); } // The example displays the following output: // Members of ArrivalStatus: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/classparse1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/classparse1.cs index 1168868357ad4..4f7f447f7475b 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/classparse1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Enum/Overview/csharp/classparse1.cs @@ -1,4 +1,4 @@ -using System; +using System; public class Example7 { @@ -13,12 +13,11 @@ public static void Main() ArrivalStatus status1 = (ArrivalStatus)Enum.Parse(typeof(ArrivalStatus), number); if (!(Enum.IsDefined(typeof(ArrivalStatus), status1))) status1 = ArrivalStatus.Unknown; - Console.WriteLine("Converted '{0}' to {1}", number, status1); + Console.WriteLine($"Converted '{number}' to {status1}"); } catch (FormatException) { - Console.WriteLine("Unable to convert '{0}' to an ArrivalStatus value.", - number); + Console.WriteLine($"Unable to convert '{number}' to an ArrivalStatus value."); } ArrivalStatus status2; @@ -26,12 +25,11 @@ public static void Main() { if (!(Enum.IsDefined(typeof(ArrivalStatus), status2))) status2 = ArrivalStatus.Unknown; - Console.WriteLine("Converted '{0}' to {1}", name, status2); + Console.WriteLine($"Converted '{name}' to {status2}"); } else { - Console.WriteLine("Unable to convert '{0}' to an ArrivalStatus value.", - number); + Console.WriteLine($"Unable to convert '{number}' to an ArrivalStatus value."); } // The example displays the following output: // Converted '-1' to Late diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Exception/HelpLink/csharp/properties.cs b/docs/fundamentals/runtime-libraries/snippets/System/Exception/HelpLink/csharp/properties.cs index 6a01d701a3c36..4bd24e14b1517 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Exception/HelpLink/csharp/properties.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Exception/HelpLink/csharp/properties.cs @@ -1,4 +1,4 @@ -// +// using System; namespace NDP_UE_CS @@ -72,14 +72,11 @@ public static void Main() } catch (Exception ex) { - Console.WriteLine("\nMessage ---\n{0}", ex.Message); - Console.WriteLine( - "\nHelpLink ---\n{0}", ex.HelpLink); - Console.WriteLine("\nSource ---\n{0}", ex.Source); - Console.WriteLine( - "\nStackTrace ---\n{0}", ex.StackTrace); - Console.WriteLine( - "\nTargetSite ---\n{0}", ex.TargetSite); + Console.WriteLine($"\nMessage ---\n{ex.Message}"); + Console.WriteLine($"\nHelpLink ---\n{ex.HelpLink}"); + Console.WriteLine($"\nSource ---\n{ex.Source}"); + Console.WriteLine($"\nStackTrace ---\n{ex.StackTrace}"); + Console.WriteLine($"\nTargetSite ---\n{ex.TargetSite}"); } } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Exception/Overview/csharp/example.cs b/docs/fundamentals/runtime-libraries/snippets/System/Exception/Overview/csharp/example.cs index 2de125836a166..14e295dd3f0c4 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Exception/Overview/csharp/example.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Exception/Overview/csharp/example.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Reflection; @@ -12,12 +12,11 @@ public static void Main() try { int[] values = primes.GetPrimesFrom(start); - Console.WriteLine("There are {0} prime numbers from {1} to {2}", - start, limit); + Console.WriteLine($"There are {start} prime numbers from {limit} to {2}"); } catch (NotPrimeException e) { - Console.WriteLine("{0} is not prime", e.NonPrime); + Console.WriteLine($"{e.NonPrime} is not prime"); Console.WriteLine(e); Console.WriteLine("--------"); } @@ -35,7 +34,7 @@ public static void Main() } catch (NotPrimeException e) { - Console.WriteLine("{0} is not prime", e.NonPrime); + Console.WriteLine($"{e.NonPrime} is not prime"); Console.WriteLine(e); Console.WriteLine("--------"); } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Exception/Overview/csharp/rethrow1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Exception/Overview/csharp/rethrow1.cs index 7ff50fe52d0f9..b3f6cab258f9c 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Exception/Overview/csharp/rethrow1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Exception/Overview/csharp/rethrow1.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Collections.Generic; @@ -49,10 +49,9 @@ public static void Main() } catch (ArgumentNullException e) { - Console.WriteLine("An exception ({0}) occurred.", - e.GetType().Name); - Console.WriteLine("Message:\n {0}\n", e.Message); - Console.WriteLine("Stack Trace:\n {0}\n", e.StackTrace); + Console.WriteLine($"An exception ({e.GetType().Name}) occurred."); + Console.WriteLine($"Message:\n {e.Message}\n"); + Console.WriteLine($"Stack Trace:\n {e.StackTrace}\n"); } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Exception/Overview/csharp/rethrow3.cs b/docs/fundamentals/runtime-libraries/snippets/System/Exception/Overview/csharp/rethrow3.cs index 25328ecc44b1c..7fcc171e46ebe 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Exception/Overview/csharp/rethrow3.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Exception/Overview/csharp/rethrow3.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; static class Library @@ -48,17 +48,16 @@ public static void Main() } catch (ArgumentNullException e) { - Console.WriteLine("An exception ({0}) occurred.", - e.GetType().Name); - Console.WriteLine(" Message:\n{0}", e.Message); - Console.WriteLine(" Stack Trace:\n {0}", e.StackTrace); + Console.WriteLine($"An exception ({e.GetType().Name}) occurred."); + Console.WriteLine($" Message:\n{e.Message}"); + Console.WriteLine($" Stack Trace:\n {e.StackTrace}"); Exception ie = e.InnerException; if (ie != null) { Console.WriteLine(" The Inner Exception:"); - Console.WriteLine(" Exception Name: {0}", ie.GetType().Name); - Console.WriteLine(" Message: {0}\n", ie.Message); - Console.WriteLine(" Stack Trace:\n {0}\n", ie.StackTrace); + Console.WriteLine($" Exception Name: {ie.GetType().Name}"); + Console.WriteLine($" Message: {ie.Message}\n"); + Console.WriteLine($" Stack Trace:\n {ie.StackTrace}\n"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Exception/Overview/csharp/usageerrors1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Exception/Overview/csharp/usageerrors1.cs index ee021306b12a3..6bc88ff16c56c 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Exception/Overview/csharp/usageerrors1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Exception/Overview/csharp/usageerrors1.cs @@ -1,4 +1,4 @@ -// +// using System; public class Person1 @@ -34,7 +34,7 @@ public static void Main() Person1 p2 = null; // The following throws a NullReferenceException. - Console.WriteLine("p1 = p2: {0}", p1.Equals(p2)); + Console.WriteLine($"p1 = p2: {p1.Equals(p2)}"); } } // diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Exception/Overview/csharp/usageerrors2.cs b/docs/fundamentals/runtime-libraries/snippets/System/Exception/Overview/csharp/usageerrors2.cs index 3d388aabe0f6e..61181b1651de4 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Exception/Overview/csharp/usageerrors2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Exception/Overview/csharp/usageerrors2.cs @@ -1,4 +1,4 @@ -// +// using System; public class Person2 @@ -35,7 +35,7 @@ public static void Main() p1.Name = "John"; Person2 p2 = null; - Console.WriteLine("p1 = p2: {0}", p1.Equals(p2)); + Console.WriteLine($"p1 = p2: {p1.Equals(p2)}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/FlagsAttribute/Overview/csharp/Program.cs b/docs/fundamentals/runtime-libraries/snippets/System/FlagsAttribute/Overview/csharp/Program.cs new file mode 100644 index 0000000000000..d68b2c57e4fc9 --- /dev/null +++ b/docs/fundamentals/runtime-libraries/snippets/System/FlagsAttribute/Overview/csharp/Program.cs @@ -0,0 +1 @@ +Example1.Main(); diff --git a/docs/fundamentals/runtime-libraries/snippets/System/FlagsAttribute/Overview/csharp/Project.csproj b/docs/fundamentals/runtime-libraries/snippets/System/FlagsAttribute/Overview/csharp/Project.csproj index 251a8cb33ffed..f2ef551a953f3 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/FlagsAttribute/Overview/csharp/Project.csproj +++ b/docs/fundamentals/runtime-libraries/snippets/System/FlagsAttribute/Overview/csharp/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net8 diff --git a/docs/fundamentals/runtime-libraries/snippets/System/FlagsAttribute/Overview/csharp/flags1.cs b/docs/fundamentals/runtime-libraries/snippets/System/FlagsAttribute/Overview/csharp/flags1.cs index d52c8dfe77db2..987dbe00bb97a 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/FlagsAttribute/Overview/csharp/flags1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/FlagsAttribute/Overview/csharp/flags1.cs @@ -28,33 +28,26 @@ public static void Main() // Which households have no service? for (int ctr = 0; ctr < households.Length; ctr++) - Console.WriteLine("Household {0} has phone service: {1}", - ctr + 1, - households[ctr] == PhoneService.None ? - "No" : "Yes"); + Console.WriteLine($"Household {ctr + 1} has phone service:" + + $"{(households[ctr] == PhoneService.None ? "No" : "Yes")}"); Console.WriteLine(); // Which households have cell phone service? for (int ctr = 0; ctr < households.Length; ctr++) - Console.WriteLine("Household {0} has cell phone service: {1}", - ctr + 1, - (households[ctr] & PhoneService.Cell) == PhoneService.Cell ? - "Yes" : "No"); + Console.WriteLine($"Household {ctr + 1} has cell phone service: " + + $"{((households[ctr] & PhoneService.Cell) == PhoneService.Cell ? "Yes" : "No")}"); Console.WriteLine(); // Which households have cell phones and land lines? var cellAndLand = PhoneService.Cell | PhoneService.LandLine; for (int ctr = 0; ctr < households.Length; ctr++) - Console.WriteLine("Household {0} has cell and land line service: {1}", - ctr + 1, - (households[ctr] & cellAndLand) == cellAndLand ? - "Yes" : "No"); + Console.WriteLine($"Household {ctr + 1} has cell and land line service: " + + $"{((households[ctr] & cellAndLand) == cellAndLand ? "Yes" : "No")}"); Console.WriteLine(); // List all types of service of each household?// for (int ctr = 0; ctr < households.Length; ctr++) - Console.WriteLine("Household {0} has: {1:G}", - ctr + 1, households[ctr]); + Console.WriteLine($"Household {ctr + 1} has: {households[ctr]:G}"); Console.WriteLine(); } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/FormatException/Overview/csharp/qa11.cs b/docs/fundamentals/runtime-libraries/snippets/System/FormatException/Overview/csharp/qa11.cs index c4d157aca8494..00e98be5982da 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/FormatException/Overview/csharp/qa11.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/FormatException/Overview/csharp/qa11.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; public class Example5 @@ -16,7 +16,7 @@ public static void Main() total += number; } numbers[3] = total; - Console.WriteLine("{0} + {1} + {2} = {3}", numbers); + Console.WriteLine($"{numbers} + {1} + {2} = {3}"); // } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/FormatException/Overview/csharp/qa21.cs b/docs/fundamentals/runtime-libraries/snippets/System/FormatException/Overview/csharp/qa21.cs index cfcad59e747a0..067bd5fdd036b 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/FormatException/Overview/csharp/qa21.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/FormatException/Overview/csharp/qa21.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; public class Example6 @@ -18,7 +18,7 @@ public static void Main() numbers[3] = total; object[] values = new object[numbers.Length]; numbers.CopyTo(values, 0); - Console.WriteLine("{0} + {1} + {2} = {3}", values); + Console.WriteLine($"{values} + {1} + {2} = {3}"); // } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/IDisposable/Overview/csharp/calling1.cs b/docs/fundamentals/runtime-libraries/snippets/System/IDisposable/Overview/csharp/calling1.cs index 2d92c94014aad..6d6e6fdeab75d 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/IDisposable/Overview/csharp/calling1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/IDisposable/Overview/csharp/calling1.cs @@ -1,4 +1,4 @@ -// +// using System; using System.IO; using System.Text.RegularExpressions; @@ -39,7 +39,6 @@ public class Example8 public static void Main() { WordCount wc = new WordCount(@"C:\users\ronpet\documents\Fr_Mike_Mass.txt"); - Console.WriteLine("File {0} ({1}) has {2} words", - wc.Name, wc.FullName, wc.Count); + Console.WriteLine($"File {wc.Name} ({wc.FullName}) has {wc.Count} words"); } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/IDisposable/Overview/csharp/calling2.cs b/docs/fundamentals/runtime-libraries/snippets/System/IDisposable/Overview/csharp/calling2.cs index f8125236c6b56..92c094d6733df 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/IDisposable/Overview/csharp/calling2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/IDisposable/Overview/csharp/calling2.cs @@ -1,4 +1,4 @@ -// +// using System; using System.IO; using System.Text.RegularExpressions; @@ -45,7 +45,6 @@ public class Example public static void Main() { WordCount2 wc = new WordCount2(@"C:\users\ronpet\documents\Fr_Mike_Mass.txt"); - Console.WriteLine("File {0} ({1}) has {2} words", - wc.Name, wc.FullName, wc.Count); + Console.WriteLine($"File {wc.Name} ({wc.FullName}) has {wc.Count} words"); } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Int32/Overview/csharp/Instantiate1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Int32/Overview/csharp/Instantiate1.cs index ea5f8c1b84b2f..84958e6bcd767 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Int32/Overview/csharp/Instantiate1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Int32/Overview/csharp/Instantiate1.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Numerics; public class Example @@ -20,7 +20,7 @@ private static void InstantiateByAssignment() int number1 = 64301; int number2 = 25548612; // - Console.WriteLine("{0} - {1}", number1, number2); + Console.WriteLine($"{number1} - {number2}"); } private static void InstantiateByNarrowingConversion() @@ -32,7 +32,7 @@ private static void InstantiateByNarrowingConversion() Console.WriteLine(number1); } catch (OverflowException) { - Console.WriteLine("{0} is out of range of an Int32.", lNumber); + Console.WriteLine($"{lNumber} is out of range of an Int32."); } double dbl2 = 35901.997; @@ -41,7 +41,7 @@ private static void InstantiateByNarrowingConversion() Console.WriteLine(number2); } catch (OverflowException) { - Console.WriteLine("{0} is out of range of an Int32.", dbl2); + Console.WriteLine($"{dbl2} is out of range of an Int32."); } BigInteger bigNumber = 132451; @@ -50,7 +50,7 @@ private static void InstantiateByNarrowingConversion() Console.WriteLine(number3); } catch (OverflowException) { - Console.WriteLine("{0} is out of range of an Int32.", bigNumber); + Console.WriteLine($"{bigNumber} is out of range of an Int32."); } // The example displays the following output: // 163245617 @@ -68,10 +68,10 @@ private static void Parse() Console.WriteLine(number1); } catch (OverflowException) { - Console.WriteLine("'{0}' is out of range of a 32-bit integer.", string1); + Console.WriteLine($"'{string1}' is out of range of a 32-bit integer."); } catch (FormatException) { - Console.WriteLine("The format of '{0}' is invalid.", string1); + Console.WriteLine($"The format of '{string1}' is invalid."); } string string2 = "F9A3C"; @@ -81,10 +81,10 @@ private static void Parse() Console.WriteLine(number2); } catch (OverflowException) { - Console.WriteLine("'{0}' is out of range of a 32-bit integer.", string2); + Console.WriteLine($"'{string2}' is out of range of a 32-bit integer."); } catch (FormatException) { - Console.WriteLine("The format of '{0}' is invalid.", string2); + Console.WriteLine($"The format of '{string2}' is invalid."); } // The example displays the following output: // 244681 diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Int64/Overview/csharp/formatting1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Int64/Overview/csharp/formatting1.cs index 73fa7b3615377..e5944afa58964 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Int64/Overview/csharp/formatting1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Int64/Overview/csharp/formatting1.cs @@ -1,4 +1,4 @@ -using System; +using System; public class Example1 { @@ -40,10 +40,10 @@ private static void CallConvert() long[] numbers = { -146, 11043, 2781913 }; foreach (var number in numbers) { - Console.WriteLine("{0} (Base 10):", number); - Console.WriteLine(" Binary: {0}", Convert.ToString(number, 2)); - Console.WriteLine(" Octal: {0}", Convert.ToString(number, 8)); - Console.WriteLine(" Hex: {0}\n", Convert.ToString(number, 16)); + Console.WriteLine($"{number} (Base 10):"); + Console.WriteLine($" Binary: {Convert.ToString(number, 2)}"); + Console.WriteLine($" Octal: {Convert.ToString(number, 8)}"); + Console.WriteLine($" Hex: {Convert.ToString(number, 16)}\n"); } // The example displays the following output: // -146 (Base 10): diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Int64/Overview/csharp/instantiate1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Int64/Overview/csharp/instantiate1.cs index 6f98b51515605..ffe7cc22985c1 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Int64/Overview/csharp/instantiate1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Int64/Overview/csharp/instantiate1.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Numerics; public class Example @@ -20,7 +20,7 @@ private static void InstantiateByAssignment() long number1 = -64301728; long number2 = 255486129307; // - Console.WriteLine("{0} - {1}", number1, number2); + Console.WriteLine($"{number1} - {number2}"); } private static void InstantiateByNarrowingConversion() @@ -32,7 +32,7 @@ private static void InstantiateByNarrowingConversion() Console.WriteLine(number1); } catch (OverflowException) { - Console.WriteLine("{0} is out of range of an Int64.", ulNumber); + Console.WriteLine($"{ulNumber} is out of range of an Int64."); } double dbl2 = 35901.997; @@ -41,7 +41,7 @@ private static void InstantiateByNarrowingConversion() Console.WriteLine(number2); } catch (OverflowException) { - Console.WriteLine("{0} is out of range of an Int64.", dbl2); + Console.WriteLine($"{dbl2} is out of range of an Int64."); } BigInteger bigNumber = (BigInteger) 1.63201978555e30; @@ -50,7 +50,7 @@ private static void InstantiateByNarrowingConversion() Console.WriteLine(number3); } catch (OverflowException) { - Console.WriteLine("{0} is out of range of an Int64.", bigNumber); + Console.WriteLine($"{bigNumber} is out of range of an Int64."); } // The example displays the following output: // 163245617943825 @@ -68,10 +68,10 @@ private static void Parse() Console.WriteLine(number1); } catch (OverflowException) { - Console.WriteLine("'{0}' is out of range of a 64-bit integer.", string1); + Console.WriteLine($"'{string1}' is out of range of a 64-bit integer."); } catch (FormatException) { - Console.WriteLine("The format of '{0}' is invalid.", string1); + Console.WriteLine($"The format of '{string1}' is invalid."); } string string2 = "F9A3CFF0A"; @@ -81,10 +81,10 @@ private static void Parse() Console.WriteLine(number2); } catch (OverflowException) { - Console.WriteLine("'{0}' is out of range of a 64-bit integer.", string2); + Console.WriteLine($"'{string2}' is out of range of a 64-bit integer."); } catch (FormatException) { - Console.WriteLine("The format of '{0}' is invalid.", string2); + Console.WriteLine($"The format of '{string2}' is invalid."); } // The example displays the following output: // 244681903147 diff --git a/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Enumerable2.cs b/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Enumerable2.cs index 9154de15f3dc0..e5b8dc2651eec 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Enumerable2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Enumerable2.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Linq; @@ -10,8 +10,7 @@ public static void Main() var moreThan4 = dbQueryResults.Where(num => num > 4); if (moreThan4.Any()) - Console.WriteLine("Average value of numbers greater than 4: {0}:", - moreThan4.Average()); + Console.WriteLine($"Average value of numbers greater than 4: {moreThan4.Average()}:"); else // handle empty collection Console.WriteLine("The dataset has no values greater than 4."); diff --git a/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Enumerable3.cs b/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Enumerable3.cs index 9563809f60bb9..7cc71ea68197b 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Enumerable3.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Enumerable3.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Linq; @@ -10,8 +10,7 @@ public static void Main() var firstNum = dbQueryResults.First(n => n > 4); - Console.WriteLine("The first value greater than 4 is {0}", - firstNum); + Console.WriteLine($"The first value greater than 4 is {firstNum}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Enumerable4.cs b/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Enumerable4.cs index 586d00658114d..52bc3a5ce7910 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Enumerable4.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Enumerable4.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Linq; @@ -13,8 +13,7 @@ public static void Main() if (firstNum == 0) Console.WriteLine("No value is greater than 4."); else - Console.WriteLine("The first value greater than 4 is {0}", - firstNum); + Console.WriteLine($"The first value greater than 4 is {firstNum}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Enumerable5.cs b/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Enumerable5.cs index 0c2d3d74ef089..a91f0e481f0c0 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Enumerable5.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Enumerable5.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Linq; @@ -11,7 +11,7 @@ public static void Main() var singleObject = dbQueryResults.Single(value => value > 4); // Display results. - Console.WriteLine("{0} is the only value greater than 4", singleObject); + Console.WriteLine($"{singleObject} is the only value greater than 4"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Enumerable6.cs b/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Enumerable6.cs index ea1dbccf3b211..8d2c1be06340b 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Enumerable6.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Enumerable6.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Linq; @@ -11,8 +11,7 @@ public static void Main() var singleObject = dbQueryResults.SingleOrDefault(value => value > 2); if (singleObject != 0) - Console.WriteLine("{0} is the only value greater than 2", - singleObject); + Console.WriteLine($"{singleObject} is the only value greater than 2"); else // Handle an empty collection. Console.WriteLine("No value is greater than 2"); diff --git a/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Iterating1.cs b/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Iterating1.cs index 2bd5a91046d9c..9d9908e52319d 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Iterating1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Iterating1.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Collections.Generic; @@ -10,8 +10,8 @@ public static void Main() foreach (var number in numbers) { int square = (int)Math.Pow(number, 2); - Console.WriteLine("{0}^{1}", number, square); - Console.WriteLine("Adding {0} to the collection...\n", square); + Console.WriteLine($"{number}^{square}"); + Console.WriteLine($"Adding {square} to the collection...\n"); numbers.Add(square); } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Iterating2.cs b/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Iterating2.cs index 9db3ee483a0ee..14116f67fa87d 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Iterating2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Iterating2.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Collections.Generic; @@ -12,8 +12,8 @@ public static void Main() for (int ctr = 0; ctr <= upperBound; ctr++) { int square = (int)Math.Pow(numbers[ctr], 2); - Console.WriteLine("{0}^{1}", numbers[ctr], square); - Console.WriteLine("Adding {0} to the collection...\n", square); + Console.WriteLine($"{numbers[ctr]}^{square}"); + Console.WriteLine($"Adding {square} to the collection...\n"); numbers.Add(square); } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/List_Sort1.cs b/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/List_Sort1.cs index a67c7ef23406a..39fedd45e0d9f 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/List_Sort1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/List_Sort1.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Collections.Generic; @@ -24,7 +24,7 @@ public static void Main() people.Add(new Person1("Jane", "Doe")); people.Sort(); foreach (var person in people) - Console.WriteLine("{0} {1}", person.FirstName, person.LastName); + Console.WriteLine($"{person.FirstName} {person.LastName}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/List_Sort2.cs b/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/List_Sort2.cs index 5e6867b2ce78b..480f962e20f24 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/List_Sort2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/List_Sort2.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Collections.Generic; @@ -30,7 +30,7 @@ public static void Main() people.Add(new Person2("Jane", "Doe")); people.Sort(); foreach (var person in people) - Console.WriteLine("{0} {1}", person.FirstName, person.LastName); + Console.WriteLine($"{person.FirstName} {person.LastName}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/List_Sort3.cs b/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/List_Sort3.cs index 3135810487a82..83855abcedb58 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/List_Sort3.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/List_Sort3.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Collections.Generic; @@ -33,7 +33,7 @@ public static void Main() people.Add(new Person3("Jane", "Doe")); people.Sort(new PersonComparer()); foreach (var person in people) - Console.WriteLine("{0} {1}", person.FirstName, person.LastName); + Console.WriteLine($"{person.FirstName} {person.LastName}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/List_Sort4.cs b/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/List_Sort4.cs index 44c784a3a74f5..b570cc078ab5d 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/List_Sort4.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/List_Sort4.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Collections.Generic; @@ -24,7 +24,7 @@ public static void Main() people.Add(new Person("Jane", "Doe")); people.Sort(PersonComparison); foreach (var person in people) - Console.WriteLine("{0} {1}", person.FirstName, person.LastName); + Console.WriteLine($"{person.FirstName} {person.LastName}"); } public static int PersonComparison(Person x, Person y) diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Object/Equals/csharp/equals2.cs b/docs/fundamentals/runtime-libraries/snippets/System/Object/Equals/csharp/equals2.cs index 83ae5c0e61f71..042751f2634d6 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Object/Equals/csharp/equals2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Object/Equals/csharp/equals2.cs @@ -1,4 +1,4 @@ -// +// using System; class Point2 @@ -77,14 +77,10 @@ public static void Main() Point3D point3Db = new Point3D(5, 5, 2); Point3D point3Dc = new Point3D(5, 5, -1); - Console.WriteLine("{0} = {1}: {2}", - point2D, point3Da, point2D.Equals(point3Da)); - Console.WriteLine("{0} = {1}: {2}", - point2D, point3Db, point2D.Equals(point3Db)); - Console.WriteLine("{0} = {1}: {2}", - point3Da, point3Db, point3Da.Equals(point3Db)); - Console.WriteLine("{0} = {1}: {2}", - point3Da, point3Dc, point3Da.Equals(point3Dc)); + Console.WriteLine($"{point2D} = {point3Da}: {point2D.Equals(point3Da)}"); + Console.WriteLine($"{point2D} = {point3Db}: {point2D.Equals(point3Db)}"); + Console.WriteLine($"{point3Da} = {point3Db}: {point3Da.Equals(point3Db)}"); + Console.WriteLine($"{point3Da} = {point3Dc}: {point3Da.Equals(point3Dc)}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Object/Equals/csharp/equals3.cs b/docs/fundamentals/runtime-libraries/snippets/System/Object/Equals/csharp/equals3.cs index e0419586b0450..b03eeb114abbd 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Object/Equals/csharp/equals3.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Object/Equals/csharp/equals3.cs @@ -1,4 +1,4 @@ -// +// using System; class Rectangle @@ -65,9 +65,9 @@ public static void Main() Rectangle r2 = new Rectangle(0, 0, 100, 200); Rectangle r3 = new Rectangle(0, 0, 150, 200); - Console.WriteLine("{0} = {1}: {2}", r1, r2, r1.Equals(r2)); - Console.WriteLine("{0} = {1}: {2}", r1, r3, r1.Equals(r3)); - Console.WriteLine("{0} = {1}: {2}", r2, r3, r2.Equals(r3)); + Console.WriteLine($"{r1} = {r2}: {r1.Equals(r2)}"); + Console.WriteLine($"{r1} = {r3}: {r1.Equals(r3)}"); + Console.WriteLine($"{r2} = {r3}: {r2.Equals(r3)}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Object/Equals/csharp/equals4.cs b/docs/fundamentals/runtime-libraries/snippets/System/Object/Equals/csharp/equals4.cs index b1ee81a3a58c6..fae811a3bca0e 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Object/Equals/csharp/equals4.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Object/Equals/csharp/equals4.cs @@ -1,4 +1,4 @@ -// +// using System; public struct Complex @@ -43,13 +43,13 @@ public static void Main() cmplx2.re = 2.0; cmplx2.im = 1.0; - Console.WriteLine("{0} <> {1}: {2}", cmplx1, cmplx2, cmplx1 != cmplx2); - Console.WriteLine("{0} = {1}: {2}", cmplx1, cmplx2, cmplx1.Equals(cmplx2)); + Console.WriteLine($"{cmplx1} <> {cmplx2}: {cmplx1 != cmplx2}"); + Console.WriteLine($"{cmplx1} = {cmplx2}: {cmplx1.Equals(cmplx2)}"); cmplx2.re = 4.0; - Console.WriteLine("{0} = {1}: {2}", cmplx1, cmplx2, cmplx1 == cmplx2); - Console.WriteLine("{0} = {1}: {2}", cmplx1, cmplx2, cmplx1.Equals(cmplx2)); + Console.WriteLine($"{cmplx1} = {cmplx2}: {cmplx1 == cmplx2}"); + Console.WriteLine($"{cmplx1} = {cmplx2}: {cmplx1.Equals(cmplx2)}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Object/Equals/csharp/equals_ref.cs b/docs/fundamentals/runtime-libraries/snippets/System/Object/Equals/csharp/equals_ref.cs index a17c1ce6bab77..8db6b1dc08afe 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Object/Equals/csharp/equals_ref.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Object/Equals/csharp/equals_ref.cs @@ -1,4 +1,4 @@ -// +// using System; // Define a reference type that does not override Equals. @@ -26,12 +26,12 @@ public static void Main() Person person2 = new Person(person1a.ToString()); Console.WriteLine("Calling Equals:"); - Console.WriteLine("person1a and person1b: {0}", person1a.Equals(person1b)); - Console.WriteLine("person1a and person2: {0}", person1a.Equals(person2)); + Console.WriteLine($"person1a and person1b: {person1a.Equals(person1b)}"); + Console.WriteLine($"person1a and person2: {person1a.Equals(person2)}"); Console.WriteLine("\nCasting to an Object and calling Equals:"); - Console.WriteLine("person1a and person1b: {0}", ((object) person1a).Equals((object) person1b)); - Console.WriteLine("person1a and person2: {0}", ((object) person1a).Equals((object) person2)); + Console.WriteLine($"person1a and person1b: {((object) person1a).Equals((object) person1b)}"); + Console.WriteLine($"person1a and person2: {((object) person1a).Equals((object) person2)}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Object/Equals/csharp/equals_val1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Object/Equals/csharp/equals_val1.cs index 0245359a8c40e..533e01bf0983e 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Object/Equals/csharp/equals_val1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Object/Equals/csharp/equals_val1.cs @@ -1,4 +1,4 @@ -using System; +using System; public class Example2 { @@ -11,10 +11,7 @@ public static void Main() object object1 = value1; object object2 = value2; - Console.WriteLine("{0} ({1}) = {2} ({3}): {4}", - object1, object1.GetType().Name, - object2, object2.GetType().Name, - object1.Equals(object2)); + Console.WriteLine($"{object1} ({object1.GetType().Name}) = {object2} ({object2.GetType().Name}): {object1.Equals(object2)}"); // The example displays the following output: // 12 (Byte) = 12 (Int32): False diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Object/Equals/csharp/equalssb1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Object/Equals/csharp/equalssb1.cs index 657a910ded38d..b51fd0e2a2a92 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Object/Equals/csharp/equalssb1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Object/Equals/csharp/equalssb1.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Text; @@ -9,14 +9,12 @@ public static void Main() StringBuilder sb1 = new StringBuilder("building a string..."); StringBuilder sb2 = new StringBuilder("building a string..."); - Console.WriteLine("sb1.Equals(sb2): {0}", sb1.Equals(sb2)); - Console.WriteLine("((Object) sb1).Equals(sb2): {0}", - ((Object) sb1).Equals(sb2)); - Console.WriteLine("Object.Equals(sb1, sb2): {0}", - Object.Equals(sb1, sb2)); + Console.WriteLine($"sb1.Equals(sb2): {sb1.Equals(sb2)}"); + Console.WriteLine($"((Object) sb1).Equals(sb2): {((Object) sb1).Equals(sb2)}"); + Console.WriteLine($"Object.Equals(sb1, sb2): {Object.Equals(sb1, sb2)}"); Object sb3 = new StringBuilder("building a string..."); - Console.WriteLine("\nsb3.Equals(sb2): {0}", sb3.Equals(sb2)); + Console.WriteLine($"\nsb3.Equals(sb2): {sb3.Equals(sb2)}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Object/Finalize/csharp/finalize_safe.cs b/docs/fundamentals/runtime-libraries/snippets/System/Object/Finalize/csharp/finalize_safe.cs index 4d902d491060d..2105ed307704a 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Object/Finalize/csharp/finalize_safe.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Object/Finalize/csharp/finalize_safe.cs @@ -1,4 +1,4 @@ -// +// using Microsoft.Win32.SafeHandles; using System; using System.ComponentModel; @@ -146,7 +146,7 @@ public class Example public static void Main() { FileAssociationInfo fa = new FileAssociationInfo(".txt"); - Console.WriteLine("{0} files are handled by '{1}'", fa.Extension, fa.Open); + Console.WriteLine($"{fa.Extension} files are handled by '{fa.Open}'"); fa.Dispose(); } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Object/ToString/csharp/tostringoverload2.cs b/docs/fundamentals/runtime-libraries/snippets/System/Object/ToString/csharp/tostringoverload2.cs index a21cb63caede7..cce98befd3ecf 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Object/ToString/csharp/tostringoverload2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Object/ToString/csharp/tostringoverload2.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Globalization; @@ -11,8 +11,7 @@ public static void Main() Decimal value = 1603.49m; foreach (var cultureName in cultureNames) { CultureInfo culture = new CultureInfo(cultureName); - Console.WriteLine("{0}: {1}", culture.Name, - value.ToString("C2", culture)); + Console.WriteLine($"{culture.Name}: {value.ToString("C2", culture)}"); } } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Random/Overview/csharp/array1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Random/Overview/csharp/array1.cs index d8e6ea8ae5e19..bdff9f6768e0f 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Random/Overview/csharp/array1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Random/Overview/csharp/array1.cs @@ -1,4 +1,4 @@ -using System; +using System; public class Example { @@ -12,8 +12,7 @@ public static void Main() "Raleigh", "San Francisco", "Tulsa", "Washington" }; Random rnd = new Random(); int index = rnd.Next(0, cities.Length); - Console.WriteLine("Today's city of the day: {0}", - cities[index]); + Console.WriteLine($"Today's city of the day: {cities[index]}"); // The example displays output like the following: // Today's city of the day: Honolulu diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Random/Overview/csharp/threadsafeex1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Random/Overview/csharp/threadsafeex1.cs index fbe85041b9452..5dca8aa952b80 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Random/Overview/csharp/threadsafeex1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Random/Overview/csharp/threadsafeex1.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Threading; @@ -47,9 +47,9 @@ private void Execute() countdown.Wait(); source.Dispose(); - Console.WriteLine("\nTotal random numbers generated: {0:N0}", totalCount); - Console.WriteLine("Total sum of all random numbers: {0:N2}", totalValue); - Console.WriteLine("Random number mean: {0:N4}", totalValue / totalCount); + Console.WriteLine($"\nTotal random numbers generated: {totalCount:N0}"); + Console.WriteLine($"Total sum of all random numbers: {totalValue:N2}"); + Console.WriteLine($"Random number mean: {totalValue / totalCount:N4}"); } private void GetRandomNumbers(Object o) @@ -82,11 +82,10 @@ private void GetRandomNumbers(Object o) perThreadTotal += result; } - Console.WriteLine("Thread {0} finished execution.", - Thread.CurrentThread.Name); - Console.WriteLine("Random numbers generated: {0:N0}", perThreadCtr); - Console.WriteLine("Sum of random numbers: {0:N2}", perThreadTotal); - Console.WriteLine("Random number mean: {0:N4}\n", perThreadTotal / perThreadCtr); + Console.WriteLine($"Thread {Thread.CurrentThread.Name} finished execution."); + Console.WriteLine($"Random numbers generated: {perThreadCtr:N0}"); + Console.WriteLine($"Sum of random numbers: {perThreadTotal:N2}"); + Console.WriteLine($"Random number mean: {perThreadTotal / perThreadCtr:N4}\n"); // Update overall totals. lock (numericLock) @@ -97,7 +96,7 @@ private void GetRandomNumbers(Object o) } catch (OperationCanceledException e) { - Console.WriteLine("Corruption in Thread {1}", e.GetType().Name, Thread.CurrentThread.Name); + Console.WriteLine($"Corruption in Thread {Thread.CurrentThread.Name}"); } finally { diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Random/Overview/csharp/threadsafeex2.cs b/docs/fundamentals/runtime-libraries/snippets/System/Random/Overview/csharp/threadsafeex2.cs index 1562b552df6bd..05a4ca7bf7b1a 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Random/Overview/csharp/threadsafeex2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Random/Overview/csharp/threadsafeex2.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Collections.Generic; using System.Threading; @@ -65,10 +65,10 @@ private async Task Execute() } // Show result. - Console.WriteLine("Task {0} finished execution.", taskNo); - Console.WriteLine("Random numbers generated: {0:N0}", taskCtr); - Console.WriteLine("Sum of random numbers: {0:N2}", taskTotal); - Console.WriteLine("Random number mean: {0:N4}\n", taskTotal / taskCtr); + Console.WriteLine($"Task {taskNo} finished execution."); + Console.WriteLine($"Random numbers generated: {taskCtr:N0}"); + Console.WriteLine($"Sum of random numbers: {taskTotal:N2}"); + Console.WriteLine($"Random number mean: {taskTotal / taskCtr:N4}\n"); // Update overall totals. lock (numericLock) @@ -82,9 +82,9 @@ private async Task Execute() try { await Task.WhenAll(tasks.ToArray()); - Console.WriteLine("\nTotal random numbers generated: {0:N0}", totalCount); - Console.WriteLine("Total sum of all random numbers: {0:N2}", totalValue); - Console.WriteLine("Random number mean: {0:N4}", totalValue / totalCount); + Console.WriteLine($"\nTotal random numbers generated: {totalCount:N0}"); + Console.WriteLine($"Total sum of all random numbers: {totalValue:N2}"); + Console.WriteLine($"Random number mean: {totalValue / totalCount:N4}"); } catch (AggregateException e) { @@ -92,9 +92,9 @@ private async Task Execute() { TaskCanceledException canc = inner as TaskCanceledException; if (canc != null) - Console.WriteLine("Task #{0} cancelled.", canc.Task.Id); + Console.WriteLine($"Task #{canc.Task.Id} cancelled."); else - Console.WriteLine("Exception: {0}", inner.GetType().Name); + Console.WriteLine($"Exception: {inner.GetType().Name}"); } } finally diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Random/Overview/csharp/unique.cs b/docs/fundamentals/runtime-libraries/snippets/System/Random/Overview/csharp/unique.cs index 0968859c1ae30..8cf5841f80b3b 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Random/Overview/csharp/unique.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Random/Overview/csharp/unique.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Threading; @@ -13,11 +13,11 @@ public static void Main() Console.WriteLine("\nThe first random number generator:"); for (int ctr = 1; ctr <= 10; ctr++) - Console.WriteLine(" {0}", rnd1.Next()); + Console.WriteLine($" {rnd1.Next()}"); Console.WriteLine("\nThe second random number generator:"); for (int ctr = 1; ctr <= 10; ctr++) - Console.WriteLine(" {0}", rnd2.Next()); + Console.WriteLine($" {rnd2.Next()}"); } } // The example displays output like the following: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Random/Overview/csharp/uniquearray1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Random/Overview/csharp/uniquearray1.cs index f760f561b8eaa..76b086b5cb86f 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Random/Overview/csharp/uniquearray1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Random/Overview/csharp/uniquearray1.cs @@ -1,4 +1,4 @@ -// +// using System; // A class that represents an individual card in a playing deck. @@ -75,8 +75,7 @@ public Card[] Deal(int numberToDeal) if (mustReshuffle & ctr < numberToDeal - 1) { - Console.WriteLine("Can only deal the {0} cards remaining on the deck.", - ctr + 1); + Console.WriteLine($"Can only deal the {ctr + 1} cards remaining on the deck."); return cardsDealt; } } @@ -96,7 +95,7 @@ private static void ShowCards(Card[] cards) { foreach (var card in cards) if (card != null) - Console.WriteLine("{0} of {1}", card.FaceValue, card.Suit); + Console.WriteLine($"{card.FaceValue} of {card.Suit}"); } } // The example displays output like the following: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Single/CompareTo/csharp/compareto2.cs b/docs/fundamentals/runtime-libraries/snippets/System/Single/CompareTo/csharp/compareto2.cs index d67d46f705761..4a0a8cc5b3adb 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Single/CompareTo/csharp/compareto2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Single/CompareTo/csharp/compareto2.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example2 @@ -8,10 +8,8 @@ public static void Main() float value1 = 16.5457f; float operand = 3.8899982f; float value2 = value1 * operand / operand; - Console.WriteLine("Comparing {0} and {1}: {2}\n", - value1, value2, value1.CompareTo(value2)); - Console.WriteLine("Comparing {0:R} and {1:R}: {2}", - value1, value2, value1.CompareTo(value2)); + Console.WriteLine($"Comparing {value1} and {value2}: {value1.CompareTo(value2)}\n"); + Console.WriteLine($"Comparing {value1:R} and {value2:R}: {value1.CompareTo(value2)}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Single/CompareTo/csharp/compareto3.cs b/docs/fundamentals/runtime-libraries/snippets/System/Single/CompareTo/csharp/compareto3.cs index e861477704a76..792ac0a13f5e8 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Single/CompareTo/csharp/compareto3.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Single/CompareTo/csharp/compareto3.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example @@ -8,10 +8,8 @@ public static void Main() float value1 = 16.5457f; float operand = 3.8899982f; object value2 = value1 * operand / operand; - Console.WriteLine("Comparing {0} and {1}: {2}\n", - value1, value2, value1.CompareTo(value2)); - Console.WriteLine("Comparing {0:R} and {1:R}: {2}", - value1, value2, value1.CompareTo(value2)); + Console.WriteLine($"Comparing {value1} and {value2}: {value1.CompareTo(value2)}\n"); + Console.WriteLine($"Comparing {value1:R} and {value2:R}: {value1.CompareTo(value2)}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Single/Epsilon/csharp/epsilon.cs b/docs/fundamentals/runtime-libraries/snippets/System/Single/Epsilon/csharp/epsilon.cs index 78244f5695810..2bf16f5c802c6 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Single/Epsilon/csharp/epsilon.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Single/Epsilon/csharp/epsilon.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example1 @@ -11,9 +11,7 @@ public static void Main() { for (int ctr2 = ctr + 1; ctr2 <= values.Length - 1; ctr2++) { - Console.WriteLine("{0:r} = {1:r}: {2}", - values[ctr], values[ctr2], - values[ctr].Equals(values[ctr2])); + Console.WriteLine($"{values[ctr]:r} = {values[ctr2]:r}: {values[ctr].Equals(values[ctr2])}"); } Console.WriteLine(); } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Single/Equals/csharp/equalsabs1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Single/Equals/csharp/equalsabs1.cs index d30d6849ce0d1..6aa1d68c28ea4 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Single/Equals/csharp/equalsabs1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Single/Equals/csharp/equalsabs1.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example @@ -10,8 +10,7 @@ public static void Main() for (int ctr = 0; ctr < 10; ctr++) value2 += .1f; - Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2, - HasMinimalDifference(value1, value2, 1)); + Console.WriteLine($"{value1:R} = {value2:R}: {HasMinimalDifference(value1, value2, 1)}"); } public static bool HasMinimalDifference(float value1, float value2, int units) diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/PrecisionList5a.cs b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/PrecisionList5a.cs deleted file mode 100644 index 6d0b21443c222..0000000000000 --- a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/PrecisionList5a.cs +++ /dev/null @@ -1,33 +0,0 @@ -// -using System; -using System.IO; - -public class Example11 -{ - public static void Main() - { - StreamWriter sw = new StreamWriter(@".\Singles.dat"); - Single[] values = { 3.2f / 1.11f, 1.0f / 3f, (float)Math.PI }; - for (int ctr = 0; ctr < values.Length; ctr++) - sw.Write("{0:G9}{1}", values[ctr], ctr < values.Length - 1 ? "|" : ""); - - sw.Close(); - - Single[] restoredValues = new Single[values.Length]; - StreamReader sr = new StreamReader(@".\Singles.dat"); - string temp = sr.ReadToEnd(); - string[] tempStrings = temp.Split('|'); - for (int ctr = 0; ctr < tempStrings.Length; ctr++) - restoredValues[ctr] = Single.Parse(tempStrings[ctr]); - - for (int ctr = 0; ctr < values.Length; ctr++) - Console.WriteLine("{0} {2} {1}", values[ctr], - restoredValues[ctr], - values[ctr].Equals(restoredValues[ctr]) ? "=" : "<>"); - } -} -// The example displays the following output: -// 2.882883 = 2.882883 -// 0.3333333 = 0.3333333 -// 3.141593 = 3.141593 -// diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/Program.cs b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/Program.cs new file mode 100644 index 0000000000000..0da786ed61aea --- /dev/null +++ b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/Program.cs @@ -0,0 +1 @@ +Example10.Main(); diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/Project.csproj b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/Project.csproj index 251a8cb33ffed..f2ef551a953f3 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/Project.csproj +++ b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/Project.csproj @@ -1,7 +1,7 @@ - Library + Exe net8 diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/comparison1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/comparison1.cs index d7ba9b768c62c..965c922e4f90e 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/comparison1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/comparison1.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example @@ -7,7 +7,7 @@ public static void Main() { float value1 = .3333333f; float value2 = 1.0f/3; - Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2, value1.Equals(value2)); + Console.WriteLine($"{value1:R} = {value2:R}: {value1.Equals(value2)}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/comparison2.cs b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/comparison2.cs index a388a4a970d1c..ada6b6e80f4e4 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/comparison2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/comparison2.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example1 @@ -9,9 +9,8 @@ public static void Main() value1 = (float)Math.Sqrt((float)Math.Pow(value1, 2)); float value2 = (float)Math.Pow((float)value1 * 3.51f, 2); value2 = ((float)Math.Sqrt(value2)) / 3.51f; - Console.WriteLine("{0} = {1}: {2}\n", - value1, value2, value1.Equals(value2)); - Console.WriteLine("{0:G9} = {1:G9}", value1, value2); + Console.WriteLine($"{value1} = {value2}: {value1.Equals(value2)}\n"); + Console.WriteLine($"{value1:G9} = {value2:G9}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/comparison3.cs b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/comparison3.cs index 11a6d92e9f85e..b5d19a7fb0dd5 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/comparison3.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/comparison3.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example2 @@ -10,7 +10,7 @@ public static void Main() int precision = 7; value1 = (float)Math.Round(value1, precision); value2 = (float)Math.Round(value2, precision); - Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2, value1.Equals(value2)); + Console.WriteLine($"{value1:R} = {value2:R}: {value1.Equals(value2)}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/comparison4.cs b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/comparison4.cs index ca47c5ed1ca4d..3d8108233dba3 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/comparison4.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/comparison4.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example3 @@ -10,10 +10,8 @@ public static void Main() for (int ctr = 1; ctr <= 10; ctr++) one2 += .1f; - Console.WriteLine("{0:R} = {1:R}: {2}", one1, one2, one1.Equals(one2)); - Console.WriteLine("{0:R} is approximately equal to {1:R}: {2}", - one1, one2, - IsApproximatelyEqual(one1, one2, .000001f)); + Console.WriteLine($"{one1:R} = {one2:R}: {one1.Equals(one2)}"); + Console.WriteLine($"{one1:R} is approximately equal to {one2:R}: {IsApproximatelyEqual(one1, one2, .000001f)}"); } static bool IsApproximatelyEqual(float value1, float value2, float epsilon) diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/convert1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/convert1.cs index 39dd58f79346c..8a5e0e75a9707 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/convert1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/convert1.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example4 @@ -20,9 +20,7 @@ public static void Main() sngValue = (float)value; else sngValue = value; - Console.WriteLine("{0} ({1}) --> {2:R} ({3})", - value, value.GetType().Name, - sngValue, sngValue.GetType().Name); + Console.WriteLine($"{value} ({value.GetType().Name}) --> {sngValue:R} ({sngValue.GetType().Name})"); } } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/convert2.cs b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/convert2.cs index 80770f04477e6..6115fe2068629 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/convert2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/convert2.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example5 @@ -16,41 +16,33 @@ public static void Main() try { Int64 lValue = (long)value; - Console.WriteLine("{0} ({1}) --> {2} (0x{2:X16}) ({3})", - value, value.GetType().Name, - lValue, lValue.GetType().Name); + Console.WriteLine($"{value} ({value.GetType().Name}) --> {lValue} (0x{lValue:X16}) ({lValue.GetType().Name})"); } catch (OverflowException) { - Console.WriteLine("Unable to convert {0} to Int64.", value); + Console.WriteLine($"Unable to convert {value} to Int64."); } try { UInt64 ulValue = (ulong)value; - Console.WriteLine("{0} ({1}) --> {2} (0x{2:X16}) ({3})", - value, value.GetType().Name, - ulValue, ulValue.GetType().Name); + Console.WriteLine($"{value} ({value.GetType().Name}) --> {ulValue} (0x{ulValue:X16}) ({ulValue.GetType().Name})"); } catch (OverflowException) { - Console.WriteLine("Unable to convert {0} to UInt64.", value); + Console.WriteLine($"Unable to convert {value} to UInt64."); } try { Decimal dValue = (decimal)value; - Console.WriteLine("{0} ({1}) --> {2} ({3})", - value, value.GetType().Name, - dValue, dValue.GetType().Name); + Console.WriteLine($"{value} ({value.GetType().Name}) --> {dValue} ({dValue.GetType().Name})"); } catch (OverflowException) { - Console.WriteLine("Unable to convert {0} to Decimal.", value); + Console.WriteLine($"Unable to convert {value} to Decimal."); } Double dblValue = value; - Console.WriteLine("{0} ({1}) --> {2} ({3})", - value, value.GetType().Name, - dblValue, dblValue.GetType().Name); + Console.WriteLine($"{value} ({value.GetType().Name}) --> {dblValue} ({dblValue.GetType().Name})"); Console.WriteLine(); } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/exceptional1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/exceptional1.cs index 60e81b5b9658a..50d367bb20202 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/exceptional1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/exceptional1.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example6 @@ -8,8 +8,8 @@ public static void Main() float value1 = 1.163287e-36f; float value2 = 9.164234e-25f; float result = value1 * value2; - Console.WriteLine("{0} * {1} = {2}", value1, value2, result); - Console.WriteLine("{0} = 0: {1}", result, result.Equals(0.0f)); + Console.WriteLine($"{value1} * {value2} = {result}"); + Console.WriteLine($"{result} = 0: {result.Equals(0.0f)}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/exceptional2.cs b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/exceptional2.cs index 043ec2df42403..14e080de0432e 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/exceptional2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/exceptional2.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example7 @@ -8,17 +8,13 @@ public static void Main() float value1 = 3.065e35f; float value2 = 6.9375e32f; float result = value1 * value2; - Console.WriteLine("PositiveInfinity: {0}", - Single.IsPositiveInfinity(result)); - Console.WriteLine("NegativeInfinity: {0}\n", - Single.IsNegativeInfinity(result)); + Console.WriteLine($"PositiveInfinity: {Single.IsPositiveInfinity(result)}"); + Console.WriteLine($"NegativeInfinity: {Single.IsNegativeInfinity(result)}\n"); value1 = -value1; result = value1 * value2; - Console.WriteLine("PositiveInfinity: {0}", - Single.IsPositiveInfinity(result)); - Console.WriteLine("NegativeInfinity: {0}", - Single.IsNegativeInfinity(result)); + Console.WriteLine($"PositiveInfinity: {Single.IsPositiveInfinity(result)}"); + Console.WriteLine($"NegativeInfinity: {Single.IsNegativeInfinity(result)}"); } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/precisionlist1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/precisionlist1.cs index b2b41598222f5..badd8b5697673 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/precisionlist1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/precisionlist1.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example8 @@ -8,8 +8,7 @@ public static void Main() Double value1 = 1 / 3.0; Single sValue2 = 1 / 3.0f; Double value2 = (Double)sValue2; - Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2, - value1.Equals(value2)); + Console.WriteLine($"{value1:R} = {value2:R}: {value1.Equals(value2)}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/precisionlist3.cs b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/precisionlist3.cs index 4f0dbce51982b..ac73f8afb67be 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/precisionlist3.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/precisionlist3.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example9 @@ -14,8 +14,7 @@ public static void Main() if (total.Equals(result)) Console.WriteLine("The sum of the values equals the total."); else - Console.WriteLine("The sum of the values ({0}) does not equal the total ({1}).", - total, result); + Console.WriteLine($"The sum of the values ({total}) does not equal the total ({result})."); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/precisionlist4a.cs b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/precisionlist4a.cs index 2078702478df0..f03faccef363a 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/precisionlist4a.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/precisionlist4a.cs @@ -1,13 +1,13 @@ -// -using System; +using System; using System.IO; public class Example10 { public static void Main() { - StreamWriter sw = new StreamWriter(@".\Singles.dat"); - Single[] values = { 3.2f / 1.11f, 1.0f / 3f, (float)Math.PI }; + // + StreamWriter sw = new(@".\Singles.dat"); + float[] values = { 3.2f / 1.11f, 1.0f / 3f, (float)Math.PI }; for (int ctr = 0; ctr < values.Length; ctr++) { sw.Write(values[ctr].ToString()); @@ -16,21 +16,20 @@ public static void Main() } sw.Close(); - Single[] restoredValues = new Single[values.Length]; - StreamReader sr = new StreamReader(@".\Singles.dat"); + float[] restoredValues = new float[values.Length]; + StreamReader sr = new(@".\Singles.dat"); string temp = sr.ReadToEnd(); string[] tempStrings = temp.Split('|'); for (int ctr = 0; ctr < tempStrings.Length; ctr++) - restoredValues[ctr] = Single.Parse(tempStrings[ctr]); + restoredValues[ctr] = float.Parse(tempStrings[ctr]); for (int ctr = 0; ctr < values.Length; ctr++) - Console.WriteLine("{0} {2} {1}", values[ctr], - restoredValues[ctr], - values[ctr].Equals(restoredValues[ctr]) ? "=" : "<>"); + Console.WriteLine($"{values[ctr]} {(values[ctr].Equals(restoredValues[ctr]) ? "=" : "<>")} {restoredValues[ctr]}"); + + // The example displays the following output: + // 2.882883 <> 2.882883 + // 0.3333333 <> 0.3333333 + // 3.141593 <> 3.141593 + // } } -// The example displays the following output: -// 2.882883 <> 2.882883 -// 0.3333333 <> 0.3333333 -// 3.141593 <> 3.141593 -// diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/representation1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/representation1.cs index b91935ccfa4bd..144b29306b7cb 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/representation1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/representation1.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example12 @@ -11,8 +11,8 @@ public static void Main() for (int ctr = 1; ctr <= 10; ctr++) result2 += value; - Console.WriteLine(".2 * 10: {0:R}", result1); - Console.WriteLine(".2 Added 10 times: {0:R}", result2); + Console.WriteLine($".2 * 10: {result1:R}"); + Console.WriteLine($".2 Added 10 times: {result2:R}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/String/IsNullOrEmpty/csharp/NullString1.cs b/docs/fundamentals/runtime-libraries/snippets/System/String/IsNullOrEmpty/csharp/NullString1.cs index cbeb40830a95b..c9da93895bacc 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/String/IsNullOrEmpty/csharp/NullString1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/String/IsNullOrEmpty/csharp/NullString1.cs @@ -1,4 +1,4 @@ -using System; +using System; public class Example { @@ -7,11 +7,11 @@ public static void Main() // String s = null; - Console.WriteLine("The value of the string is '{0}'", s); + Console.WriteLine($"The value of the string is '{s}'"); try { - Console.WriteLine("String length is {0}", s.Length); + Console.WriteLine($"String length is {s.Length}"); } catch (NullReferenceException e) { @@ -31,7 +31,7 @@ public void Test() { // String s = ""; - Console.WriteLine("The length of '{0}' is {1}.", s, s.Length); + Console.WriteLine($"The length of '{s}' is {s.Length}."); // The example displays the following output: // The length of '' is 0. diff --git a/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/case2.cs b/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/case2.cs index 7de5ec46cd4dc..966719e67c84a 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/case2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/case2.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Globalization; using System.Threading; @@ -24,10 +24,9 @@ private static void IsAccessAllowed(String resource) // Change the current culture and perform the comparison. foreach (var culture in cultures) { Thread.CurrentThread.CurrentCulture = culture; - Console.WriteLine("Culture: {0}", CultureInfo.CurrentCulture.DisplayName); + Console.WriteLine($"Culture: {CultureInfo.CurrentCulture.DisplayName}"); Console.WriteLine(resource); - Console.WriteLine("Access allowed: {0}", - ! String.Equals(disallowed, scheme, StringComparison.CurrentCultureIgnoreCase)); + Console.WriteLine($"Access allowed: {! String.Equals(disallowed, scheme, StringComparison.CurrentCultureIgnoreCase)}"); Console.WriteLine(); } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/compare3.cs b/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/compare3.cs index bc8a6fc79ae57..832720bc04a68 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/compare3.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/compare3.cs @@ -1,4 +1,4 @@ -// +// using System; public class Example8 @@ -23,11 +23,9 @@ private static void FindInString(string s, string substring, StringComparison op { int result = s.IndexOf(substring, options); if (result != -1) - Console.WriteLine("'{0}' found in {1} at position {2}", - substring, s, result); + Console.WriteLine($"'{substring}' found in {s} at position {result}"); else - Console.WriteLine("'{0}' not found in {1}", - substring, s); + Console.WriteLine($"'{substring}' not found in {s}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/compare4.cs b/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/compare4.cs index eb56bef05bc7b..8aa8cbae65f5d 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/compare4.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/compare4.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Globalization; using System.Threading; @@ -13,30 +13,20 @@ public static void Main() // Set the current culture to Danish in Denmark. Thread.CurrentThread.CurrentCulture = new CultureInfo("da-DK"); - Console.WriteLine("Current culture: {0}", - CultureInfo.CurrentCulture.Name); - Console.WriteLine("Comparison of {0} with {1}: {2}", - str1, str2, String.Compare(str1, str2)); - Console.WriteLine("Comparison of {0} with {1}: {2}\n", - str2, str3, String.Compare(str2, str3)); + Console.WriteLine($"Current culture: {CultureInfo.CurrentCulture.Name}"); + Console.WriteLine($"Comparison of {str1} with {str2}: {String.Compare(str1, str2)}"); + Console.WriteLine($"Comparison of {str2} with {str3}: {String.Compare(str2, str3)}\n"); // Set the current culture to English in the U.S. Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); - Console.WriteLine("Current culture: {0}", - CultureInfo.CurrentCulture.Name); - Console.WriteLine("Comparison of {0} with {1}: {2}", - str1, str2, String.Compare(str1, str2)); - Console.WriteLine("Comparison of {0} with {1}: {2}\n", - str2, str3, String.Compare(str2, str3)); + Console.WriteLine($"Current culture: {CultureInfo.CurrentCulture.Name}"); + Console.WriteLine($"Comparison of {str1} with {str2}: {String.Compare(str1, str2)}"); + Console.WriteLine($"Comparison of {str2} with {str3}: {String.Compare(str2, str3)}\n"); // Perform an ordinal comparison. Console.WriteLine("Ordinal comparison"); - Console.WriteLine("Comparison of {0} with {1}: {2}", - str1, str2, - String.Compare(str1, str2, StringComparison.Ordinal)); - Console.WriteLine("Comparison of {0} with {1}: {2}", - str2, str3, - String.Compare(str2, str3, StringComparison.Ordinal)); + Console.WriteLine($"Comparison of {str1} with {str2}: {String.Compare(str1, str2, StringComparison.Ordinal)}"); + Console.WriteLine($"Comparison of {str2} with {str3}: {String.Compare(str2, str3, StringComparison.Ordinal)}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/equality1.cs b/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/equality1.cs index 5d0e0e54d85cb..dd7b9583e7a8c 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/equality1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/equality1.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Globalization; using System.Threading; @@ -13,15 +13,15 @@ public static void Main() Console.WriteLine("Culture-sensitive test for equality:"); if (! TestForEquality(filePath, StringComparison.CurrentCultureIgnoreCase)) - Console.WriteLine("Access to {0} is allowed.", filePath); + Console.WriteLine($"Access to {filePath} is allowed."); else - Console.WriteLine("Access to {0} is not allowed.", filePath); + Console.WriteLine($"Access to {filePath} is not allowed."); Console.WriteLine("\nOrdinal test for equality:"); if (! TestForEquality(filePath, StringComparison.OrdinalIgnoreCase)) - Console.WriteLine("Access to {0} is allowed.", filePath); + Console.WriteLine($"Access to {filePath} is allowed."); else - Console.WriteLine("Access to {0} is not allowed.", filePath); + Console.WriteLine($"Access to {filePath} is not allowed."); } private static bool TestForEquality(string str, StringComparison cmp) diff --git a/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/index11.cs b/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/index11.cs index 333dbeca0a1f1..73a175e96a7df 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/index11.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/index11.cs @@ -1,4 +1,4 @@ -using System; +using System; public class Example7 { @@ -13,8 +13,7 @@ public static void Main() if (Char.IsPunctuation(s1[ctr]) | Char.IsWhiteSpace(s1[ctr])) nWords++; } - Console.WriteLine("The sentence\n {0}\nhas {1} words.", - s1, nWords); + Console.WriteLine($"The sentence\n {s1}\nhas {nWords} words."); // The example displays the following output: // The sentence // This string consists of a single short sentence. diff --git a/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/index2.cs b/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/index2.cs index 9fbb137811a3f..b5174628209f6 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/index2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/index2.cs @@ -1,4 +1,4 @@ -using System; +using System; public class Example11 { @@ -13,8 +13,7 @@ public static void Main() if (Char.IsPunctuation(ch) | Char.IsWhiteSpace(ch)) nWords++; } - Console.WriteLine("The sentence\n {0}\nhas {1} words.", - s1, nWords); + Console.WriteLine($"The sentence\n {s1}\nhas {nWords} words."); // The example displays the following output: // The sentence // This string consists of a single short sentence. diff --git a/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/search1.cs b/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/search1.cs index c697f33eaba86..b2828ea6c9395 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/search1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/search1.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Globalization; @@ -12,14 +12,12 @@ public static void Main() Char ch = 'æ'; // U+00E6 Console.Write("Ordinal comparison -- "); - Console.WriteLine("Position of '{0}' in {1}: {2}", ch, str, - str.IndexOf(ch)); + Console.WriteLine($"Position of '{ch}' in {str}: {str.IndexOf(ch)}"); foreach (var cultureName in cultureNames) { ci = CultureInfo.CreateSpecificCulture(cultureName).CompareInfo; Console.Write("{0} cultural comparison -- ", cultureName); - Console.WriteLine("Position of '{0}' in {1}: {2}", ch, str, - ci.IndexOf(str, ch)); + Console.WriteLine($"Position of '{ch}' in {str}: {ci.IndexOf(str, ch)}"); } } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/sort1.cs b/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/sort1.cs index d982852d438e4..fd1eaf9bd1eea 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/sort1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/sort1.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Globalization; using System.Threading; @@ -36,7 +36,7 @@ public static void PrintIndexAndValues(string[] myArray) { for (int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ ) - Console.WriteLine("[{0}]: {1}", i, myArray[i]); + Console.WriteLine($"[{i}]: {myArray[i]}"); Console.WriteLine(); } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/surrogate1.cs b/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/surrogate1.cs index 3799bc77d2ede..18b5cd6f57768 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/surrogate1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/surrogate1.cs @@ -1,4 +1,4 @@ -using System; +using System; public class Example18 { @@ -10,8 +10,7 @@ public static void Main() Console.Write($"U+{(ushort)surrogate[ctr]:X2} "); Console.WriteLine(); - Console.WriteLine(" Is Surrogate Pair: {0}", - Char.IsSurrogatePair(surrogate[0], surrogate[1])); + Console.WriteLine($" Is Surrogate Pair: {Char.IsSurrogatePair(surrogate[0], surrogate[1])}"); // The example displays the following output: // U+D800 U+DC03 // Is Surrogate Pair: True diff --git a/docs/fundamentals/runtime-libraries/snippets/System/TimeSpan/Overview/csharp/instantiate1.cs b/docs/fundamentals/runtime-libraries/snippets/System/TimeSpan/Overview/csharp/instantiate1.cs index a2b58defd4837..c93d7f0128538 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/TimeSpan/Overview/csharp/instantiate1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/TimeSpan/Overview/csharp/instantiate1.cs @@ -1,4 +1,4 @@ -using System; +using System; public class Example { @@ -38,7 +38,7 @@ private static void TimeSpanOperation() DateTime departure = new DateTime(2010, 6, 12, 18, 32, 0); DateTime arrival = new DateTime(2010, 6, 13, 22, 47, 0); TimeSpan travelTime = arrival - departure; - Console.WriteLine("{0} - {1} = {2}", arrival, departure, travelTime); + Console.WriteLine($"{arrival} - {departure} = {travelTime}"); // The example displays the following output: // 6/13/2010 10:47:00 PM - 6/12/2010 6:32:00 PM = 1.04:15:00 @@ -53,13 +53,13 @@ private static void Parse() { try { TimeSpan ts = TimeSpan.Parse(value); - Console.WriteLine("'{0}' --> {1}", value, ts); + Console.WriteLine($"'{value}' --> {ts}"); } catch (FormatException) { - Console.WriteLine("Unable to parse '{0}'", value); + Console.WriteLine($"Unable to parse '{value}'"); } catch (OverflowException) { - Console.WriteLine("'{0}' is outside the range of a TimeSpan.", value); + Console.WriteLine($"'{value}' is outside the range of a TimeSpan."); } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/TimeSpan/Overview/csharp/legacycode1.cs b/docs/fundamentals/runtime-libraries/snippets/System/TimeSpan/Overview/csharp/legacycode1.cs index 0a303a683e6c0..80ea800b80002 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/TimeSpan/Overview/csharp/legacycode1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/TimeSpan/Overview/csharp/legacycode1.cs @@ -1,4 +1,4 @@ -using System; +using System; public class Example1 { @@ -38,15 +38,15 @@ void ShowParsingCode() try { TimeSpan interval = TimeSpan.Parse(value); - Console.WriteLine("{0} --> {1}", value, interval); + Console.WriteLine($"{value} --> {interval}"); } catch (FormatException) { - Console.WriteLine("{0}: Bad Format", value); + Console.WriteLine($"{value}: Bad Format"); } catch (OverflowException) { - Console.WriteLine("{0}: Overflow", value); + Console.WriteLine($"{value}: Overflow"); } } // diff --git a/docs/fundamentals/runtime-libraries/snippets/System/TimeSpan/Overview/csharp/zero1.cs b/docs/fundamentals/runtime-libraries/snippets/System/TimeSpan/Overview/csharp/zero1.cs index dbb51211d76ff..50334405583fb 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/TimeSpan/Overview/csharp/zero1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/TimeSpan/Overview/csharp/zero1.cs @@ -1,4 +1,4 @@ -using System; +using System; public class Example4 { @@ -12,7 +12,7 @@ public static void Main() timeSpent += GetTimeBeforeLunch(); timeSpent += GetTimeAfterLunch(); - Console.WriteLine("Total time: {0}", timeSpent); + Console.WriteLine($"Total time: {timeSpent}"); TimeSpan GetTimeBeforeLunch() { diff --git a/docs/fundamentals/runtime-libraries/snippets/System/TimeSpan/Parse/csharp/parsefailure1.cs b/docs/fundamentals/runtime-libraries/snippets/System/TimeSpan/Parse/csharp/parsefailure1.cs index 23d1a7e39896b..a39e989f2b017 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/TimeSpan/Parse/csharp/parsefailure1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/TimeSpan/Parse/csharp/parsefailure1.cs @@ -1,4 +1,4 @@ -using System; +using System; public class Example { @@ -10,13 +10,13 @@ public static void Main() { try { TimeSpan interval = TimeSpan.Parse(value); - Console.WriteLine("{0} --> {1}", value, interval); + Console.WriteLine($"{value} --> {interval}"); } catch (FormatException) { - Console.WriteLine("{0}: Bad Format", value); + Console.WriteLine($"{value}: Bad Format"); } catch (OverflowException) { - Console.WriteLine("{0}: Overflow", value); + Console.WriteLine($"{value}: Overflow"); } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/TimeSpan/TryParse/csharp/tryparsefailure1.cs b/docs/fundamentals/runtime-libraries/snippets/System/TimeSpan/TryParse/csharp/tryparsefailure1.cs index 875605e60bf8b..1eb14f20926aa 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/TimeSpan/TryParse/csharp/tryparsefailure1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/TimeSpan/TryParse/csharp/tryparsefailure1.cs @@ -1,4 +1,4 @@ -using System; +using System; public class Example { @@ -8,9 +8,9 @@ public static void Main() string value = "000000006"; TimeSpan interval; if (TimeSpan.TryParse(value, out interval)) - Console.WriteLine("{0} --> {1}", value, interval); + Console.WriteLine($"{value} --> {interval}"); else - Console.WriteLine("Unable to parse '{0}'", value); + Console.WriteLine($"Unable to parse '{value}'"); // Output from .NET Framework 3.5 and earlier versions: // 000000006 --> 6.00:00:00 diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Type/Overview/csharp/Equals1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Type/Overview/csharp/Equals1.cs index 8e82a42407ce6..47792afadb8e3 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Type/Overview/csharp/Equals1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Type/Overview/csharp/Equals1.cs @@ -1,4 +1,4 @@ -using System; +using System; public class Example { @@ -14,12 +14,9 @@ public static void Main() Type t = number1.GetType(); // Compare types of all objects with number1. - Console.WriteLine("Type of number1 and number2 are equal: {0}", - Object.ReferenceEquals(t, number2.GetType())); - Console.WriteLine("Type of number1 and number3 are equal: {0}", - Object.ReferenceEquals(t, number3.GetType())); - Console.WriteLine("Type of number1 and number4 are equal: {0}", - Object.ReferenceEquals(t, number4.GetType())); + Console.WriteLine($"Type of number1 and number2 are equal: {Object.ReferenceEquals(t, number2.GetType())}"); + Console.WriteLine($"Type of number1 and number3 are equal: {Object.ReferenceEquals(t, number3.GetType())}"); + Console.WriteLine($"Type of number1 and number4 are equal: {Object.ReferenceEquals(t, number4.GetType())}"); // The example displays the following output: // Type of number1 and number2 are equal: False diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Type/Overview/csharp/GetType1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Type/Overview/csharp/GetType1.cs index 7f4224265c310..b304b9c666cbb 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Type/Overview/csharp/GetType1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Type/Overview/csharp/GetType1.cs @@ -1,4 +1,4 @@ -using System; +using System; public class Example1 { @@ -7,8 +7,7 @@ public static void Main() // object[] values = { "word", true, 120, 136.34, 'a' }; foreach (var value in values) - Console.WriteLine("{0} - type {1}", value, - value.GetType().Name); + Console.WriteLine($"{value} - type {value.GetType().Name}"); // The example displays the following output: // word - type String diff --git a/docs/fundamentals/runtime-libraries/snippets/System/TypeInitializationException/Overview/csharp/Regex1.cs b/docs/fundamentals/runtime-libraries/snippets/System/TypeInitializationException/Overview/csharp/Regex1.cs index f0737a4089402..9d541e4240b85 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/TypeInitializationException/Overview/csharp/Regex1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/TypeInitializationException/Overview/csharp/Regex1.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Text.RegularExpressions; @@ -11,9 +11,8 @@ public static void Main() domain.SetData("REGEX_DEFAULT_MATCH_TIMEOUT", TimeSpan.FromSeconds(-2)); Regex rgx = new Regex("[aeiouy]"); - Console.WriteLine("Regular expression pattern: {0}", rgx.ToString()); - Console.WriteLine("Timeout interval for this regex: {0} seconds", - rgx.MatchTimeout.TotalSeconds); + Console.WriteLine($"Regular expression pattern: {rgx.ToString()}"); + Console.WriteLine($"Timeout interval for this regex: {rgx.MatchTimeout.TotalSeconds} seconds"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Version/Overview/csharp/GettingVersions1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Version/Overview/csharp/GettingVersions1.cs index 935436e20f12c..7f3c7d773cc6f 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Version/Overview/csharp/GettingVersions1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Version/Overview/csharp/GettingVersions1.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Reflection; [assembly: CLSCompliant(true)] @@ -25,7 +25,7 @@ private static void GetOsVersion() // Get the operating system version. OperatingSystem os = Environment.OSVersion; Version ver = os.Version; - Console.WriteLine("Operating System: {0} ({1})", os.VersionString, ver.ToString()); + Console.WriteLine($"Operating System: {os.VersionString} ({ver.ToString()})"); // } @@ -34,7 +34,7 @@ private static void GetClrVersion() // // Get the common language runtime version. Version ver = Environment.Version; - Console.WriteLine("CLR Version {0}", ver.ToString()); + Console.WriteLine($"CLR Version {ver.ToString()}"); // } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Version/Overview/csharp/clickonce.cs b/docs/fundamentals/runtime-libraries/snippets/System/Version/Overview/csharp/clickonce.cs index e05774aa507a3..cf0bcd4cb442b 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Version/Overview/csharp/clickonce.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Version/Overview/csharp/clickonce.cs @@ -1,4 +1,4 @@ -// +// using System; using System.Deployment.Application; @@ -7,7 +7,7 @@ public class Example public static void Main() { Version ver = ApplicationDeployment.CurrentDeployment.CurrentVersion; - Console.WriteLine("ClickOnce Publish Version: {0}", ver); + Console.WriteLine($"ClickOnce Publish Version: {ver}"); } } // diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Version/Overview/csharp/comparisons1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Version/Overview/csharp/comparisons1.cs index 027c22a8835ce..d50e6800846c4 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Version/Overview/csharp/comparisons1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Version/Overview/csharp/comparisons1.cs @@ -1,4 +1,4 @@ -using System; +using System; public class Example7 { @@ -25,7 +25,7 @@ private static void CompareSimple() Console.Write("earlier than"); break; } - Console.WriteLine(" Version {0}.", v2); + Console.WriteLine($" Version {v2}."); // The example displays the following output: // Version 2.0 is earlier than Version 2.1. // diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Version/Overview/csharp/comparisons2.cs b/docs/fundamentals/runtime-libraries/snippets/System/Version/Overview/csharp/comparisons2.cs index d5a9d04e9af70..a0b01047657fc 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Version/Overview/csharp/comparisons2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Version/Overview/csharp/comparisons2.cs @@ -1,4 +1,4 @@ -// +// using System; enum VersionTime {Earlier = -1, Same = 0, Later = 1 }; @@ -17,8 +17,7 @@ public static void Main() private static void ShowRelationship(Version v1, Version v2) { - Console.WriteLine("Relationship of {0} to {1}: {2}", - v1, v2, (VersionTime) v1.CompareTo(v2)); + Console.WriteLine($"Relationship of {v1} to {v2}: {(VersionTime) v1.CompareTo(v2)}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/system-double.md b/docs/fundamentals/runtime-libraries/system-double.md index c8c9f608e2a8c..5213cd68488fd 100644 --- a/docs/fundamentals/runtime-libraries/system-double.md +++ b/docs/fundamentals/runtime-libraries/system-double.md @@ -51,20 +51,15 @@ The limited precision of a floating-point number has several consequences: When accuracy in numeric operations with fractional values is important, you can use the rather than the type. When accuracy in numeric operations with integral values beyond the range of the types is important, use the type. -- A value might not round-trip if a floating-point number is involved. A value is said to round-trip if an operation converts an original floating-point number to another form, an inverse operation transforms the converted form back to a floating-point number, and the final floating-point number is not equal to the original floating-point number. The round trip might fail because one or more least significant digits are lost or changed in a conversion. In the following example, three values are converted to strings and saved in a file. As the output shows, however, even though the values appear to be identical, the restored values are not equal to the original values. +- A value might not *round-trip* if a floating-point number is involved. A value is said to round-trip if an operation converts an original floating-point number to another form, an inverse operation transforms the converted form back to a floating-point number, and the final floating-point number is equal to the original floating-point number. The round trip might fail because one or more least significant digits are lost or changed in the conversion. + + In the following example, three values are converted to strings and saved in a file. If you run this example on .NET Framework, even though the values appear to be identical, the restored values are not equal to the original values. (This has since been addressed in .NET, where the values round-trip correctly.) :::code language="csharp" source="./snippets/System/Double/Overview/csharp/precisionlist4.cs" id="Snippet7"::: :::code language="fsharp" source="./snippets/System/Double/Overview/fsharp/precisionlist4.fs" id="Snippet7"::: :::code language="vb" source="./snippets/System/Double/Overview/vb/precisionlist4.vb" id="Snippet7"::: - In this case, the values can be successfully round-tripped by using the "G17" [standard numeric format string](../../standard/base-types/standard-numeric-format-strings.md) to preserve the full precision of values, as the following example shows. - - :::code language="csharp" source="./snippets/System/Double/Overview/csharp/precisionlist5.cs" id="Snippet8"::: - :::code language="fsharp" source="./snippets/System/Double/Overview/fsharp/precisionlist5.fs" id="Snippet8"::: - :::code language="vb" source="./snippets/System/Double/Overview/vb/precisionlist5.vb" id="Snippet8"::: - - > [!IMPORTANT] - > When used with a value, the "R" format specifier in some cases fails to successfully round-trip the original value. To ensure that values successfully round-trip, use the "G17" format specifier. + If you're targeting .NET Framework, the values can be successfully round-tripped by using the "G17" [standard numeric format string](../../standard/base-types/standard-numeric-format-strings.md) to preserve the full precision of values. - values have less precision than values. A value that is converted to a seemingly equivalent often does not equal the value because of differences in precision. In the following example, the result of identical division operations is assigned to a and a value. After the value is cast to a , a comparison of the two values shows that they are unequal. diff --git a/docs/fundamentals/runtime-libraries/system-single.md b/docs/fundamentals/runtime-libraries/system-single.md index b880c44f522ba..8a97f9ab8fdb2 100644 --- a/docs/fundamentals/runtime-libraries/system-single.md +++ b/docs/fundamentals/runtime-libraries/system-single.md @@ -53,17 +53,15 @@ The limited precision of a floating-point number has several consequences: When accuracy in numeric operations with fractional values is important, use the type instead of the type. When accuracy in numeric operations with integral values beyond the range of the or types is important, use the type. -- A value might not round-trip if a floating-point number is involved. A value is said to round-trip if an operation converts an original floating-point number to another form, an inverse operation transforms the converted form back to a floating-point number, and the final floating-point number is equal to the original floating-point number. The round trip might fail because one or more least significant digits are lost or changed in a conversion. In the following example, three values are converted to strings and saved in a file. As the output shows, although the values appear to be identical, the restored values are not equal to the original values. +- A value might not *round-trip* if a floating-point number is involved. A value is said to round-trip if an operation converts an original floating-point number to another form, an inverse operation transforms the converted form back to a floating-point number, and the final floating-point number is equal to the original floating-point number. The round trip might fail because one or more least significant digits are lost or changed in a conversion. + + In the following example, three values are converted to strings and saved in a file. If you run this example on .NET Framework, even though the values appear to be identical, the restored values are not equal to the original values. (This has since been addressed in .NET, where the values round-trip correctly.) :::code language="csharp" source="./snippets/System/Single/Overview/csharp/precisionlist4a.cs" id="Snippet17"::: :::code language="fsharp" source="./snippets/System/Single/Overview/fsharp/precisionlist4a.fs" id="Snippet17"::: :::code language="vb" source="./snippets/System/Single/Overview/vb/PrecisionList4a.vb" id="Snippet17"::: - In this case, the values can be successfully round-tripped by using the "G9" [standard numeric format string](../../standard/base-types/standard-numeric-format-strings.md) to preserve the full precision of values, as the following example shows. - - :::code language="csharp" source="./snippets/System/Single/Overview/csharp/PrecisionList5a.cs" id="Snippet18"::: - :::code language="fsharp" source="./snippets/System/Single/Overview/fsharp/PrecisionList5a.fs" id="Snippet18"::: - :::code language="vb" source="./snippets/System/Single/Overview/vb/PrecisionList5a.vb" id="Snippet18"::: + If you're targeting .NET Framework, the values can be successfully round-tripped by using the "G9" [standard numeric format string](../../standard/base-types/standard-numeric-format-strings.md) to preserve the full precision of values. - values have less precision than values. A value that is converted to a seemingly equivalent often does not equal the value because of differences in precision. In the following example, the result of identical division operations is assigned to a value and a value. After the value is cast to a , a comparison of the two values shows that they are unequal. From 50192eb9bf3e4ef21a24c1e0caab0d73491e84b3 Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Fri, 21 Mar 2025 08:33:27 -0700 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Bill Wagner --- .../snippets/System/Boolean/Overview/csharp/binary1.cs | 2 +- .../System/Double/CompareTo/csharp/compareto2.cs | 2 +- .../System/Double/CompareTo/csharp/compareto3.cs | 2 +- .../System/Double/Overview/csharp/comparison2.cs | 2 +- .../System/Double/Overview/csharp/exceptional2.cs | 2 +- .../System/Double/Overview/csharp/precisionlist4.cs | 4 ++-- .../System/Exception/HelpLink/csharp/properties.cs | 10 +++++----- .../System/Exception/Overview/csharp/rethrow1.cs | 4 ++-- .../System/Exception/Overview/csharp/rethrow3.cs | 8 ++++---- .../System/Int64/Overview/csharp/formatting1.cs | 2 +- .../Overview/csharp/Other/Iterating1.cs | 3 ++- .../Overview/csharp/Other/Iterating2.cs | 3 ++- .../System/Single/CompareTo/csharp/compareto2.cs | 3 ++- .../System/Single/CompareTo/csharp/compareto3.cs | 3 ++- .../System/Single/Overview/csharp/comparison2.cs | 3 ++- .../System/Single/Overview/csharp/exceptional2.cs | 3 ++- .../System/Single/Overview/csharp/precisionlist4a.cs | 4 ++-- .../snippets/System/String/Overview/csharp/index11.cs | 6 +++++- .../snippets/System/String/Overview/csharp/index2.cs | 6 +++++- 19 files changed, 43 insertions(+), 29 deletions(-) diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/binary1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/binary1.cs index f0658a31294d0..56975c83825a4 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/binary1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Boolean/Overview/csharp/binary1.cs @@ -14,7 +14,7 @@ public static void Main() Console.WriteLine($"Binary value: {value} ({GetBinaryString(value)})"); // Restore the flag from its binary representation. bool newFlag = BitConverter.ToBoolean(new Byte[] { value }, 0); - Console.WriteLine($"Restored value: {flag}\n"); + Console.WriteLine($"Restored value: {flag}{Environment.NewLine}"); } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Double/CompareTo/csharp/compareto2.cs b/docs/fundamentals/runtime-libraries/snippets/System/Double/CompareTo/csharp/compareto2.cs index 11c020825add0..10a113fd4bcbf 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Double/CompareTo/csharp/compareto2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Double/CompareTo/csharp/compareto2.cs @@ -7,7 +7,7 @@ public static void Main() { double value1 = 6.185; double value2 = value1 * .1 / .1; - Console.WriteLine($"Comparing {value1} and {value2}: {value1.CompareTo(value2)}\n"); + Console.WriteLine($"Comparing {value1} and {value2}: {value1.CompareTo(value2)}{Environment.NewLine}"); Console.WriteLine($"Comparing {value1:R} and {value2:R}: {value1.CompareTo(value2)}"); } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Double/CompareTo/csharp/compareto3.cs b/docs/fundamentals/runtime-libraries/snippets/System/Double/CompareTo/csharp/compareto3.cs index 2865d8ce522b2..7ceb735d23e4a 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Double/CompareTo/csharp/compareto3.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Double/CompareTo/csharp/compareto3.cs @@ -7,7 +7,7 @@ public static void Main() { double value1 = 6.185; object value2 = value1 * .1 / .1; - Console.WriteLine($"Comparing {value1} and {value2}: {value1.CompareTo(value2)}\n"); + Console.WriteLine($"Comparing {value1} and {value2}: {value1.CompareTo(value2)}{Environment.NewLine}"); Console.WriteLine($"Comparing {value1:R} and {value2:R}: {value1.CompareTo(value2)}"); } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/comparison2.cs b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/comparison2.cs index 1c2f14e286a09..e856e479137a7 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/comparison2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/comparison2.cs @@ -9,7 +9,7 @@ public static void Main() value1 = Math.Sqrt(Math.Pow(value1, 2)); double value2 = Math.Pow(value1 * 3.51, 2); value2 = Math.Sqrt(value2) / 3.51; - Console.WriteLine($"{value1} = {value2}: {value1.Equals(value2)}\n"); + Console.WriteLine($"{value1} = {value2}: {value1.Equals(value2)}{Environment.NewLine}"); Console.WriteLine($"{value1:R} = {value2:R}"); } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/exceptional2.cs b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/exceptional2.cs index b4358bdc1ea2f..68e1e4b4cb504 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/exceptional2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/exceptional2.cs @@ -9,7 +9,7 @@ public static void Main() Double value2 = 6.9375e172; Double result = value1 * value2; Console.WriteLine($"PositiveInfinity: {Double.IsPositiveInfinity(result)}"); - Console.WriteLine($"NegativeInfinity: {Double.IsNegativeInfinity(result)}\n"); + Console.WriteLine($"NegativeInfinity: {Double.IsNegativeInfinity(result)}{Environment.NewLine}"); value1 = -value1; result = value1 * value2; diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/precisionlist4.cs b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/precisionlist4.cs index b049845d3115f..d804ac08c6c44 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/precisionlist4.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Double/Overview/csharp/precisionlist4.cs @@ -6,7 +6,7 @@ public class Example11 public static void Main() { // - StreamWriter sw = new(@".\Doubles.dat"); + StreamWriter sw = new(@"./Doubles.dat"); double[] values = [2.2 / 1.01, 1.0 / 3, Math.PI]; for (int ctr = 0; ctr < values.Length; ctr++) { @@ -17,7 +17,7 @@ public static void Main() sw.Close(); double[] restoredValues = new double[values.Length]; - StreamReader sr = new(@".\Doubles.dat"); + StreamReader sr = new(@"./Doubles.dat"); string temp = sr.ReadToEnd(); string[] tempStrings = temp.Split('|'); for (int ctr = 0; ctr < tempStrings.Length; ctr++) diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Exception/HelpLink/csharp/properties.cs b/docs/fundamentals/runtime-libraries/snippets/System/Exception/HelpLink/csharp/properties.cs index 4bd24e14b1517..e30c08c7ee82e 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Exception/HelpLink/csharp/properties.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Exception/HelpLink/csharp/properties.cs @@ -72,11 +72,11 @@ public static void Main() } catch (Exception ex) { - Console.WriteLine($"\nMessage ---\n{ex.Message}"); - Console.WriteLine($"\nHelpLink ---\n{ex.HelpLink}"); - Console.WriteLine($"\nSource ---\n{ex.Source}"); - Console.WriteLine($"\nStackTrace ---\n{ex.StackTrace}"); - Console.WriteLine($"\nTargetSite ---\n{ex.TargetSite}"); + Console.WriteLine($"{Environment.NewLine}Message ---{Environment.NewLine}{ex.Message}"); + Console.WriteLine($"{Environment.NewLine}HelpLink ---{Environment.NewLine}{ex.HelpLink}"); + Console.WriteLine($"{Environment.NewLine}Source ---{Environment.NewLine}{ex.Source}"); + Console.WriteLine($"{Environment.NewLine}StackTrace ---{Environment.NewLine}{ex.StackTrace}"); + Console.WriteLine($"{Environment.NewLine}TargetSite ---{Environment.NewLine}{ex.TargetSite}"); } } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Exception/Overview/csharp/rethrow1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Exception/Overview/csharp/rethrow1.cs index b3f6cab258f9c..f8cde6813b518 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Exception/Overview/csharp/rethrow1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Exception/Overview/csharp/rethrow1.cs @@ -50,8 +50,8 @@ public static void Main() catch (ArgumentNullException e) { Console.WriteLine($"An exception ({e.GetType().Name}) occurred."); - Console.WriteLine($"Message:\n {e.Message}\n"); - Console.WriteLine($"Stack Trace:\n {e.StackTrace}\n"); + Console.WriteLine($"Message:{Environment.NewLine} {e.Message}{Environment.NewLine}"); + Console.WriteLine($"Stack Trace:{Environment.NewLine} {e.StackTrace}{Environment.NewLine}"); } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Exception/Overview/csharp/rethrow3.cs b/docs/fundamentals/runtime-libraries/snippets/System/Exception/Overview/csharp/rethrow3.cs index 7fcc171e46ebe..8b4514f228b3b 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Exception/Overview/csharp/rethrow3.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Exception/Overview/csharp/rethrow3.cs @@ -49,15 +49,15 @@ public static void Main() catch (ArgumentNullException e) { Console.WriteLine($"An exception ({e.GetType().Name}) occurred."); - Console.WriteLine($" Message:\n{e.Message}"); - Console.WriteLine($" Stack Trace:\n {e.StackTrace}"); + Console.WriteLine($" Message:{Environment.NewLine}{e.Message}"); + Console.WriteLine($" Stack Trace:{Environment.NewLine} {e.StackTrace}"); Exception ie = e.InnerException; if (ie != null) { Console.WriteLine(" The Inner Exception:"); Console.WriteLine($" Exception Name: {ie.GetType().Name}"); - Console.WriteLine($" Message: {ie.Message}\n"); - Console.WriteLine($" Stack Trace:\n {ie.StackTrace}\n"); + Console.WriteLine($" Message: {ie.Message}{Environment.NewLine}"); + Console.WriteLine($" Stack Trace:{Environment.NewLine} {ie.StackTrace}{Environment.NewLine}"); } } // The example displays the following output: diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Int64/Overview/csharp/formatting1.cs b/docs/fundamentals/runtime-libraries/snippets/System/Int64/Overview/csharp/formatting1.cs index e5944afa58964..f4dedc1638193 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Int64/Overview/csharp/formatting1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Int64/Overview/csharp/formatting1.cs @@ -43,7 +43,7 @@ private static void CallConvert() Console.WriteLine($"{number} (Base 10):"); Console.WriteLine($" Binary: {Convert.ToString(number, 2)}"); Console.WriteLine($" Octal: {Convert.ToString(number, 8)}"); - Console.WriteLine($" Hex: {Convert.ToString(number, 16)}\n"); + Console.WriteLine($" Hex: {Convert.ToString(number, 16)}{Environment.NewLine}"); } // The example displays the following output: // -146 (Base 10): diff --git a/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Iterating1.cs b/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Iterating1.cs index 9d9908e52319d..d13af74d0ecba 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Iterating1.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Iterating1.cs @@ -11,7 +11,8 @@ public static void Main() { int square = (int)Math.Pow(number, 2); Console.WriteLine($"{number}^{square}"); - Console.WriteLine($"Adding {square} to the collection...\n"); + Console.WriteLine($"Adding {square} to the collection..."); + Console.WriteLine(); numbers.Add(square); } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Iterating2.cs b/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Iterating2.cs index 14116f67fa87d..b68b322ffa440 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Iterating2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/InvalidOperationException/Overview/csharp/Other/Iterating2.cs @@ -13,7 +13,8 @@ public static void Main() { int square = (int)Math.Pow(numbers[ctr], 2); Console.WriteLine($"{numbers[ctr]}^{square}"); - Console.WriteLine($"Adding {square} to the collection...\n"); + Console.WriteLine($"Adding {square} to the collection..."); + Console.WriteLine(); numbers.Add(square); } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Single/CompareTo/csharp/compareto2.cs b/docs/fundamentals/runtime-libraries/snippets/System/Single/CompareTo/csharp/compareto2.cs index 4a0a8cc5b3adb..cdd6191ed7715 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Single/CompareTo/csharp/compareto2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Single/CompareTo/csharp/compareto2.cs @@ -8,7 +8,8 @@ public static void Main() float value1 = 16.5457f; float operand = 3.8899982f; float value2 = value1 * operand / operand; - Console.WriteLine($"Comparing {value1} and {value2}: {value1.CompareTo(value2)}\n"); + Console.WriteLine($"Comparing {value1} and {value2}: {value1.CompareTo(value2)}"); + Console.WriteLine(); Console.WriteLine($"Comparing {value1:R} and {value2:R}: {value1.CompareTo(value2)}"); } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Single/CompareTo/csharp/compareto3.cs b/docs/fundamentals/runtime-libraries/snippets/System/Single/CompareTo/csharp/compareto3.cs index 792ac0a13f5e8..4d5b9b389fcd9 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Single/CompareTo/csharp/compareto3.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Single/CompareTo/csharp/compareto3.cs @@ -8,7 +8,8 @@ public static void Main() float value1 = 16.5457f; float operand = 3.8899982f; object value2 = value1 * operand / operand; - Console.WriteLine($"Comparing {value1} and {value2}: {value1.CompareTo(value2)}\n"); + Console.WriteLine($"Comparing {value1} and {value2}: {value1.CompareTo(value2)}"); + Console.WriteLine(); Console.WriteLine($"Comparing {value1:R} and {value2:R}: {value1.CompareTo(value2)}"); } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/comparison2.cs b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/comparison2.cs index ada6b6e80f4e4..327bc18593301 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/comparison2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/comparison2.cs @@ -9,7 +9,8 @@ public static void Main() value1 = (float)Math.Sqrt((float)Math.Pow(value1, 2)); float value2 = (float)Math.Pow((float)value1 * 3.51f, 2); value2 = ((float)Math.Sqrt(value2)) / 3.51f; - Console.WriteLine($"{value1} = {value2}: {value1.Equals(value2)}\n"); + Console.WriteLine($"{value1} = {value2}: {value1.Equals(value2)}"); + Console.WriteLine(); Console.WriteLine($"{value1:G9} = {value2:G9}"); } } diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/exceptional2.cs b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/exceptional2.cs index 14e080de0432e..6f2e02dfb0df1 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/exceptional2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/exceptional2.cs @@ -9,7 +9,8 @@ public static void Main() float value2 = 6.9375e32f; float result = value1 * value2; Console.WriteLine($"PositiveInfinity: {Single.IsPositiveInfinity(result)}"); - Console.WriteLine($"NegativeInfinity: {Single.IsNegativeInfinity(result)}\n"); + Console.WriteLine($"NegativeInfinity: {Single.IsNegativeInfinity(result)}"); + Console.WriteLine(); value1 = -value1; result = value1 * value2; diff --git a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/precisionlist4a.cs b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/precisionlist4a.cs index f03faccef363a..cb1dcc9201b29 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/precisionlist4a.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/Single/Overview/csharp/precisionlist4a.cs @@ -6,7 +6,7 @@ public class Example10 public static void Main() { // - StreamWriter sw = new(@".\Singles.dat"); + StreamWriter sw = new(@"./Singles.dat"); float[] values = { 3.2f / 1.11f, 1.0f / 3f, (float)Math.PI }; for (int ctr = 0; ctr < values.Length; ctr++) { @@ -17,7 +17,7 @@ public static void Main() sw.Close(); float[] restoredValues = new float[values.Length]; - StreamReader sr = new(@".\Singles.dat"); + StreamReader sr = new(@"./Singles.dat"); string temp = sr.ReadToEnd(); string[] tempStrings = temp.Split('|'); for (int ctr = 0; ctr < tempStrings.Length; ctr++) diff --git a/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/index11.cs b/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/index11.cs index 73a175e96a7df..c3f22746728ab 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/index11.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/index11.cs @@ -13,7 +13,11 @@ public static void Main() if (Char.IsPunctuation(s1[ctr]) | Char.IsWhiteSpace(s1[ctr])) nWords++; } - Console.WriteLine($"The sentence\n {s1}\nhas {nWords} words."); + Console.WriteLine($""" + The sentence + {s1} + has {nWords} words. + """); // The example displays the following output: // The sentence // This string consists of a single short sentence. diff --git a/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/index2.cs b/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/index2.cs index b5174628209f6..565f8a277cb13 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/index2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/index2.cs @@ -13,7 +13,11 @@ public static void Main() if (Char.IsPunctuation(ch) | Char.IsWhiteSpace(ch)) nWords++; } - Console.WriteLine($"The sentence\n {s1}\nhas {nWords} words."); + Console.WriteLine($""" + The sentence + {s1} + has {nWords} words. + """); // The example displays the following output: // The sentence // This string consists of a single short sentence. From fe1f256cfb29775218d9a4965fda048cc187b1fe Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Fri, 21 Mar 2025 08:41:06 -0700 Subject: [PATCH 3/3] Apply suggestions from code review --- .../snippets/System/String/Overview/csharp/index11.cs | 2 +- .../snippets/System/String/Overview/csharp/index2.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/index11.cs b/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/index11.cs index c3f22746728ab..8085b2d874edb 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/index11.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/index11.cs @@ -14,7 +14,7 @@ public static void Main() nWords++; } Console.WriteLine($""" - The sentence + The sentence {s1} has {nWords} words. """); diff --git a/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/index2.cs b/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/index2.cs index 565f8a277cb13..2d7275d20daa2 100644 --- a/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/index2.cs +++ b/docs/fundamentals/runtime-libraries/snippets/System/String/Overview/csharp/index2.cs @@ -14,7 +14,7 @@ public static void Main() nWords++; } Console.WriteLine($""" - The sentence + The sentence {s1} has {nWords} words. """);