Skip to content

Commit 80a743b

Browse files
authored
Use interpolated strings (#45493)
1 parent 73e4572 commit 80a743b

File tree

13 files changed

+53
-75
lines changed
  • docs
    • csharp
    • standard
      • base-types/snippets
      • datetime/snippets/instantiating-a-datetimeoffset-object/csharp
      • io/snippets/how-to-use-named-pipes-for-network-interprocess-communication/csharp/NamedPipeServerStream_ImpersonationSample

13 files changed

+53
-75
lines changed

docs/csharp/language-reference/builtin-types/snippets/shared/Arrays.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class TestArraysClass
1+
class TestArraysClass
22
{
33
public static void DeclareArrays()
44
{
@@ -105,7 +105,7 @@ static void Print2DArray(int[,] arr)
105105
{
106106
for (int j = 0; j < arr.GetLength(1); j++)
107107
{
108-
System.Console.WriteLine("Element({0},{1})={2}", i, j, arr[i, j]);
108+
System.Console.WriteLine($"Element({i},{j})={arr[i,j]}");
109109
}
110110
}
111111
}
@@ -166,10 +166,10 @@ public static void ForMultiDimension()
166166
}
167167
System.Console.WriteLine();
168168
}
169-
// Output (including blank lines):
169+
// Output (including blank lines):
170170
// 1 2 3
171171
// 4 5 6
172-
//
172+
//
173173
// 7 8 9
174174
// 10 11 12
175175
//
@@ -185,7 +185,7 @@ public static void JaggedArrayDeclaration()
185185
jaggedArray[1] = [0, 2, 4, 6];
186186
jaggedArray[2] = [11, 22];
187187

