Skip to content
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

Convert to interpolated strings (standard, code-analysis, ml) #45440

Merged
merged 2 commits into from
Mar 20, 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 @@
using System;
using System;

namespace ca1003
{
Expand Down Expand Up @@ -35,7 +35,7 @@ public ClassThatHandlesEvent(ClassThatRaisesEvent eventRaiser)

private void HandleEvent(object sender, CustomEventArgs e)
{
Console.WriteLine("Event handled: {0}", e.info);
Console.WriteLine($"Event handled: {e.info}");
}
}

Expand Down Expand Up @@ -84,7 +84,7 @@ public ClassThatHandlesEvent(ClassThatRaisesEvent eventRaiser)

private void HandleEvent(object? sender, CustomEventArgs e)
{
Console.WriteLine("Event handled: {0}", e.info);
Console.WriteLine($"Event handled: {e.info}");
}
}

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

namespace ca1021
{
Expand Down Expand Up @@ -142,8 +142,7 @@ static void UseTheComplicatedClass()
// The call to the library.
disposition[i] = BadRefAndOut.ReplyInformation(
t, out reply[i], ref action[i]);
Console.WriteLine("Reply: {0} Action: {1} return? {2} ",
reply[i], action[i], disposition[i]);
Console.WriteLine($"Reply: {reply[i]} Action: {action[i]} return? {disposition[i]} ");
i++;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.IO;

namespace ca1031
Expand All @@ -18,7 +18,7 @@ public GenericExceptionsCaught(string inFile, string outFile)
}
catch (SystemException)
{
Console.WriteLine("Unable to open {0}.", inFile);
Console.WriteLine($"Unable to open {inFile}.");
}

try
Expand All @@ -27,7 +27,7 @@ public GenericExceptionsCaught(string inFile, string outFile)
}
catch
{
Console.WriteLine("Unable to open {0}.", outFile);
Console.WriteLine($"Unable to open {outFile}.");
}
}
}
Expand All @@ -47,7 +47,7 @@ public GenericExceptionsCaughtFixed(string inFile, string outFile)
// Fix the first violation by catching a specific exception.
catch (FileNotFoundException)
{
Console.WriteLine("Unable to open {0}.", inFile);
Console.WriteLine($"Unable to open {inFile}.");
};

// For functionally equivalent code, also catch
Expand All @@ -62,7 +62,7 @@ public GenericExceptionsCaughtFixed(string inFile, string outFile)
// exception at the end of the catch block.
catch
{
Console.WriteLine("Unable to open {0}.", outFile);
Console.WriteLine($"Unable to open {outFile}.");
throw;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Globalization;

//<snippet1>
Expand Down Expand Up @@ -128,7 +128,7 @@ public static void Main1036(params string[] args)
else
answer = "equal to";

Console.WriteLine("{0} is {1} {2}", r1.Rating, answer, r2.Rating);
Console.WriteLine($"{r1.Rating} is {answer} {r2.Rating}");
}
}
//</snippet2>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;

namespace ca1045
{
Expand Down Expand Up @@ -144,8 +144,7 @@ static void UseTheComplicatedClass()
// The call to the library.
disposition[i] = BadRefAndOut.ReplyInformation(
t, out reply[i], ref action[i]);
Console.WriteLine("Reply: {0} Action: {1} return? {2} ",
reply[i], action[i], disposition[i]);
Console.WriteLine($"Reply: {reply[i]} Action: {action[i]} return? {disposition[i]} ");
i++;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;

namespace ca1046
{
Expand All @@ -14,7 +14,7 @@ public MyReferenceType(int a, int b)

public override string ToString()
{
return String.Format("({0},{1})", a, b);
return String.Format($"({a},{b})");
}
}
//</snippet1>
Expand All @@ -28,10 +28,10 @@ public static void Main1046()
MyReferenceType b = new MyReferenceType(2, 2);
MyReferenceType c = a;

Console.WriteLine("a = new {0} and b = new {1} are equal? {2}", a, b, a.Equals(b) ? "Yes" : "No");
Console.WriteLine("c and a are equal? {0}", c.Equals(a) ? "Yes" : "No");
Console.WriteLine("b and a are == ? {0}", b == a ? "Yes" : "No");
Console.WriteLine("c and a are == ? {0}", c == a ? "Yes" : "No");
Console.WriteLine($"a = new {a} and b = new {b} are equal? {(a.Equals(b) ? "Yes" : "No")}");
Console.WriteLine($"c and a are equal? {(c.Equals(a) ? "Yes" : "No")}");
Console.WriteLine($"b and a are == ? {(b == a ? "Yes" : "No")}");
Console.WriteLine($"c and a are == ? {(c == a ? "Yes" : "No")}");
}
}
//</snippet2>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;

