Skip to content

Commit 24fbb5a

Browse files
authored
Use interpolated strings (other snippets dirs) (#45496)
1 parent d8dbea8 commit 24fbb5a

File tree

35 files changed

+573
-661
lines changed

35 files changed

+573
-661
lines changed

Diff for: samples/snippets/csharp/VS_Snippets_ADO.NET/DP LINQ to DataSet Examples/CS/Program.cs

+13-38
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,7 @@ from order in orders.AsEnumerable()
564564
Console.WriteLine("Orders that were made after 12/1/2002:");
565565
foreach (DataRow order in query)
566566
{
567-
Console.WriteLine("OrderID {0} Order date: {1:d} ",
568-
order.Field<int>("SalesOrderID"), order.Field<DateTime>("OrderDate"));
567+
Console.WriteLine($"OrderID {order.Field<int>("SalesOrderID")} Order date: {order.Field<DateTime>("OrderDate"):d} ");
569568
foreach (DataRow orderDetail in order.GetChildRows("SalesOrderHeaderDetail"))
570569
{
571570
Console.WriteLine($" Product ID: {orderDetail["ProductID"]} Unit Price {orderDetail["UnitPrice"]}");
@@ -665,10 +664,7 @@ static void TakeSimple()
665664
Console.WriteLine("First 5 contacts:");
666665
foreach (DataRow contact in first5Contacts)
667666
{
668-
Console.WriteLine("Title = {0} \t FirstName = {1} \t Lastname = {2}",
669-
contact.Field<string>("Title"),
670-
contact.Field<string>("FirstName"),
671-
contact.Field<string>("Lastname"));
667+
Console.WriteLine($"Title = {contact.Field<string>("Title")} \t FirstName = {contact.Field<string>("FirstName")} \t Lastname = {contact.Field<string>("Lastname")}");
672668
}
673669
//</SnippetTakeSimple>
674670
}
@@ -691,9 +687,7 @@ static void SkipSimple()
691687
Console.WriteLine("All but first 5 contacts:");
692688
foreach (DataRow contact in allButFirst5Contacts)
693689
{
694-
Console.WriteLine("FirstName = {0} \tLastname = {1}",
695-
contact.Field<string>("FirstName"),
696-
contact.Field<string>("Lastname"));
690+
Console.WriteLine($"FirstName = {contact.Field<string>("FirstName")} \tLastname = {contact.Field<string>("Lastname")}");
697691
}
698692
//</SnippetSkipSimple>
699693
}
@@ -891,10 +885,7 @@ orderby product.Field<string>("Name"),
891885

892886
foreach (DataRow product in query)
893887
{
894-
Console.WriteLine("Product ID: {0} Product Name: {1} List Price {2}",
895-
product.Field<int>("ProductID"),
896-
product.Field<string>("Name"),
897-
product.Field<Decimal>("ListPrice"));
888+
Console.WriteLine($"Product ID: {product.Field<int>("ProductID")} Product Name: {product.Field<string>("Name")} List Price {product.Field<Decimal>("ListPrice")}");
898889
}
899890
//</SnippetThenByDescendingSimple>
900891
}
@@ -922,10 +913,7 @@ static void ThenByDescendingComparer_MQ()
922913

923914
foreach (DataRow product in query)
924915
{
925-
Console.WriteLine("Product ID: {0} Product Name: {1} List Price {2}",
926-
product.Field<int>("ProductID"),
927-
product.Field<string>("Name"),
928-
product.Field<Decimal>("ListPrice"));
916+
Console.WriteLine($"Product ID: {product.Field<int>("ProductID")} Product Name: {product.Field<string>("Name")} List Price {product.Field<Decimal>("ListPrice")}");
929917
}
930918
//</SnippetThenByDescendingComparer_MQ>
931919
}
@@ -1066,10 +1054,8 @@ Month into mg
10661054
Console.WriteLine($"\t\t Month= {monthGroup.Month}");
10671055
foreach (var order in monthGroup.Orders)
10681056
{
1069-
Console.WriteLine("\t\t\t OrderID= {0} ",
1070-
order.Field<int>("SalesOrderID"));
1071-
Console.WriteLine("\t\t\t OrderDate= {0} ",
1072-
order.Field<DateTime>("OrderDate"));
1057+
Console.WriteLine($"\t\t\t OrderID= {order.Field<int>("SalesOrderID")} ");
1058+
Console.WriteLine($"\t\t\t OrderDate= {order.Field<DateTime>("OrderDate")} ");
10731059
}
10741060
}
10751061
}
@@ -1420,8 +1406,7 @@ static void FirstCondition_MQ()
14201406
DataRow startsWith = contacts.AsEnumerable().
14211407
First(contact => contact.Field<string>("EmailAddress").StartsWith("caroline"));
14221408

