Skip to content

Commit 969829c

Browse files
authored
Modernize C# code snippets - System/U*, System/V*, System/W* (#12974)
1 parent 4a3eddb commit 969829c

101 files changed

Lines changed: 2676 additions & 2578 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

snippets/csharp/System/UInt16/CompareTo/source.cs

Lines changed: 300 additions & 300 deletions
Large diffs are not rendered by default.

snippets/csharp/System/UInt16/Equals/equalsoverl.cs

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,36 @@
33

44
public class Example
55
{
6-
static ushort value = 112;
7-
8-
public static void Main()
9-
{
10-
byte byte1= 112;
11-
Console.WriteLine("value = byte1: {0,16}", value.Equals(byte1));
12-
TestObjectForEquality(byte1);
13-
14-
short short1 = 112;
15-
Console.WriteLine("value = short1: {0,17}", value.Equals(short1));
16-
TestObjectForEquality(short1);
17-
18-
int int1 = 112;
19-
Console.WriteLine("value = int1: {0,19}", value.Equals(int1));
20-
TestObjectForEquality(int1);
21-
22-
sbyte sbyte1 = 112;
23-
Console.WriteLine("value = sbyte1: {0,17}", value.Equals(sbyte1));
24-
TestObjectForEquality(sbyte1);
25-
26-
decimal dec1 = 112m;
27-
Console.WriteLine("value = dec1: {0,21}", value.Equals(dec1));
28-
TestObjectForEquality(dec1);
29-
30-
double dbl1 = 112;
31-
Console.WriteLine("value = dbl1: {0,20}", value.Equals(dbl1));
32-
TestObjectForEquality(dbl1);
33-
}
34-
35-
private static void TestObjectForEquality(Object obj)
36-
{
37-
Console.WriteLine("{0} ({1}) = {2} ({3}): {4}\n",
38-
value, value.GetType().Name,
39-
obj, obj.GetType().Name,
40-
value.Equals(obj));
41-
}
6+
static ushort value = 112;
7+
8+
public static void Main()
9+
{
10+
byte byte1 = 112;
11+
Console.WriteLine($"value = byte1: {value.Equals(byte1),16}");
12+
TestObjectForEquality(byte1);
13+
14+
short short1 = 112;
15+
Console.WriteLine($"value = short1: {value.Equals(short1),17}");
16+
TestObjectForEquality(short1);
17+
18+
int int1 = 112;
19+
Console.WriteLine($"value = int1: {value.Equals(int1),19}");
20+
TestObjectForEquality(int1);
21+
22+
sbyte sbyte1 = 112;
23+
Console.WriteLine($"value = sbyte1: {value.Equals(sbyte1),17}");
24+
TestObjectForEquality(sbyte1);
25+
26+
decimal dec1 = 112m;
27+
Console.WriteLine($"value = dec1: {value.Equals(dec1),21}");
28+
TestObjectForEquality(dec1);
29+
30+
double dbl1 = 112;
31+
Console.WriteLine($"value = dbl1: {value.Equals(dbl1),20}");
32+
TestObjectForEquality(dbl1);
33+
}
34+
35+
private static void TestObjectForEquality(object obj) => Console.WriteLine($"{value} ({value.GetType().Name}) = {obj} ({obj.GetType().Name}): {value.Equals(obj)}\n");
4236
}
4337
// The example displays the following output:
4438
// value = byte1: True

snippets/csharp/System/UInt16/Equals/uint16_equals.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ public static void MyMethod()
1515
try
1616
{
1717
// <Snippet1>
18-
UInt16 myVariable1 = 10;
19-
UInt16 myVariable2 = 10;
18+
ushort myVariable1 = 10;
19+
ushort myVariable2 = 10;
2020

2121
//Display the declaring type.
2222
Console.WriteLine("\nType of 'myVariable1' is '{0}' and" +
@@ -36,7 +36,7 @@ public static void MyMethod()
3636
}
3737
catch (Exception e)
3838
{
39-
Console.WriteLine("Exception :{0}", e.Message);
39+
Console.WriteLine($"Exception :{e.Message}");
4040
}
4141
}
4242
}

