Skip to content

Use interpolated strings (fundamentals/runtime-libraries chunk one) #45453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <Snippet4>
// <Snippet4>
using System;
using System.Reflection;

Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <Snippet6>
// <Snippet6>
using System;
using System.Reflection;

Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <Snippet1>
// <Snippet1>
using System;

public class Example1
Expand All @@ -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}{Environment.NewLine}");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <Snippet8>
// <Snippet8>
using System;

public class Example3
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <Snippet13>
// <Snippet13>
using System;

public class Example6
Expand All @@ -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}.");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <Snippet2>
// <Snippet2>
using System;

public class Example7
Expand All @@ -13,23 +13,23 @@ 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();
// Parse strings using the Boolean.TryParse method.
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}'");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <Snippet9>
// <Snippet9>
using System;

public class Example8
Expand All @@ -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}'");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <Snippet14>
// <Snippet14>
using System;

public struct BoolStruct
Expand All @@ -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}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <Snippet3>
// <Snippet3>
using System;

public class Example10
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <Snippet4>
// <Snippet4>
using System;

public class Example11
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <Snippet1>
// <Snippet1>
using System;
using System.Globalization;

Expand All @@ -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}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <Snippet2>
// <Snippet2>
using System;
using System.Collections.Generic;
using System.Globalization;
Expand All @@ -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)})");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;

public class Example2
{
Expand All @@ -15,7 +15,7 @@ private static void InstantiateByAssignment()
byte value1 = 64;
byte value2 = 255;
// </Snippet1>
Console.WriteLine("{0} {1}", value1, value2);
Console.WriteLine($"{value1} {value2}");
}

private static void InstantiateByNarrowingConversion()
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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";
Expand All @@ -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
Expand Down
Loading