1423-
Console.WriteLine("An email address starting with 'caroline': {0}",
1424-
startsWith.Field<string>("EmailAddress"));
1409+
Console.WriteLine($"An email address starting with 'caroline': {startsWith.Field<string>("EmailAddress")}");
14251410
//</SnippetFirstCondition_MQ>
14261411
}
14271412

@@ -1805,9 +1790,7 @@ group order by order.Field<Int32>("ContactID") into g
18051790
Console.WriteLine($"ContactID: {orderGroup.Category}");
18061791
foreach (var order in orderGroup.smallestTotalDue)
18071792
{
1808-
Console.WriteLine("Minimum TotalDue {0} for SalesOrderID {1}: ",
1809-
order.Field<decimal>("TotalDue"),
1810-
order.Field<Int32>("SalesOrderID"));
1793+
Console.WriteLine($"Minimum TotalDue {order.Field<decimal>("TotalDue")} for SalesOrderID {order.Field<Int32>("SalesOrderID")}: ");
18111794
}
18121795
Console.WriteLine("");
18131796
}
@@ -1893,9 +1876,7 @@ group order by order.Field<Int32>("ContactID") into g
18931876
Console.WriteLine($"ContactID: {orderGroup.Category}");
18941877
foreach (var order in orderGroup.CheapestProducts)
18951878
{
1896-
Console.WriteLine("Average total due for SalesOrderID {1} is: {0}",
1897-
order.Field<decimal>("TotalDue"),
1898-
order.Field<Int32>("SalesOrderID"));
1879+
Console.WriteLine($"Average total due for SalesOrderID {order.Field<Int32>("SalesOrderID")} is: {order.Field<decimal>("TotalDue")}");
18991880
}
19001881
Console.WriteLine("");
19011882
}
@@ -1981,9 +1962,7 @@ group order by order.Field<Int32>("ContactID") into g
19811962
Console.WriteLine($"ContactID: {orderGroup.Category}");
19821963
foreach (var order in orderGroup.CheapestProducts)
19831964
{
1984-
Console.WriteLine("MaxTotalDue {0} for SalesOrderID {1}: ",
1985-
order.Field<decimal>("TotalDue"),
1986-
order.Field<Int32>("SalesOrderID"));
1965+
Console.WriteLine($"MaxTotalDue {order.Field<decimal>("TotalDue")} for SalesOrderID {order.Field<Int32>("SalesOrderID")}: ");
19871966
}
19881967
}
19891968
//</SnippetMaxElements_MQ>
@@ -2505,12 +2484,8 @@ group product by product.Field<string>("Size") into g
25052484
Console.WriteLine($"{productGroup.Category}:");
25062485
foreach (var product in productGroup.Products)
25072486
{
2508-
Console.WriteLine(" Name: {0} Color: {1}",
2509-
product.Field<string>("Name"),
2510-
product.Field<string>("Color"));
2511-
Console.WriteLine(" List price: {0} Size: {1}",
2512-
product.Field<Decimal>("ListPrice"),
2513-
product.Field<string>("Size"));
2487+
Console.WriteLine($" Name: {product.Field<string>("Name")} Color: {product.Field<string>("Color")}");
2488+
Console.WriteLine($" List price: {product.Field<Decimal>("ListPrice")} Size: {product.Field<string>("Size")}");
25142489
}
25152490
}
25162491
}

Diff for: samples/snippets/csharp/VS_Snippets_ADO.NET/DataWorks DataTable.Events/CS/source.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Data;
33

44
static class Class1
@@ -85,8 +85,7 @@ static void Column_Changing(object sender, DataColumnChangeEventArgs e) =>
8585

