Skip to content
  •  
  •  
  •  
8 changes: 4 additions & 4 deletions .github/skills/csharp-snippet-modernization/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ Apply these changes when they preserve behavior and sample clarity:

- Use top-level statements where possible.
- Use C# built-in aliases, such as `string`, `int`, and `bool`, instead of
framework type names.
- Use target-typed `new` when the target type is evident.
framework type names. Use `nint` and `nuint` for `IntPtr` and `UIntPtr`.
- Remove calls to `ToString()` when the result is used in a string context, unless the call is the subject of the snippet.
- Use string interpolation instead of composite formatting, unless it makes the code harder to read
or the composite-format overload is the subject of the snippet. For example, keep composite
formatting when the arguments are complex:
Expand All @@ -34,8 +34,8 @@ Apply these changes when they preserve behavior and sample clarity:
timeZoneTime.DateTime);
```
- Use raw string literals (or interpolated raw string literals) for paragraph-style output.
- Use object and collection initializers when evaluation order and behavior
remain unchanged.
- Use target-typed `new` when the target type is evident.
- Use collection expressions to initialize collections and arrays wherever possible.
- Convert eligible value-producing `switch` statements to switch expressions.
- Use auto-implemented properties instead of defining a separate field.
- Use expression-bodied members for simple single-expression members. If the line gets too long, for example, a method signature with a type parameter constraint, place the expression body on a new line:
Expand Down
2 changes: 2 additions & 0 deletions snippets/csharp/System/ComparisonT/Overview/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ComparisonTOverviewExample1.Run();
ComparisonTOverviewExample2.Run();
8 changes: 8 additions & 0 deletions snippets/csharp/System/ComparisonT/Overview/Project.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>

</Project>
95 changes: 41 additions & 54 deletions snippets/csharp/System/ComparisonT/Overview/comparisont1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,62 @@

public class CityInfo
{
string cityName;
string countryName;
int pop2010;
string cityName;
string countryName;
int pop2010;

public CityInfo(string name, string country, int pop2010)
{
this.cityName = name;
this.countryName = country;
this.pop2010 = pop2010;
}
public CityInfo(string name, string country, int pop2010)
{
this.cityName = name;
this.countryName = country;
this.pop2010 = pop2010;
}

public string City
{ get { return this.cityName; } }
public string City => this.cityName;

public string Country
{ get { return this.countryName; } }
public string Country => this.countryName;

public int Population
{ get { return this.pop2010; } }
public int Population => this.pop2010;

public static int CompareByName(CityInfo city1, CityInfo city2)
{
return String.Compare(city1.City, city2.City);
}
public static int CompareByName(CityInfo city1, CityInfo city2) => string.Compare(city1.City, city2.City);

public static int CompareByPopulation(CityInfo city1, CityInfo city2)
{
return city1.Population.CompareTo(city2.Population);
}
public static int CompareByPopulation(CityInfo city1, CityInfo city2) => city1.Population.CompareTo(city2.Population);

public static int CompareByNames(CityInfo city1, CityInfo city2)
{
return String.Compare(city1.Country + city1.City, city2.Country + city2.City);
}
public static int CompareByNames(CityInfo city1, CityInfo city2) => string.Compare(city1.Country + city1.City, city2.Country + city2.City);
}

public class Example
public class ComparisonTOverviewExample1
{
public static void Main()
{
CityInfo NYC = new CityInfo("New York City", "United States of America", 8175133 );
CityInfo Det = new CityInfo("Detroit", "United States of America", 713777);
CityInfo Paris = new CityInfo("Paris", "France", 2193031);
CityInfo[] cities = { NYC, Det, Paris };
// Display ordered array.
DisplayArray(cities);
public static void Run()
{
CityInfo NYC = new("New York City", "United States of America", 8175133);
CityInfo Det = new("Detroit", "United States of America", 713777);
CityInfo Paris = new("Paris", "France", 2193031);
CityInfo[] cities = { NYC, Det, Paris };
// Display ordered array.
DisplayArray(cities);

// Sort array by city name.
Array.Sort(cities, CityInfo.CompareByName);
DisplayArray(cities);
// Sort array by city name.
Array.Sort(cities, CityInfo.CompareByName);
DisplayArray(cities);

// Sort array by population.
Array.Sort(cities, CityInfo.CompareByPopulation);
DisplayArray(cities);
// Sort array by population.
Array.Sort(cities, CityInfo.CompareByPopulation);
DisplayArray(cities);

// Sort array by country + city name.
Array.Sort(cities, CityInfo.CompareByNames);
DisplayArray(cities);
}
// Sort array by country + city name.
Array.Sort(cities, CityInfo.CompareByNames);
DisplayArray(cities);
}

private static void DisplayArray(CityInfo[] cities)
{
Console.WriteLine("{0,-20} {1,-25} {2,10}", "City", "Country", "Population");
foreach (var city in cities)
Console.WriteLine("{0,-20} {1,-25} {2,10:N0}", city.City,
city.Country, city.Population);
private static void DisplayArray(CityInfo[] cities)
{
Console.WriteLine($"{"City",-20} {"Country",-25} {"Population",10}");
foreach (var city in cities)
Console.WriteLine($"{city.City,-20} {city.Country,-25} {city.Population,10:N0}");

Console.WriteLine();
}
Console.WriteLine();
}
}
// The example displays the following output:
// City Country Population
Expand Down
26 changes: 14 additions & 12 deletions snippets/csharp/System/ComparisonT/Overview/source.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System;
using System.Collections.Generic;

public class Example
public class ComparisonTOverviewExample2
{
private static int CompareDinosByLength(string x, string y)
{
Expand All @@ -26,7 +26,7 @@ private static int CompareDinosByLength(string x, string y)
// If x is not null...
//
if (y == null)
// ...and y is null, x is greater.
// ...and y is null, x is greater.
{
return 1;
}
Expand Down Expand Up @@ -55,15 +55,17 @@ private static int CompareDinosByLength(string x, string y)
}
}

public static void Main()
public static void Run()
{
List<string> dinosaurs = new List<string>();
dinosaurs.Add("Pachycephalosaurus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("");
dinosaurs.Add(null);
dinosaurs.Add("Mamenchisaurus");
dinosaurs.Add("Deinonychus");
List<string> dinosaurs = new()
{
"Pachycephalosaurus",
"Amargasaurus",
"",
null,
"Mamenchisaurus",
"Deinonychus"
};
Display(dinosaurs);

Console.WriteLine("\nSort with generic Comparison<string> delegate:");
Expand All @@ -74,12 +76,12 @@ public static void Main()
private static void Display(List<string> list)
{
Console.WriteLine();
foreach( string s in list )
foreach (string s in list)
{
if (s == null)
Console.WriteLine("(null)");
else
Console.WriteLine("\"{0}\"", s);
Console.WriteLine($"\"{s}\"");
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions snippets/csharp/System/LazyT/.ctor/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LazyCtorExample1.Run();
LazyCtorExample2.Run();
LazyCtorExample3.Run();
LazyCtorExample4.Run();
LazyCtorExample5.Run();
LazyCtorExample6.Run();
8 changes: 8 additions & 0 deletions snippets/csharp/System/LazyT/.ctor/Project.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>

</Project>
17 changes: 9 additions & 8 deletions snippets/csharp/System/LazyT/.ctor/example.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//<SnippetAll>
using System;
using System.Threading;
using LargeObject = LargeObjectCtorExample1;

class Program
class LazyCtorExample1
{
static Lazy<LargeObject> lazyLargeObject = null;

static void Main()
public static void Run()
{
// The lazy initializer is created here. LargeObject is not created until the
// ThreadProc method executes.
Expand All @@ -25,7 +26,7 @@ static void Main()
Console.ReadLine();

// Create and start 3 threads, passing the same blocking event to all of them.
ManualResetEvent startingGate = new ManualResetEvent(false);
ManualResetEvent startingGate = new(false);
Thread[] threads = { new Thread(ThreadProc), new Thread(ThreadProc), new Thread(ThreadProc) };
foreach (Thread t in threads)
{
Expand All @@ -49,7 +50,7 @@ static void Main()
static void ThreadProc(object state)
{
// Wait for the signal.
ManualResetEvent waitForStart = (ManualResetEvent) state;
ManualResetEvent waitForStart = (ManualResetEvent)state;
waitForStart.WaitOne();

//<SnippetValueProp>
Expand All @@ -62,7 +63,7 @@ static void ThreadProc(object state)
// IMPORTANT: Lazy initialization is thread-safe, but it doesn't protect the
// object after creation. You must lock the object before accessing it,
// unless the type is thread safe. (LargeObject is not thread safe.)
lock(large)
lock (large)
{
large.Data[0] = Thread.CurrentThread.ManagedThreadId;
Console.WriteLine("Initialized by thread {0}; last used by thread {1}.",
Expand All @@ -71,12 +72,12 @@ static void ThreadProc(object state)
}
}

class LargeObject
class LargeObjectCtorExample1
{
int initBy = 0;
public int InitializedBy { get { return initBy; } }
public int InitializedBy => initBy;

public LargeObject()
public LargeObjectCtorExample1()
{
initBy = Thread.CurrentThread.ManagedThreadId;
Console.WriteLine("LargeObject was created on thread id {0}.", initBy);
Expand Down
13 changes: 5 additions & 8 deletions snippets/csharp/System/LazyT/.ctor/example1.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//<SnippetAll>
using System;
using System.Threading;
using LargeObject = LargeObjectCtorExample2;

class Program
class LazyCtorExample2
{
static Lazy<LargeObject> lazyLargeObject = null;

static void Main()
public static void Run()
{
// The lazy initializer is created here. LargeObject is not created until the
// ThreadProc method executes.
Expand Down Expand Up @@ -34,13 +35,9 @@ static void Main()
}
}

class LargeObject
class LargeObjectCtorExample2
{
public LargeObject()
{
Console.WriteLine("LargeObject was created on thread id {0}.",
Thread.CurrentThread.ManagedThreadId);
}
public LargeObjectCtorExample2() => Console.WriteLine($"LargeObject was created on thread id {Thread.CurrentThread.ManagedThreadId}.");
public long[] Data = new long[100000000];
}

Expand Down
20 changes: 9 additions & 11 deletions snippets/csharp/System/LazyT/.ctor/example2.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
//<SnippetAll>
using System;
using System.Threading;
using LargeObject = LargeObjectCtorExample3;

class Program
class LazyCtorExample3
{
static Lazy<LargeObject> lazyLargeObject = null;

//<SnippetFactoryFunc>
static LargeObject InitLargeObject()
{
return new LargeObject();
}
static LargeObject InitLargeObject() => new LargeObject();
//</SnippetFactoryFunc>

static void Main()
public static void Run()
{
// The lazy initializer is created here. LargeObject is not created until the
// ThreadProc method executes.
Expand Down Expand Up @@ -58,7 +56,7 @@ static void ThreadProc(object state)
// IMPORTANT: Lazy initialization is thread-safe, but it doesn't protect the
// object after creation. You must lock the object before accessing it,
// unless the type is thread safe. (LargeObject is not thread safe.)
lock(large)
lock (large)
{
large.Data[0] = Thread.CurrentThread.ManagedThreadId;
Console.WriteLine("Initialized by thread {0}; last used by thread {1}.",
Expand All @@ -67,20 +65,20 @@ static void ThreadProc(object state)
}
catch (ApplicationException aex)
{
Console.WriteLine("Exception: {0}", aex.Message);
Console.WriteLine($"Exception: {aex.Message}");
}
//</SnippetValueProp>
}
}

class LargeObject
class LargeObjectCtorExample3
{
int initBy = 0;
public int InitializedBy { get { return initBy; } }
public int InitializedBy => initBy;

//<SnippetLargeCtor>
static int instanceCount = 0;
public LargeObject()
public LargeObjectCtorExample3()
{
if (1 == Interlocked.Increment(ref instanceCount))
{
Expand Down
Loading
Loading