namespace ca2214
{
Expand Down Expand Up @@ -29,7 +29,7 @@ public DerivedType()
}
public override void DoSomething()
{
Console.WriteLine("Derived DoSomething is called - initialized ? {0}", initialized);
Console.WriteLine($"Derived DoSomething is called - initialized ? {initialized}");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <SnippetAddUsings>
// <SnippetAddUsings>
using Microsoft.ML;
using GitHubIssueClassification;
// </SnippetAddUsings>
Expand Down Expand Up @@ -186,5 +186,5 @@ void SaveModelAsFile(MLContext mlContext,DataViewSchema trainingDataViewSchema,
mlContext.Model.Save(model, trainingDataViewSchema, _modelPath);
// </SnippetSaveModel>

Console.WriteLine("The model is saved to {0}", _modelPath);
Console.WriteLine($"The model is saved to {_modelPath}");
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <SnippetAddUsings>
// <SnippetAddUsings>
using Microsoft.ML;
using Microsoft.ML.TimeSeries;
using PhoneCallsAnomalyDetection;
Expand Down Expand Up @@ -38,7 +38,7 @@ int DetectPeriod(MLContext mlContext, IDataView phoneCalls)
// </SnippetDetectSeasonality>

// <SnippetDisplayPeriod>
Console.WriteLine("Period of the series is: {0}.", period);
Console.WriteLine($"Period of the series is: {period}.");
// </SnippetDisplayPeriod>

return period;
Expand Down
4 changes: 2 additions & 2 deletions docs/orleans/host/snippets/ExampleExternalProgram.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

using Orleans.Configuration;
Expand Down Expand Up @@ -84,7 +84,7 @@ class GameObserver : IGameObserver
{
public void UpdateGameScore(string score)
{
Console.WriteLine("New game score: {0}", score);
Console.WriteLine($"New game score: {score}");
}
}
// </gameobserver>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


void Index()
{
// <index>
Expand Down Expand Up @@ -73,7 +73,7 @@ void Examples_Currency()
{
// <example_currency>
int myNumber = 100;
Console.WriteLine("{0:C}", myNumber);
Console.WriteLine($"{myNumber:C}");

// The example displays the following output
// if en-US is the current culture:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <Main>
// <Main>
using System.Globalization;
using System.Text.RegularExpressions;

Expand Down Expand Up @@ -37,7 +37,7 @@
}
catch (FormatException)
{
Console.WriteLine("Unable to convert {0} to a date.", dateString);
Console.WriteLine($"Unable to convert {dateString} to a date.");
}
// The example displays the following output if the current culture is en-US:
// Millisecond component only: 126
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;

public class RegExExamples
{
Expand All @@ -20,16 +20,16 @@ public static void Example1()
switch (m.Groups[2].Value)
{
case "+":
Console.WriteLine("{0} = {1}", m.Value, value1 + value2);
Console.WriteLine($"{m.Value} = {value1 + value2}");
break;
case "-":
Console.WriteLine("{0} = {1}", m.Value, value1 - value2);
Console.WriteLine($"{m.Value} = {value1 - value2}");
break;
case "*":
Console.WriteLine("{0} = {1}", m.Value, value1 * value2);
Console.WriteLine($"{m.Value} = {value1 * value2}");
break;
case "/":
Console.WriteLine("{0} = {1:N2}", m.Value, value1 / value2);
Console.WriteLine($"{m.Value} = {value1 / value2:N2}");
break;
}
}
Expand Down Expand Up @@ -57,7 +57,7 @@ public static void Example2()
foreach (System.Text.RegularExpressions.Match m in
System.Text.RegularExpressions.Regex.Matches(input, pattern))
{
Console.WriteLine("{0}: {1}", ++ctr, m.Groups[1].Value);
Console.WriteLine($"{++ctr}: {m.Groups[1].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;
using System.Text.RegularExpressions;

Expand All @@ -9,8 +9,7 @@ public static void Run()
string input = "needing a reed";
string pattern = @"e{2}\w\b";
foreach (Match match in Regex.Matches(input, pattern))
Console.WriteLine("{0} found at position {1}",
match.Value, match.Index);
Console.WriteLine($"{match.Value} found at position {match.Index}");
}
}
// The example displays the following output:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <Snippet5>
// <Snippet5>
using System;
using System.Diagnostics;
using System.Text.RegularExpressions;
Expand All @@ -15,13 +15,13 @@ public static void Run()
sw = Stopwatch.StartNew();
result = Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase);
sw.Stop();
Console.WriteLine("Match: {0} in {1}", result, sw.Elapsed);
Console.WriteLine($"Match: {result} in {sw.Elapsed}");

string behindPattern = @"^[0-9A-Z][-.\w]*(?<=[0-9A-Z])@";
sw = Stopwatch.StartNew();
result = Regex.IsMatch(input, behindPattern, RegexOptions.IgnoreCase);
sw.Stop();
Console.WriteLine("Match with Lookbehind: {0} in {1}", result, sw.Elapsed);
Console.WriteLine($"Match with Lookbehind: {result} in {sw.Elapsed}");
}
}
// The example displays output similar to the following:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <Snippet6>
// <Snippet6>
using System;
using System.Diagnostics;
using System.Text.RegularExpressions;
Expand All @@ -15,13 +15,13 @@ public static void Run()
sw = Stopwatch.StartNew();
result = Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase);
sw.Stop();
Console.WriteLine("{0} in {1}", result, sw.Elapsed);
Console.WriteLine($"{result} in {sw.Elapsed}");