8686
static void Table_NewRow(object sender,
8787
DataTableNewRowEventArgs e) =>
88-
Console.WriteLine("Table_NewRow Event: RowState={0}",
89-
e.Row.RowState.ToString());
88+
Console.WriteLine($"Table_NewRow Event: RowState={e.Row.RowState.ToString()}");
9089

9190
static void Table_Cleared(object sender, DataTableClearEventArgs e) =>
9291
Console.WriteLine("Table_Cleared Event: TableName={0}; Rows={1}",

Diff for: samples/snippets/csharp/VS_Snippets_ADO.NET/DataWorks DbProviderFactories.DbCommand/CS/source.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static void ExecuteDbCommand(DbConnection connection)
3232
// Handle data errors.
3333
catch (DbException exDb)
3434
{
35-
Console.WriteLine("DbException.GetType: {0}", exDb.GetType());
35+
Console.WriteLine($"DbException.GetType: {exDb.GetType()}");
3636
Console.WriteLine($"DbException.Source: {exDb.Source}");
3737
Console.WriteLine($"DbException.ErrorCode: {exDb.ErrorCode}");
3838
Console.WriteLine($"DbException.Message: {exDb.Message}");

Diff for: samples/snippets/csharp/VS_Snippets_ADO.NET/DataWorks SqlClient.HasRows/CS/source.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Data.SqlClient;
33

44
namespace NextResultCS;
@@ -28,8 +28,7 @@ static void HasRows(SqlConnection connection)
2828
{
2929
while (reader.Read())
3030
{
31-
Console.WriteLine("{0}\t{1}", reader.GetInt32(0),
32-
reader.GetString(1));
31+
Console.WriteLine($"{reader.GetInt32(0)}\t{reader.GetString(1)}");
3332
}
3433
}
3534
else

Diff for: samples/snippets/csharp/VS_Snippets_ADO.NET/DataWorks SqlClient.NextResult/CS/source.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Data.SqlClient;
33

44
namespace NextResultCS;
@@ -27,13 +27,11 @@ static void RetrieveMultipleResults(SqlConnection connection)
2727

2828
while (reader.HasRows)
2929
{
30-
Console.WriteLine("\t{0}\t{1}", reader.GetName(0),
31-
reader.GetName(1));
30+
Console.WriteLine($"\t{reader.GetName(0)}\t{reader.GetName(1)}");
3231

3332
while (reader.Read())
3433
{
35-
Console.WriteLine("\t{0}\t{1}", reader.GetInt32(0),
36-
reader.GetString(1));
34+
Console.WriteLine($"\t{reader.GetInt32(0)}\t{reader.GetString(1)}");
3735
}
3836
reader.NextResult();
3937
}

Diff for: samples/snippets/csharp/VS_Snippets_ADO.NET/DataWorks SqlClient.SprocIdentityReturn/CS/source.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static void ReturnIdentity(string connectionString)
5353
// Retrieve the ReturnValue.
5454
var rowCount = (int)adapter.InsertCommand.Parameters["@RowCount"].Value;
5555

56-
Console.WriteLine("ReturnValue: {0}", rowCount.ToString());
56+
Console.WriteLine($"ReturnValue: {rowCount.ToString()}");
5757
Console.WriteLine("All Rows:");
5858
foreach (DataRow row in categories.Rows)
5959
{

Diff for: samples/snippets/csharp/VS_Snippets_ADO.NET/DataWorks SqlTypes.CompareNulls/CS/source.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Data.SqlTypes;
33

44
namespace SqlNullsCS;
@@ -19,12 +19,12 @@ static void CompareNulls()
1919

2020
// Compare nulls using static/shared SqlString.Equals.
2121
Console.WriteLine("SqlString.Equals shared/static method:");
22-
Console.WriteLine(" Two nulls={0}", SqlStringEquals(a, b));
22+
Console.WriteLine($" Two nulls={SqlStringEquals(a, b)}");
2323

2424
// Compare nulls using instance method String.Equals.
2525
Console.WriteLine();
2626
Console.WriteLine("String.Equals instance method:");
27-
Console.WriteLine(" Two nulls={0}", StringEquals(a, b));
27+
Console.WriteLine($" Two nulls={StringEquals(a, b)}");
2828

2929
// Make them empty strings.
3030
a = "";
@@ -34,11 +34,11 @@ static void CompareNulls()
3434
// the instance Equals methods evaluate to true.
3535
Console.WriteLine();
3636
Console.WriteLine("SqlString.Equals shared/static method:");
37-
Console.WriteLine(" Two empty strings={0}", SqlStringEquals(a, b));
37+
Console.WriteLine($" Two empty strings={SqlStringEquals(a, b)}");
3838

3939
Console.WriteLine();
4040
Console.WriteLine("String.Equals instance method:");
41-
Console.WriteLine(" Two empty strings={0}", StringEquals(a, b));
41+
Console.WriteLine($" Two empty strings={StringEquals(a, b)}");
4242
}
4343

4444
static string SqlStringEquals(SqlString string1, SqlString string2)

Diff for: samples/snippets/csharp/VS_Snippets_Data/DP L2E Examples/CS/Program.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,7 @@ static void GroupByCount_MQ()
10991099
foreach (IGrouping<int, SalesOrderHeader> group in query)
11001100
{
11011101
Console.WriteLine($"Customer ID: {group.Key}");
1102-
Console.WriteLine("Order count: {0}", group.Count());
1102+
Console.WriteLine($"Order count: {group.Count()}");
11031103

11041104
foreach (SalesOrderHeader sale in group)
11051105
{
@@ -1344,9 +1344,9 @@ static void DistinctRows()
13441344
// = doubledContacts.AsEnumerable().Distinct();
13451345
// = Enumerable.Distinct(doubledContacts.AsEnumerable());
13461346

1347-
Console.WriteLine("Initial number of contacts: {0}", contact.Count());
1347+
Console.WriteLine($"Initial number of contacts: {contact.Count()}");
13481348
Console.WriteLine("First 100, then doubled: {0}", doubledContacts.Count());
1349-
Console.WriteLine("Number of unique contacts: {0}", uniqueContact.Count());
1349+
Console.WriteLine($"Number of unique contacts: {uniqueContact.Count()}");
13501350
}
13511351
//</SnippetDistinctRows>
13521352
}

Diff for: samples/snippets/csharp/VS_Snippets_Data/DP ObjectServices Concepts/CS/Source.cs

+4-8
Original file line numberDiff line numberDiff line change
@@ -1377,14 +1377,12 @@ static public void ObjectContextAddDeleteSave_ObjectStateEntryState()
13771377
ObjectStateEntry stateEntry =
13781378
context.ObjectStateManager
13791379
.GetObjectStateEntry(order);
1380-
Console.WriteLine("State before SaveChanges() is called: {0}",
1381-
stateEntry.State.ToString());
1380+
Console.WriteLine($"State before SaveChanges() is called: {stateEntry.State.ToString()}");
13821381

13831382
// Save changes in the object context to the database.
13841383
int changes = context.SaveChanges();
13851384

1386-
Console.WriteLine("State after SaveChanges() is called: {0}",
1387-
stateEntry.State.ToString());
1385+
Console.WriteLine($"State after SaveChanges() is called: {stateEntry.State.ToString()}");
13881386

13891387
Console.WriteLine(changes.ToString() + " changes saved!");
13901388
Console.WriteLine("Updated item for order ID: "
@@ -1813,8 +1811,7 @@ from order in customer.SalesOrderHeaders
18131811
select order;
18141812

18151813
// Write the number of orders placed online.
1816-
Console.WriteLine("{0} orders placed online have been shipped.",
1817-
shippedOrders.Count());
1814+
Console.WriteLine($"{shippedOrders.Count()} orders placed online have been shipped.");
18181815
}
18191816
//</snippetQueryEntityCollection>
18201817
}
@@ -1843,8 +1840,7 @@ from orders in customer.SalesOrderHeaders.CreateSourceQuery()
18431840
select orders;
18441841

18451842
// Write the number of orders placed online.
1846-
Console.WriteLine("{0} orders placed online have been shipped.",
1847-
shippedOrders.Count());
1843+
Console.WriteLine($"{shippedOrders.Count()} orders placed online have been shipped.");
18481844

18491845
// You do not have to call the Load method to load the orders for the customer,
18501846
// because lazy loading is set to true

Diff for: samples/snippets/csharp/VS_Snippets_Data/XPathNavigatorMethods/CS/xpathnavigatormethods.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ static void XPathNavigatorMethods_Evaluate1()
242242
XPathNavigator navigator = document.CreateNavigator();
243243

244244
Double total = (double)navigator.Evaluate("sum(descendant::book/price)");
245-
Console.WriteLine("Total price for all books: {0}", total.ToString());
245+
Console.WriteLine($"Total price for all books: {total.ToString()}");
246246
//</snippet10>
247247
}
248248