snippets/csharp/System/UInt16/MaxValue/MaxValue.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
public class Class1
44
{
5-
public static void Main()
6-
{
7-
// <Snippet1>
8-
int integerValue = 1216;
9-
ushort uIntegerValue;
10-
11-
if (integerValue >= ushort.MinValue & integerValue <= ushort.MaxValue)
12-
{
13-
uIntegerValue = (ushort) integerValue;
14-
Console.WriteLine(uIntegerValue);
15-
}
16-
else
17-
{
18-
Console.WriteLine("Unable to convert {0} to a UInt16t.", integerValue);
19-
}
20-
// </Snippet1>
21-
}
5+
public static void Main()
6+
{
7+
// <Snippet1>
8+
int integerValue = 1216;
9+
ushort uIntegerValue;
10+
11+
if (integerValue >= ushort.MinValue && integerValue <= ushort.MaxValue)
12+
{
13+
uIntegerValue = (ushort)integerValue;
14+
Console.WriteLine(uIntegerValue);
15+
}
16+
else
17+
{
18+
Console.WriteLine($"Unable to convert {integerValue} to a UInt16.");
19+
}
20+
// </Snippet1>
21+
}
2222
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
UInt16ParseExample2.Run();
2+
UInt16ParseExample3.Run();
3+
UInt16ParseExample4.Run();
4+
UInt16ParseExample5.Run();
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>Exe</OutputType>
5+
<TargetFramework>net10.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

snippets/csharp/System/UInt16/Parse/parseex2.cs

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,36 @@
22
using System;
33
using System.Globalization;
44

5-
public class Example
5+
public class UInt16ParseExample2
66
{
7-
public static void Main()
8-
{
9-
string[] values = { " 214 ", "1,064", "(0)", "1241+", " + 214 ", " +214 ", "2153.0", "1e03", "1300.0e-2" };
10-
NumberStyles whitespace = NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite;
11-
NumberStyles[] styles = { NumberStyles.None, whitespace,
12-
NumberStyles.AllowLeadingSign | NumberStyles.AllowTrailingSign | whitespace,
13-
NumberStyles.AllowThousands | NumberStyles.AllowCurrencySymbol,
7+
public static void Run()
8+
{
9+
string[] values = { " 214 ", "1,064", "(0)", "1241+", " + 214 ", " +214 ", "2153.0", "1e03", "1300.0e-2" };
10+
NumberStyles whitespace = NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite;
11+
NumberStyles[] styles = { NumberStyles.None, whitespace,
12+
NumberStyles.AllowLeadingSign | NumberStyles.AllowTrailingSign | whitespace,
13+
NumberStyles.AllowThousands | NumberStyles.AllowCurrencySymbol,
1414
NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint };
1515

16-
// Attempt to convert each number using each style combination.
17-
foreach (string value in values)
18-
{
19-
Console.WriteLine("Attempting to convert '{0}':", value);
20-
foreach (NumberStyles style in styles)
21-
{
22-
try {
23-
ushort number = UInt16.Parse(value, style);
24-
Console.WriteLine(" {0}: {1}", style, number);
25-
}
26-
catch (FormatException) {
27-
Console.WriteLine(" {0}: Bad Format", style);
16+
// Attempt to convert each number using each style combination.
17+
foreach (string value in values)
18+
{
19+
Console.WriteLine($"Attempting to convert '{value}':");
20+
foreach (NumberStyles style in styles)
21+
{
22+
try
23+
{
24+
ushort number = ushort.Parse(value, style);
25+
Console.WriteLine($" {style}: {number}");
26+
}
27+
catch (FormatException)
28+
{
29+
Console.WriteLine($" {style}: Bad Format");
30+
}
2831
}
29-
}
30-
Console.WriteLine();
31-
}
32-
}
32+
Console.WriteLine();
33+
}
34+
}
3335
}
3436
// The example display the following output:
3537
// Attempting to convert ' 214 ':
@@ -38,56 +40,56 @@ public static void Main()
3840
// Integer, AllowTrailingSign: 214
3941
// AllowThousands, AllowCurrencySymbol: Bad Format
4042
// AllowDecimalPoint, AllowExponent: Bad Format
41-
//
43+
//
4244
// Attempting to convert '1,064':
4345
// None: Bad Format
4446
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
4547
// Integer, AllowTrailingSign: Bad Format
4648
// AllowThousands, AllowCurrencySymbol: 1064
4749
// AllowDecimalPoint, AllowExponent: Bad Format
48-
//
50+
//
4951
// Attempting to convert '(0)':
5052
// None: Bad Format
5153
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
5254
// Integer, AllowTrailingSign: Bad Format
5355
// AllowThousands, AllowCurrencySymbol: Bad Format
5456
// AllowDecimalPoint, AllowExponent: Bad Format
55-
//
57+
//
5658
// Attempting to convert '1241+':
5759
// None: Bad Format
5860
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
5961
// Integer, AllowTrailingSign: 1241
6062
// AllowThousands, AllowCurrencySymbol: Bad Format
6163
// AllowDecimalPoint, AllowExponent: Bad Format
62-
//
64+
//
6365
// Attempting to convert ' + 214 ':
6466
// None: Bad Format
6567
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
6668
// Integer, AllowTrailingSign: Bad Format
6769
// AllowThousands, AllowCurrencySymbol: Bad Format
6870
// AllowDecimalPoint, AllowExponent: Bad Format
69-
//
71+
//
7072
// Attempting to convert ' +214 ':
7173
// None: Bad Format
7274
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
7375
// Integer, AllowTrailingSign: 214
7476
// AllowThousands, AllowCurrencySymbol: Bad Format
7577
// AllowDecimalPoint, AllowExponent: Bad Format
76-
//
78+
//
7779
// Attempting to convert '2153.0':
7880
// None: Bad Format
7981
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
8082
// Integer, AllowTrailingSign: Bad Format
8183
// AllowThousands, AllowCurrencySymbol: Bad Format
8284
// AllowDecimalPoint, AllowExponent: 2153
83-
//
85+
//
8486
// Attempting to convert '1e03':
8587
// None: Bad Format
8688
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
8789
// Integer, AllowTrailingSign: Bad Format
8890
// AllowThousands, AllowCurrencySymbol: Bad Format
8991
// AllowDecimalPoint, AllowExponent: 1000
90-
//
92+
//
9193
// Attempting to convert '1300.0e-2':
9294
// None: Bad Format
9395
// AllowLeadingWhite, AllowTrailingWhite: Bad Format

snippets/csharp/System/UInt16/Parse/parseex3.cs

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,40 @@
22
using System;
33
using System.Globalization;
44

5-
public class Example
5+
public class UInt16ParseExample3
66
{
7-
public static void Main()
8-
{
9-
// Define a custom culture that uses "++" as a positive sign.
10-
CultureInfo ci = new CultureInfo("");
11-
ci.NumberFormat.PositiveSign = "++";
12-
// Create an array of cultures.
13-
CultureInfo[] cultures = { ci, CultureInfo.InvariantCulture };
14-
// Create an array of strings to parse.
15-
string[] values = { "++1403", "-0", "+0", "+16034",
16-
Int16.MinValue.ToString(), "14.0", "18012" };
17-
// Parse the strings using each culture.
18-
foreach (CultureInfo culture in cultures)
19-
{
20-
Console.WriteLine("Parsing with the '{0}' culture.", culture.Name);
21-
foreach (string value in values)
22-
{
23-
try {
24-
ushort number = UInt16.Parse(value, culture);
25-
Console.WriteLine(" Converted '{0}' to {1}.", value, number);
7+
public static void Run()
8+
{
9+
// Define a custom culture that uses "++" as a positive sign.
10+
CultureInfo ci = new("");
11+
ci.NumberFormat.PositiveSign = "++";
12+
// Create an array of cultures.
13+
CultureInfo[] cultures = { ci, CultureInfo.InvariantCulture };
14+
// Create an array of strings to parse.
15+
string[] values = { "++1403", "-0", "+0", "+16034",
16+
short.MinValue.ToString(), "14.0", "18012" };
17+
// Parse the strings using each culture.
18+
foreach (CultureInfo culture in cultures)
19+
{
20+
Console.WriteLine($"Parsing with the '{culture.Name}' culture.");
21+
foreach (string value in values)
22+
{
23+
try
24+
{
25+
ushort number = ushort.Parse(value, culture);
26+
Console.WriteLine($" Converted '{value}' to {number}.");
27+
}
28+
catch (FormatException)
29+
{
30+
Console.WriteLine($" The format of '{value}' is invalid.");
31+
}
32+
catch (OverflowException)
33+
{
34+
Console.WriteLine($" '{value}' is outside the range of a UInt16 value.");
35+
}
2636
}
27-
catch (FormatException) {
28-
Console.WriteLine(" The format of '{0}' is invalid.", value);
29-
}
30-
catch (OverflowException) {
31-
Console.WriteLine(" '{0}' is outside the range of a UInt16 value.", value);
32-
}
33-
}
34-
}
35-
}
37+
}
38+
}
3639
}
3740
// The example displays the following output:
3841
// Parsing with the culture.

0 commit comments

Comments
 (0)