Skip to content

Commit ddc5780

Browse files
authored
Modernize C# code snippets - System/P*, System/R* (#12969)
1 parent 969829c commit ddc5780

26 files changed

Lines changed: 337 additions & 387 deletions

File tree

snippets/csharp/System/ParamArrayAttribute/Overview/Example.cs

Lines changed: 54 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,81 +3,75 @@
33

44
public class Temperature
55
{
6-
private decimal temp;
6+
private decimal temp;
77

8-
public Temperature(decimal temperature)
9-
{
10-
this.temp = temperature;
11-
}
8+
public Temperature(decimal temperature) => this.temp = temperature;
129

13-
public override string ToString()
14-
{
15-
return ToString("C");
16-
}
10+
public override string ToString() => ToString("C");
1711

18-
public string ToString(string format)
19-
{
20-
if (String.IsNullOrEmpty(format))
21-
format = "G";
12+
public string ToString(string format)
13+
{
14+
if (string.IsNullOrEmpty(format))
15+
format = "G";
2216

23-
switch (format.ToUpper())
24-
{
25-
case "G":
26-
case "C":
27-
return temp.ToString("N") + " °C";
28-
case "F":
29-
return (9 * temp / 5 + 32).ToString("N") + " °F";
30-
case "K":
31-
return (temp + 273.15m).ToString("N") + " °K";
32-
default:
33-
throw new FormatException(String.Format("The '{0}' format specifier is not supported",
34-
format));
35-
}
36-
}
17+
switch (format.ToUpperInvariant())
18+
{
19+
case "G":
20+
case "C":
21+
return temp.ToString("N") + " °C";
22+
case "F":
23+
return (9 * temp / 5 + 32).ToString("N") + " °F";
24+
case "K":
25+
return (temp + 273.15m).ToString("N") + " °K";
26+
default:
27+
throw new FormatException($"The '{format}' format specifier is not supported");
28+
}
29+
}
3730

38-
public void Display(params string []formats)
39-
{
40-
if (formats.Length == 0)
41-
{
42-
Console.WriteLine(this.ToString("G"));
43-
}
44-
else
45-
{
46-
foreach (string format in formats)
47-
{
48-
try {
49-
Console.WriteLine(this.ToString(format));
31+
public void Display(params string[] formats)
32+
{
33+
if (formats.Length == 0)
34+
{
35+
Console.WriteLine(this.ToString("G"));
36+
}
37+
else
38+
{
39+
foreach (string format in formats)
40+
{
41+
try
42+
{
43+
Console.WriteLine(this.ToString(format));
44+
}
45+
// If there is an exception, do nothing.
46+
catch { }
5047
}
51-
// If there is an exception, do nothing.
52-
catch { }
53-
}
54-
}
55-
}
48+
}
49+
}
5650
}
5751
// </Snippet1>
5852

5953
// <Snippet2>
6054
public class Class1
6155
{
62-
public static void Main()
63-
{
64-
Temperature temp1 = new Temperature(100);
65-
string[] formats = { "C", "G", "F", "K" };
56+
public static void Main()
57+
{
58+
Temperature temp1 = new(100);
59+
string[] formats = [ "C", "G", "F", "K" ];
6660

67-
// Call Display method with a string array.
68-
Console.WriteLine("Calling Display with a string array:");
69-
temp1.Display(formats);
70-
Console.WriteLine();
61+
// Call Display method with a string array.
62+
Console.WriteLine("Calling Display with a string array:");
63+
temp1.Display(formats);
64+
Console.WriteLine();
7165

72-
// Call Display method with individual string arguments.
73-
Console.WriteLine("Calling Display with individual arguments:");
74-
temp1.Display("C", "F", "K", "G");
75-
Console.WriteLine();
66+
// Call Display method with individual string arguments.
67+
Console.WriteLine("Calling Display with individual arguments:");
68+
temp1.Display("C", "F", "K", "G");
69+
Console.WriteLine();
7670

77-
// Call parameterless Display method.
78-
Console.WriteLine("Calling Display with an implicit parameter array:");
79-
temp1.Display();
80-
}
71+
// Call parameterless Display method.
72+
Console.WriteLine("Calling Display with an implicit parameter array:");
73+
temp1.Display();
74+
}
8175
}
8276
// The example displays the following output:
8377
// Calling Display with a string array:

snippets/csharp/System/PlatformID/Overview/pid.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,28 @@ class Sample
66
{
77
public static void Main()
88
{
9-
string msg1 = "This is a Windows operating system.";
10-
string msg2 = "This is a Unix operating system.";
11-
string msg3 = "ERROR: This platform identifier is invalid.";
9+
string msg1 = "This is a Windows operating system.";
10+
string msg2 = "This is a Unix operating system.";
11+
string msg3 = "ERROR: This platform identifier is invalid.";
1212

13-
// Assume this example is run on a Windows operating system.
13+
// Assume this example is run on a Windows operating system.
1414

15-
OperatingSystem os = Environment.OSVersion;
16-
PlatformID pid = os.Platform;
17-
switch (pid)
15+
OperatingSystem os = Environment.OSVersion;
16+
PlatformID pid = os.Platform;
17+
switch (pid)
1818
{
19-
case PlatformID.Win32NT:
20-
case PlatformID.Win32S:
21-
case PlatformID.Win32Windows:
22-
case PlatformID.WinCE:
23-
Console.WriteLine(msg1);
24-
break;
25-
case PlatformID.Unix:
26-
Console.WriteLine(msg2);
27-
break;
28-
default:
29-
Console.WriteLine(msg3);
30-
break;
19+
case PlatformID.Win32NT:
20+
case PlatformID.Win32S:
21+
case PlatformID.Win32Windows:
22+
case PlatformID.WinCE:
23+
Console.WriteLine(msg1);
24+
break;
25+
case PlatformID.Unix:
26+
Console.WriteLine(msg2);
27+
break;
28+
default:
29+
Console.WriteLine(msg3);
30+
break;
3131
}
3232
}
3333
}
@@ -36,4 +36,4 @@ public static void Main()
3636
3737
This is a Windows operating system.
3838
*/
39-
//</snippet1>
39+
//</snippet1>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Library</OutputType>
5+
<TargetFramework>net10.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

snippets/csharp/System/PredicateT/Overview/predicate1.cs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,42 @@
1-
// <Snippet3>
1+
namespace PredicateExample3;
2+
3+
// <Snippet3>
24
using System;
35
using System.Collections.Generic;
46

57
public class HockeyTeam
68
{
7-
private string _name;
8-
private int _founded;
9+
private string _name;
10+
private int _founded;
911

10-
public HockeyTeam(string name, int year)
11-
{
12-
_name = name;
13-
_founded = year;
14-
}
12+
public HockeyTeam(string name, int year)
13+
{
14+
_name = name;
15+
_founded = year;
16+
}
1517

16-
public string Name {
17-
get { return _name; }
18-
}
18+
public string Name => _name;
1919

20-
public int Founded {
21-
get { return _founded; }
22-
}
20+
public int Founded => _founded;
2321
}
2422

2523
public class Example
2624
{
27-
public static void Main()
28-
{
29-
Random rnd = new Random();
30-
List<HockeyTeam> teams = new List<HockeyTeam>();
31-
teams.AddRange( new HockeyTeam[] { new HockeyTeam("Detroit Red Wings", 1926),
25+
public static void Main()
26+
{
27+
Random rnd = new();
28+
List<HockeyTeam> teams = new();
29+
teams.AddRange(new HockeyTeam[] { new HockeyTeam("Detroit Red Wings", 1926),
3230
new HockeyTeam("Chicago Blackhawks", 1926),
3331
new HockeyTeam("San Jose Sharks", 1991),
3432
new HockeyTeam("Montreal Canadiens", 1909),
35-
new HockeyTeam("St. Louis Blues", 1967) } );
36-
int[] years = { 1920, 1930, 1980, 2000 };
37-
int foundedBeforeYear = years[rnd.Next(0, years.Length)];
38-
Console.WriteLine("Teams founded before {0}:", foundedBeforeYear);
39-
foreach (var team in teams.FindAll( x => x.Founded <= foundedBeforeYear))
40-
Console.WriteLine("{0}: {1}", team.Name, team.Founded);
41-
}
33+
new HockeyTeam("St. Louis Blues", 1967) });
34+
int[] years = { 1920, 1930, 1980, 2000 };
35+
int foundedBeforeYear = years[rnd.Next(0, years.Length)];
36+
Console.WriteLine($"Teams founded before {foundedBeforeYear}:");
37+
foreach (var team in teams.FindAll(x => x.Founded <= foundedBeforeYear))
38+
Console.WriteLine($"{team.Name}: {team.Founded}");
39+
}
4240
}
4341
// The example displays output similar to the following:
4442
// Teams founded before 1930:

snippets/csharp/System/PredicateT/Overview/predicateex1.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
// <Snippet2>
1+
namespace PredicateExample2;
2+
3+
// <Snippet2>
24
using System;
35
using System.Drawing;
46

57
public class Example
68
{
7-
public static void Main()
8-
{
9-
// Create an array of Point structures.
10-
Point[] points = { new Point(100, 200),
9+
public static void Main()
10+
{
11+
// Create an array of Point structures.
12+
Point[] points = { new Point(100, 200),
1113
new Point(150, 250), new Point(250, 375),
1214
new Point(275, 395), new Point(295, 450) };
1315

14-
// Find the first Point structure for which X times Y
15-
// is greater than 100000.
16-
Point first = Array.Find(points, x => x.X * x.Y > 100000 );
16+
// Find the first Point structure for which X times Y
17+
// is greater than 100000.
18+
Point first = Array.Find(points, x => x.X * x.Y > 100000);
1719

18-
// Display the first structure found.
19-
Console.WriteLine("Found: X = {0}, Y = {1}", first.X, first.Y);
20-
}
20+
// Display the first structure found.
21+
Console.WriteLine($"Found: X = {first.X}, Y = {first.Y}");
22+
}
2123
}
2224
// The example displays the following output:
2325
// Found: X = 275, Y = 395

snippets/csharp/System/PredicateT/Overview/predicateex2.cs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
1-
// <Snippet4>
1+
namespace PredicateExample4;
2+
3+
// <Snippet4>
24
using System;
35
using System.Drawing;
46

57
public class Example
68
{
7-
public static void Main()
8-
{
9-
// Create an array of Point structures.
10-
Point[] points = { new Point(100, 200),
9+
public static void Main()
10+
{
11+
// Create an array of Point structures.
12+
Point[] points = { new Point(100, 200),
1113
new Point(150, 250), new Point(250, 375),
1214
new Point(275, 395), new Point(295, 450) };
1315

14-
// Define the Predicate<T> delegate.
15-
Predicate<Point> predicate = FindPoints;
16+
// Define the Predicate<T> delegate.
17+
Predicate<Point> predicate = FindPoints;
1618

17-
// Find the first Point structure for which X times Y
18-
// is greater than 100000.
19-
Point first = Array.Find(points, predicate);
19+
// Find the first Point structure for which X times Y
20+
// is greater than 100000.
21+
Point first = Array.Find(points, predicate);
2022

21-
// Display the first structure found.
22-
Console.WriteLine("Found: X = {0}, Y = {1}", first.X, first.Y);
23-
}
23+
// Display the first structure found.
24+
Console.WriteLine($"Found: X = {first.X}, Y = {first.Y}");
25+
}
2426

25-
private static bool FindPoints(Point obj)
26-
{
27-
return obj.X * obj.Y > 100000;
28-
}
27+
private static bool FindPoints(Point obj) => obj.X * obj.Y > 100000;
2928
}
3029
// The example displays the following output:
3130
// Found: X = 275, Y = 395

0 commit comments

Comments
 (0)