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 (csharp dir) #45441

Merged
merged 3 commits into from
Mar 24, 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.Linq.Expressions;
using System.Linq.Expressions;
public static class InterpretExpressions
{
public static void ParseExpression()
Expand All @@ -16,8 +16,7 @@ public static void ParseExpression()
ParameterExpression left = (ParameterExpression)operation.Left;
ConstantExpression right = (ConstantExpression)operation.Right;

Console.WriteLine("Decomposed expression: {0} => {1} {2} {3}",
param.Name, left.Name, operation.NodeType, right.Value);
Console.WriteLine($"Decomposed expression: {param.Name} => {left.Name} {operation.NodeType} {right.Value}");

// This code produces the following output:

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// See https://aka.ms/new-console-template for more information
// See https://aka.ms/new-console-template for more information


using System.Reflection;
Expand Down Expand Up @@ -35,10 +35,10 @@ from method in type.GetMethods()

foreach (var groupOfMethods in pubTypesQuery)
{
Console.WriteLine("Type: {0}", groupOfMethods.Key);
Console.WriteLine($"Type: {groupOfMethods.Key}");
foreach (var method in groupOfMethods)
{
Console.WriteLine(" {0}", method);
Console.WriteLine($" {method}");
}
}
// </QueryReflection>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Text.RegularExpressions;
using System.Text.RegularExpressions;
using System.Windows;
using Microsoft.AspNetCore.Mvc;

Expand Down Expand Up @@ -111,7 +111,7 @@ private static void Download(string URL)

private static void DoSomethingWithData(object stringData)
{
Console.WriteLine("Displaying data: ", stringData);
Console.WriteLine($"Displaying data: {stringData}");
}

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

namespace Coding_Conventions_Examples
{
Expand Down Expand Up @@ -32,7 +32,7 @@ public static void DelegateExamples()

public static void DelMethod(string str)
{
Console.WriteLine("DelMethod argument: {0}", str);
Console.WriteLine($"DelMethod argument: {str}");
}
//</snippet14b>

Expand Down Expand Up @@ -174,7 +174,7 @@ static void Main(string[] args)

if ((divisor != 0) && (dividend / divisor) is var result)
{
Console.WriteLine("Quotient: {0}", result);
Console.WriteLine($"Quotient: {result}");
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;

namespace Exceptions
{
Expand All @@ -22,7 +22,7 @@ public static void Main()
try
{
result = SafeDivision(a, b);
Console.WriteLine("{0} divided by {1} = {2}", a, b, result);
Console.WriteLine($"{a} divided by {b} = {result}");
}
catch (DivideByZeroException)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;

namespace Example
{
Expand All @@ -20,7 +20,7 @@ static void Main()
// Create struct instance and initialize by using "new".
// Memory is allocated on thread stack.
Person p1 = new Person("Alex", 9);
Console.WriteLine("p1 Name = {0} Age = {1}", p1.Name, p1.Age);
Console.WriteLine($"p1 Name = {p1.Name} Age = {p1.Age}");

// Create new struct object. Note that struct can be initialized
// without using "new".
Expand All @@ -29,10 +29,10 @@ static void Main()
// Assign values to p2 members.
p2.Name = "Spencer";
p2.Age = 7;
Console.WriteLine("p2 Name = {0} Age = {1}", p2.Name, p2.Age);
Console.WriteLine($"p2 Name = {p2.Name} Age = {p2.Age}");

// p1 values remain unchanged because p2 is copy.
Console.WriteLine("p1 Name = {0} Age = {1}", p1.Name, p1.Age);
Console.WriteLine($"p1 Name = {p1.Name} Age = {p1.Age}");
}
}
/*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;

public class Person
{
Expand All @@ -17,7 +17,7 @@ class Program
static void Main()
{
Person person1 = new Person("Leopold", 6);
Console.WriteLine("person1 Name = {0} Age = {1}", person1.Name, person1.Age);
Console.WriteLine($"person1 Name = {person1.Name} Age = {person1.Age}");

// Declare new person, assign person1 to it.
Person person2 = person1;
Expand All @@ -26,8 +26,8 @@ static void Main()
person2.Name = "Molly";
person2.Age = 16;

Console.WriteLine("person2 Name = {0} Age = {1}", person2.Name, person2.Age);
Console.WriteLine("person1 Name = {0} Age = {1}", person1.Name, person1.Age);
Console.WriteLine($"person2 Name = {person2.Name} Age = {person2.Age}");
Console.WriteLine($"person1 Name = {person1.Name} Age = {person1.Age}");
}
}
/*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Keywords;
namespace Keywords;

internal class ParameterModifiers
{
Expand All @@ -9,17 +9,17 @@ internal static void ParamPassingExamples()
// Passing a variable by value means passing a copy of the variable.
// n1 is a value type
int n1 = 5;
System.Console.WriteLine("The value before calling the method: {0}", n1);
System.Console.WriteLine($"The value before calling the method: {n1}");

SquareItValue(n1); // Passing the variable by value.
System.Console.WriteLine("The value after calling the method: {0}", n1);
System.Console.WriteLine($"The value after calling the method: {n1}");

static void SquareItValue(int x)
// The parameter x is passed by value.
// Changes to x will not affect the original value of x.
{
x *= x;
System.Console.WriteLine("The value inside the method: {0}", x);
System.Console.WriteLine($"The value inside the method: {x}");
}
/* Output:
The value before calling the method: 5
Expand Down Expand Up @@ -49,17 +49,17 @@ static void ChangeValue(int[] pArray)
// Passing a value by reference means passing a reference to the variable.
// n is a value type
int n2 = 5;
System.Console.WriteLine("The value before calling the method: {0}", n2);
System.Console.WriteLine($"The value before calling the method: {n2}");

SquareItReference(ref n2); // Passing the variable by reference.
System.Console.WriteLine("The value after calling the method: {0}", n2);
System.Console.WriteLine($"The value after calling the method: {n2}");

static void SquareItReference(ref int x)
// The parameter x is passed by reference.
// Changes to x will affect the original value of x.
{
x *= x;
System.Console.WriteLine("The value inside the method: {0}", x);
System.Console.WriteLine($"The value inside the method: {x}");
}
/* Output:
The value before calling the method: 5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static void Conversions()
p++;
}
System.Console.WriteLine();
System.Console.WriteLine("The value of the integer: {0}", number);
System.Console.WriteLine($"The value of the integer: {number}");

/* Output:
The 4 bytes of the integer: 00 04 00 00
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <CreateSequence>
// <CreateSequence>
using WalkthroughWritingLinqQueries;

// Create a data source by using a collection initializer.
Expand Down Expand Up @@ -237,7 +237,7 @@ from student in students
select totalScore;

double averageScore = studentQuery.Average();
Console.WriteLine("Class average score = {0}", averageScore);
Console.WriteLine($"Class average score = {averageScore}");

// Output:
// Class average score = 334.166666666667
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Xml.Linq;
using System.Xml.Linq;

namespace StandardQueryOperators;

Expand Down Expand Up @@ -34,7 +34,7 @@ orderby gr.Key

foreach (var obj in query)
{
Console.WriteLine("Words of length {0}:", obj.Length);
Console.WriteLine($"Words of length {obj.Length}:");
foreach (string word in obj.Words)
Console.WriteLine(word);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;

namespace Properties
{
Expand Down Expand Up @@ -74,20 +74,20 @@ static void Main()
Person person = new Person();

// Print out the name and the age associated with the person:
Console.WriteLine("Person details - {0}", person);
Console.WriteLine($"Person details - {person}");

// Set some values on the person object:
//<Snippet3>
person.Name = "Joe";
person.Age = 99;
//</Snippet3>
Console.WriteLine("Person details - {0}", person);
Console.WriteLine($"Person details - {person}");

// Increment the Age property:
//<Snippet5>
person.Age += 1;
//</Snippet5>
Console.WriteLine("Person details - {0}", person);
Console.WriteLine($"Person details - {person}");

// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using CustomExtensions;
using CustomExtensions;

string s = "The quick brown fox jumped over the lazy dog.";
// Call the method as if it were an
// instance method on the type. Note that the first
// parameter is not specified by the calling code.
int i = s.WordCount();
System.Console.WriteLine("Word count of s is {0}", i);
System.Console.WriteLine($"Word count of s is {i}");


namespace CustomExtensions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//<Snippet1>
//<Snippet1>
abstract class Motorcycle
{
// Anyone can call this.
Expand Down Expand Up @@ -31,7 +31,7 @@ static void Main()
moto.AddGas(15);
moto.Drive(5, 20);
double speed = moto.GetTopSpeed();
Console.WriteLine("My top speed is {0}", speed);
Console.WriteLine($"My top speed is {speed}");
}
}
//</Snippet2>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//<Snippet1>
//<Snippet1>
class NLog
{
// Private Constructor:
Expand Down Expand Up @@ -33,7 +33,7 @@ static void Main()

Counter.currentCount = 100;
Counter.IncrementCount();
Console.WriteLine("New count: {0}", Counter.currentCount);
Console.WriteLine($"New count: {Counter.currentCount}");

// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace HidingExample;
namespace HidingExample;
//<Hiding>
public class Employee
{
Expand Down Expand Up @@ -38,8 +38,8 @@ public static void Test()
((Employee)m1).Name = "Mary";
//</CastToBase>

System.Console.WriteLine("Name in the derived class is: {0}", m1.Name);
System.Console.WriteLine("Name in the base class is: {0}", ((Employee)m1).Name);
System.Console.WriteLine($"Name in the derived class is: {m1.Name}");
System.Console.WriteLine($"Name in the base class is: {((Employee)m1).Name}");
}
}
/* Output:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;

namespace interfaces
{
Expand Down Expand Up @@ -93,8 +93,8 @@ public static void Examples()
e1.Name = System.Console.ReadLine();

System.Console.WriteLine("The employee information:");
System.Console.WriteLine("Employee number: {0}", e1.Counter);
System.Console.WriteLine("Employee name: {0}", e1.Name);
System.Console.WriteLine($"Employee number: {e1.Counter}");
System.Console.WriteLine($"Employee name: {e1.Name}");
// </SnippetUseProperty>
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//<Snippet1>
//<Snippet1>
public static class TemperatureConverter
{
public static double CelsiusToFahrenheit(string temperatureCelsius)
Expand Down Expand Up @@ -41,13 +41,13 @@ static void Main()
case "1":
Console.Write("Please enter the Celsius temperature: ");
F = TemperatureConverter.CelsiusToFahrenheit(Console.ReadLine() ?? "0");
Console.WriteLine("Temperature in Fahrenheit: {0:F2}", F);
Console.WriteLine($"Temperature in Fahrenheit: {F:F2}");
break;

case "2":
Console.Write("Please enter the Fahrenheit temperature: ");
C = TemperatureConverter.FahrenheitToCelsius(Console.ReadLine() ?? "0");
Console.WriteLine("Temperature in Celsius: {0:F2}", C);
Console.WriteLine($"Temperature in Celsius: {C:F2}");
break;

default:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//<Snippet1>
//<Snippet1>
class SimpleClass
{
// Static variable that must be initialized at run time.
Expand Down Expand Up @@ -39,7 +39,7 @@ static Bus()
public Bus(int routeNum)
{
RouteNumber = routeNum;
Console.WriteLine("Bus #{0} is created.", RouteNumber);
Console.WriteLine($"Bus #{RouteNumber} is created.");
}

// Instance method.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ValueEqualityClass;
namespace ValueEqualityClass;

class TwoDPoint : IEquatable<TwoDPoint>
{
Expand Down Expand Up @@ -138,14 +138,14 @@ static void Main(string[] args)
int i = 5;

Console.WriteLine("pointA.Equals(pointB) = {0}", pointA.Equals(pointB));
Console.WriteLine("pointA == pointB = {0}", pointA == pointB);
Console.WriteLine($"pointA == pointB = {pointA == pointB}");
Console.WriteLine("null comparison = {0}", pointA.Equals(pointC));
Console.WriteLine("Compare to some other type = {0}", pointA.Equals(i));

TwoDPoint pointD = null;
TwoDPoint pointE = null;

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

pointE = new TwoDPoint(3, 4);
Console.WriteLine("(pointE == pointA) = {0}", pointE == pointA);
Expand Down
Loading