@@ -257,7 +257,7 @@ static void XPathNavigatorMethods_Evaluate2()
257257
XPathExpression query = navigator.Compile("sum(descendant::book/price)");
258258

259259
Double total = (double)navigator.Evaluate(query);
260-
Console.WriteLine("Total price for all books: {0}", total.ToString());
260+
Console.WriteLine($"Total price for all books: {total.ToString()}");
261261
//</snippet11>
262262
}
263263

@@ -273,7 +273,7 @@ static void XPathNavigatorMethods_Evaluate3()
273273
manager.AddNamespace("bk", "http://www.contoso.com/books");
274274

275275
Double total = (double)navigator.Evaluate("sum(descendant::bk:book/bk:price)", manager);
276-
Console.WriteLine("Total price for all books: {0}", total.ToString());
276+
Console.WriteLine($"Total price for all books: {total.ToString()}");
277277
//</snippet12>
278278
}
279279

@@ -289,7 +289,7 @@ static void XPathNavigatorMethods_Evaluate4()
289289
XPathExpression query = nodes.Current.Compile("sum(descendant::price)");
290290

291291
Double total = (double)navigator.Evaluate(query, nodes);
292-
Console.WriteLine("Total price for all books: {0}", total.ToString());
292+
Console.WriteLine($"Total price for all books: {total.ToString()}");
293293
//</snippet13>
294294
}
295295
#endregion
@@ -598,12 +598,12 @@ static void XPathNavigatorMethods_MoveToFollowing4()
598598