188-
int[][] jaggedArray2 =
188+
int[][] jaggedArray2 =
189189
[
190190
[1, 3, 5, 7, 9],
191191
[0, 2, 4, 6],

docs/csharp/linq/snippets/HowToFilesAndDirectories/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using HowToFilesAndDirectories;
1+
using HowToFilesAndDirectories;
22

33
Console.WriteLine("Find files by extension:");
44
FindFilesByExtension();
@@ -324,7 +324,7 @@ where Convert.ToInt32(nameFields[2]) == Convert.ToInt32(scoreFields[0])
324324
{
325325
Console.WriteLine(item);
326326
}
327-
Console.WriteLine("{0} total names in list", scoreQuery.Count());
327+
Console.WriteLine($"{scoreQuery.Count()} total names in list");
328328
/* Output:
329329
Merge two spreadsheets:
330330
Omelchenko, 97, 92, 81, 60

docs/csharp/programming-guide/classes-and-structs/snippets/static-constructors/Program.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ static Bus()
3131

3232
// The following statement produces the first line of output,
3333
// and the line occurs only once.
34-
Console.WriteLine("Static constructor sets global start time to {0}",
35-
globalStartTime.ToLongTimeString());
34+
Console.WriteLine($"Static constructor sets global start time to {globalStartTime.ToLongTimeString()}");
3635
}
3736

3837
// Instance constructor.
@@ -49,10 +48,7 @@ public void Drive()
4948

5049
// For demonstration purposes we treat milliseconds as minutes to simulate
5150
// actual bus times. Do not do this in your actual bus schedule program!
52-
Console.WriteLine("{0} is starting its route {1:N2} minutes after global start time {2}.",
53-
this.RouteNumber,
54-
elapsedTime.Milliseconds,
55-
globalStartTime.ToShortTimeString());
51+
Console.WriteLine($"{this.RouteNumber} is starting its route {elapsedTime.Milliseconds:N2} minutes after global start time {globalStartTime.ToShortTimeString()}.");
5652
}
5753
}
5854

docs/csharp/programming-guide/statements-expressions-operators/snippets/how-to-define-value-equality-for-a-type/ValueEqualityClass/Program.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,24 +137,24 @@ static void Main(string[] args)
137137
ThreeDPoint pointC = null;
138138
int i = 5;
139139

140-
Console.WriteLine("pointA.Equals(pointB) = {0}", pointA.Equals(pointB));
140+
Console.WriteLine($"pointA.Equals(pointB) = {pointA.Equals(pointB)}");
141141
Console.WriteLine($"pointA == pointB = {pointA == pointB}");
142-
Console.WriteLine("null comparison = {0}", pointA.Equals(pointC));
143-
Console.WriteLine("Compare to some other type = {0}", pointA.Equals(i));
142+
Console.WriteLine($"null comparison = {pointA.Equals(pointC)}");
143+
Console.WriteLine($"Compare to some other type = {pointA.Equals(i)}");
144144

145145
TwoDPoint pointD = null;
146146
TwoDPoint pointE = null;
147147

148148
Console.WriteLine($"Two null TwoDPoints are equal: {pointD == pointE}");
149149

150150
pointE = new TwoDPoint(3, 4);
151-
Console.WriteLine("(pointE == pointA) = {0}", pointE == pointA);
152-
Console.WriteLine("(pointA == pointE) = {0}", pointA == pointE);
153-
Console.WriteLine("(pointA != pointE) = {0}", pointA != pointE);
151+
Console.WriteLine($"(pointE == pointA) = {pointE == pointA}");
152+
Console.WriteLine($"(pointA == pointE) = {pointA == pointE}");
153+
Console.WriteLine($"(pointA != pointE) = {pointA != pointE}");
154154

155155
System.Collections.ArrayList list = new System.Collections.ArrayList();
156156
list.Add(new ThreeDPoint(3, 4, 5));
157-
Console.WriteLine("pointE.Equals(list[0]): {0}", pointE.Equals(list[0]));
157+
Console.WriteLine($"pointE.Equals(list[0]): {pointE.Equals(list[0])}");
158158

159159
// Keep the console window open in debug mode.
160160
Console.WriteLine("Press any key to exit.");

docs/csharp/programming-guide/statements-expressions-operators/snippets/how-to-define-value-equality-for-a-type/ValueEqualityStruct/Program.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,44 +36,44 @@ static void Main(string[] args)
3636
int i = 5;
3737

3838
// True:
39-
Console.WriteLine("pointA.Equals(pointB) = {0}", pointA.Equals(pointB));
39+
Console.WriteLine($"pointA.Equals(pointB) = {pointA.Equals(pointB)}");
4040
// True:
4141
Console.WriteLine($"pointA == pointB = {pointA == pointB}");
4242
// True:
43-
Console.WriteLine("object.Equals(pointA, pointB) = {0}", object.Equals(pointA, pointB));
43+
Console.WriteLine($"object.Equals(pointA, pointB) = {object.Equals(pointA, pointB)}");
4444
// False:
45-
Console.WriteLine("pointA.Equals(null) = {0}", pointA.Equals(null));
45+
Console.WriteLine($"pointA.Equals(null) = {pointA.Equals(null)}");
4646
// False:
47-
Console.WriteLine("(pointA == null) = {0}", pointA == null);
47+
Console.WriteLine($"(pointA == null) = {pointA == null}");
4848
// True:
49-
Console.WriteLine("(pointA != null) = {0}", pointA != null);
49+
Console.WriteLine($"(pointA != null) = {pointA != null}");
5050
// False:
51-
Console.WriteLine("pointA.Equals(i) = {0}", pointA.Equals(i));
51+
Console.WriteLine($"pointA.Equals(i) = {pointA.Equals(i)}");
5252
// CS0019:
5353
// Console.WriteLine($"pointA == i = {pointA == i}");
5454

5555
// Compare unboxed to boxed.
5656
System.Collections.ArrayList list = new System.Collections.ArrayList();
5757
list.Add(new TwoDPoint(3, 4));
5858
// True:
59-
Console.WriteLine("pointA.Equals(list[0]): {0}", pointA.Equals(list[0]));
59+
Console.WriteLine($"pointA.Equals(list[0]): {pointA.Equals(list[0])}");
6060

6161
// Compare nullable to nullable and to non-nullable.
6262
TwoDPoint? pointC = null;
6363
TwoDPoint? pointD = null;
6464
// False:
65-
Console.WriteLine("pointA == (pointC = null) = {0}", pointA == pointC);
65+
Console.WriteLine($"pointA == (pointC = null) = {pointA == pointC}");
6666
// True:
6767
Console.WriteLine($"pointC == pointD = {pointC == pointD}");
6868

6969
TwoDPoint temp = new TwoDPoint(3, 4);
7070
pointC = temp;
7171
// True:
72-
Console.WriteLine("pointA == (pointC = 3,4) = {0}", pointA == pointC);
72+
Console.WriteLine($"pointA == (pointC = 3,4) = {pointA == pointC}");
7373

7474
pointD = temp;
7575
// True:
76-
Console.WriteLine("pointD == (pointC = 3,4) = {0}", pointD == pointC);
76+
Console.WriteLine($"pointD == (pointC = 3,4) = {pointD == pointC}");
7777

7878
Console.WriteLine("Press any key to exit.");
7979
Console.ReadKey();

docs/csharp/programming-guide/statements-expressions-operators/snippets/how-to-test-for-reference-equality-identity/Program.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,12 @@ static void Main()
3131
TestClass tcA = new TestClass() { Num = 1, Name = "New TestClass" };
3232
TestClass tcB = new TestClass() { Num = 1, Name = "New TestClass" };
3333

34-
Console.WriteLine("ReferenceEquals(tcA, tcB) = {0}",
35-
Object.ReferenceEquals(tcA, tcB)); // false
34+
Console.WriteLine($"ReferenceEquals(tcA, tcB) = {Object.ReferenceEquals(tcA, tcB)}"); // false
3635

3736
// After assignment, tcB and tcA refer to the same object.
3837
// They now have reference equality.
3938
tcB = tcA;
40-
Console.WriteLine("After assignment: ReferenceEquals(tcA, tcB) = {0}",
41-
Object.ReferenceEquals(tcA, tcB)); // true
39+
Console.WriteLine($"After assignment: ReferenceEquals(tcA, tcB) = {Object.ReferenceEquals(tcA, tcB)}"); // true
4240

4341
// Changes made to tcA are reflected in tcB. Therefore, objects
4442
// that have reference equality also have value equality.
@@ -55,8 +53,7 @@ static void Main()
5553
// Value types are boxed into separate objects when passed to ReferenceEquals.
5654
// Even if the same variable is used twice, boxing ensures they are different instances.
5755
TestStruct tsD = tsC;
58-
Console.WriteLine("After assignment: ReferenceEquals(tsC, tsD) = {0}",
59-
Object.ReferenceEquals(tsC, tsD)); // false
56+
Console.WriteLine($"After assignment: ReferenceEquals(tsC, tsD) = {Object.ReferenceEquals(tsC, tsD)}"); // false
6057
#endregion
6158

6259
#region stringRefEquality
@@ -65,8 +62,7 @@ static void Main()
6562
// the two strings have reference equality although no assignment takes place.
6663
string strA = "Hello world!";
6764
string strB = "Hello world!";
68-
Console.WriteLine("ReferenceEquals(strA, strB) = {0}",
69-
Object.ReferenceEquals(strA, strB)); // true
65+
Console.WriteLine($"ReferenceEquals(strA, strB) = {Object.ReferenceEquals(strA, strB)}"); // true
7066

7167
// After a new string is assigned to strA, strA and strB
7268
// are no longer interned and no longer have reference equality.
@@ -80,8 +76,7 @@ static void Main()
8076
StringBuilder sb = new StringBuilder("Hello world!");
8177
string stringC = sb.ToString();
8278
// False:
83-
Console.WriteLine("ReferenceEquals(stringC, strB) = {0}",
84-
Object.ReferenceEquals(stringC, strB));
79+
Console.WriteLine($"ReferenceEquals(stringC, strB) = {Object.ReferenceEquals(stringC, strB)}");
8580

8681
// The string class overloads the == operator to perform an equality comparison.
8782
Console.WriteLine($"stringC == strB = {stringC == strB}"); // true

docs/csharp/snippets/methods/inherited1.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// <Snippet104>
1+
// <Snippet104>
22
public class Person
33
{
44
public string FirstName = default!;
@@ -10,7 +10,7 @@ public static void Main()
1010
{
1111
Person p1 = new() { FirstName = "John" };
1212
Person p2 = new() { FirstName = "John" };
13-
Console.WriteLine("p1 = p2: {0}", p1.Equals(p2));
13+
Console.WriteLine($"p1 = p2: {p1.Equals(p2)}");
1414
}
1515
}
1616
// The example displays the following output:

docs/csharp/snippets/methods/overridden1.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// <Snippet105>
1+
// <Snippet105>
22
namespace methods;
33

44
public class Person
@@ -18,7 +18,7 @@ public static void Main()
1818
{
1919
Person p1 = new() { FirstName = "John" };
2020
Person p2 = new() { FirstName = "John" };
21-
Console.WriteLine("p1 = p2: {0}", p1.Equals(p2));
21+
Console.WriteLine($"p1 = p2: {p1.Equals(p2)}");
2222
}
2323
}
2424
// The example displays the following output:

docs/standard/base-types/snippets/best-practices-strings/csharp/comparison3/Program.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
string separated = "\u0061\u030a";
1+
string separated = "\u0061\u030a";
22
string combined = "\u00e5";
33

4-
Console.WriteLine("Equal sort weight of {0} and {1} using InvariantCulture: {2}",
5-
separated, combined,
6-
string.Compare(separated, combined, StringComparison.InvariantCulture) == 0);
4+
Console.WriteLine($"Equal sort weight of {separated} and {combined} using InvariantCulture: {string.Compare(separated, combined, StringComparison.InvariantCulture) == 0}");
75

8-
Console.WriteLine("Equal sort weight of {0} and {1} using Ordinal: {2}",
9-
separated, combined,
10-
string.Compare(separated, combined, StringComparison.Ordinal) == 0);
6+
Console.WriteLine($"Equal sort weight of {separated} and {combined} using Ordinal: {string.Compare(separated, combined, StringComparison.Ordinal) == 0}");
117

128
// The example displays the following output:
139
// Equal sort weight of a° and å using InvariantCulture: True

docs/standard/base-types/snippets/how-to-display-milliseconds-in-date-and-time-values/csharp/Program.cs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,12 @@
1010
DateTimeOffset dateOffsetValue = DateTimeOffset.Parse(dateString);
1111

1212
// Display Millisecond component alone.
13-
Console.WriteLine("Millisecond component only: {0}",
14-
dateValue.ToString("fff"));
15-
Console.WriteLine("Millisecond component only: {0}",
16-
dateOffsetValue.ToString("fff"));
13+
Console.WriteLine($"Millisecond component only: {dateValue.ToString("fff")}");
14+
Console.WriteLine($"Millisecond component only: {dateOffsetValue.ToString("fff")}");
1715

1816
// Display Millisecond component with full date and time.
19-
Console.WriteLine("Date and Time with Milliseconds: {0}",
20-
dateValue.ToString("MM/dd/yyyy hh:mm:ss.fff tt"));
21-
Console.WriteLine("Date and Time with Milliseconds: {0}",
22-
dateOffsetValue.ToString("MM/dd/yyyy hh:mm:ss.fff tt"));
17+
Console.WriteLine($"Date and Time with Milliseconds: {dateValue.ToString("MM/dd/yyyy hh:mm:ss.fff tt")}");
18+
Console.WriteLine($"Date and Time with Milliseconds: {dateOffsetValue.ToString("MM/dd/yyyy hh:mm:ss.fff tt")}");
2319

2420
string fullPattern = DateTimeFormatInfo.CurrentInfo.FullDateTimePattern;
2521

@@ -30,10 +26,8 @@
3026
fullPattern = Regex.Replace(fullPattern, "(:ss|:s)", $"$1{millisecondFormat}");
3127

3228
// Display Millisecond component with modified full date and time pattern.
33-
Console.WriteLine("Modified full date time pattern: {0}",
34-
dateValue.ToString(fullPattern));
35-
Console.WriteLine("Modified full date time pattern: {0}",
36-
dateOffsetValue.ToString(fullPattern));
29+
Console.WriteLine($"Modified full date time pattern: {dateValue.ToString(fullPattern)}");
30+
Console.WriteLine($"Modified full date time pattern: {dateOffsetValue.ToString(fullPattern)}");
3731
}
3832
catch (FormatException)
3933
{
@@ -66,9 +60,9 @@ public static void Show2()
6660
{
6761
// <Fraction>
6862
DateTime dateValue = new DateTime(2008, 7, 16, 8, 32, 45, 180);
69-
Console.WriteLine("{0} seconds", dateValue.ToString("s.f"));
70-
Console.WriteLine("{0} seconds", dateValue.ToString("s.ff"));
71-
Console.WriteLine("{0} seconds", dateValue.ToString("s.ffff"));
63+
Console.WriteLine($"{dateValue.ToString("s.f")} seconds");
64+
Console.WriteLine($"{dateValue.ToString("s.ff")} seconds");
65+
Console.WriteLine($"{dateValue.ToString("s.ffff")} seconds");
7266
// The example displays the following output to the console:
7367
// 45.1 seconds
7468
// 45.18 seconds

0 commit comments

Comments
 (0)