string aheadPattern = @"^((?=[A-Z])\w+\.)*[A-Z]\w*$";
sw = Stopwatch.StartNew();
result = Regex.IsMatch(input, aheadPattern, RegexOptions.IgnoreCase);
sw.Stop();
Console.WriteLine("{0} in {1}", result, sw.Elapsed);
Console.WriteLine($"{result} in {sw.Elapsed}");
}
}
// The example displays the following output:
Expand Down
14 changes: 7 additions & 7 deletions docs/standard/base-types/snippets/regex/csharp/compare1.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -37,7 +37,7 @@ public async static Task RunIt()
break;
}
sw.Stop();
Console.WriteLine(" {0} matches in {1}", ctr, sw.Elapsed);
Console.WriteLine($" {ctr} matches in {sw.Elapsed}");

// Read first ten sentences with compiled regex.
Console.WriteLine("10 Sentences with Compiled Regex:");
Expand All @@ -54,7 +54,7 @@ public async static Task RunIt()
break;
}
sw.Stop();
Console.WriteLine(" {0} matches in {1}", ctr, sw.Elapsed);
Console.WriteLine($" {ctr} matches in {sw.Elapsed}");

// Read first ten sentences with source-generated regex.
Console.WriteLine("10 Sentences with Source-generated Regex:");
Expand All @@ -70,7 +70,7 @@ public async static Task RunIt()
break;
}
sw.Stop();
Console.WriteLine(" {0} matches in {1}", ctr, sw.Elapsed);
Console.WriteLine($" {ctr} matches in {sw.Elapsed}");

// Read all sentences with interpreted regex.
Console.WriteLine("All Sentences with Interpreted Regex:");
Expand All @@ -85,7 +85,7 @@ public async static Task RunIt()
match = match.NextMatch();
}
sw.Stop();
Console.WriteLine(" {0:N0} matches in {1}", matches, sw.Elapsed);
Console.WriteLine($" {matches:N0} matches in {sw.Elapsed}");

// Read all sentences with compiled regex.
Console.WriteLine("All Sentences with Compiled Regex:");
Expand All @@ -101,7 +101,7 @@ public async static Task RunIt()
match = match.NextMatch();
}
sw.Stop();
Console.WriteLine(" {0:N0} matches in {1}", matches, sw.Elapsed);
Console.WriteLine($" {matches:N0} matches in {sw.Elapsed}");

// Read all sentences with source-generated regex.
Console.WriteLine("All Sentences with Source-generated Regex:");
Expand All @@ -115,7 +115,7 @@ public async static Task RunIt()
match = match.NextMatch();
}
sw.Stop();
Console.WriteLine(" {0:N0} matches in {1}", matches, sw.Elapsed);
Console.WriteLine($" {matches:N0} matches in {sw.Elapsed}");

return;
}
Expand Down
Loading