599599
navigator.MoveToFollowing("price", "http://www.contoso.com/books", boundary);
600600

601-
Console.WriteLine("Position (after boundary): {0}", navigator.Name);
601+
Console.WriteLine($"Position (after boundary): {navigator.Name}");
602602
Console.WriteLine(navigator.OuterXml);
603603

604604
navigator.MoveToFollowing("title", "http://www.contoso.com/books", boundary);
605605

606-
Console.WriteLine("Position (before boundary): {0}", navigator.Name);
606+
Console.WriteLine($"Position (before boundary): {navigator.Name}");
607607
Console.WriteLine(navigator.OuterXml);
608608
//</snippet28>
609609
}

Diff for: samples/snippets/csharp/VS_Snippets_Misc/cds/cs/cds2.cs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//<snippet01>
1+
//<snippet01>
22
namespace ProducerConsumer
33
{
44
using System;
@@ -69,12 +69,10 @@ static void RunConsumer(Object stateInfo)
6969
// underlying collection.
7070
foreach (long item in collection.GetConsumingEnumerable())
7171
{
72-
Console.WriteLine("Consuming tick value {0} : item# {1} ",
73-
item.ToString("D18"), subtractions++);
72+
Console.WriteLine($"Consuming tick value {item.ToString("D18")} : item# {subtractions++} ");
7473
}
7574

76-
Console.WriteLine("Total added: {0} Total consumed: {1} Current count: {2} ",
77-
additions, subtractions, collection.Count());
75+
Console.WriteLine($"Total added: {additions} Total consumed: {subtractions} Current count: {collection.Count()} ");
7876
sw.Stop();
7977

8078
Console.WriteLine("Press any key to exit");
@@ -92,7 +90,7 @@ static void RunConsumer2(Object stateInfo)
9290
bool b = collection.TryTake(out ticks, 30);
9391
if (b == true)
9492
{
95-
Console.WriteLine("Consuming {0} : {1} ", ticks.ToString("D18"), subtractions++);
93+
Console.WriteLine($"Consuming {ticks.ToString("D18")} : {subtractions++} ");
9694
}
9795
else
9896
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Library</OutputType>
4+
<TargetFrameworks>net8.0</TargetFrameworks>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.0.1" />
8+
</ItemGroup>
9+
</Project>

0 commit comments

Comments
 (0)