Skip to content
Draft
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
600 changes: 300 additions & 300 deletions snippets/csharp/System/UInt16/CompareTo/source.cs

Large diffs are not rendered by default.

66 changes: 30 additions & 36 deletions snippets/csharp/System/UInt16/Equals/equalsoverl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,36 @@

public class Example
{
static ushort value = 112;

public static void Main()
{
byte byte1= 112;
Console.WriteLine("value = byte1: {0,16}", value.Equals(byte1));
TestObjectForEquality(byte1);

short short1 = 112;
Console.WriteLine("value = short1: {0,17}", value.Equals(short1));
TestObjectForEquality(short1);

int int1 = 112;
Console.WriteLine("value = int1: {0,19}", value.Equals(int1));
TestObjectForEquality(int1);

sbyte sbyte1 = 112;
Console.WriteLine("value = sbyte1: {0,17}", value.Equals(sbyte1));
TestObjectForEquality(sbyte1);

decimal dec1 = 112m;
Console.WriteLine("value = dec1: {0,21}", value.Equals(dec1));
TestObjectForEquality(dec1);

double dbl1 = 112;
Console.WriteLine("value = dbl1: {0,20}", value.Equals(dbl1));
TestObjectForEquality(dbl1);
}

private static void TestObjectForEquality(Object obj)
{
Console.WriteLine("{0} ({1}) = {2} ({3}): {4}\n",
value, value.GetType().Name,
obj, obj.GetType().Name,
value.Equals(obj));
}
static ushort value = 112;

public static void Main()
{
byte byte1 = 112;
Console.WriteLine($"value = byte1: {value.Equals(byte1),16}");
TestObjectForEquality(byte1);

short short1 = 112;
Console.WriteLine($"value = short1: {value.Equals(short1),17}");
TestObjectForEquality(short1);

int int1 = 112;
Console.WriteLine($"value = int1: {value.Equals(int1),19}");
TestObjectForEquality(int1);

sbyte sbyte1 = 112;
Console.WriteLine($"value = sbyte1: {value.Equals(sbyte1),17}");
TestObjectForEquality(sbyte1);

decimal dec1 = 112m;
Console.WriteLine($"value = dec1: {value.Equals(dec1),21}");
TestObjectForEquality(dec1);

double dbl1 = 112;
Console.WriteLine($"value = dbl1: {value.Equals(dbl1),20}");
TestObjectForEquality(dbl1);
}

private static void TestObjectForEquality(object obj) => Console.WriteLine($"{value} ({value.GetType().Name}) = {obj} ({obj.GetType().Name}): {value.Equals(obj)}\n");
}
// The example displays the following output:
// value = byte1: True
Expand Down
6 changes: 3 additions & 3 deletions snippets/csharp/System/UInt16/Equals/uint16_equals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public static void MyMethod()
try
{
// <Snippet1>
UInt16 myVariable1 = 10;
UInt16 myVariable2 = 10;
ushort myVariable1 = 10;
ushort myVariable2 = 10;

//Display the declaring type.
Console.WriteLine("\nType of 'myVariable1' is '{0}' and" +
Expand All @@ -36,7 +36,7 @@ public static void MyMethod()
}
catch (Exception e)
{
Console.WriteLine("Exception :{0}", e.Message);
Console.WriteLine($"Exception :{e.Message}");
}
}
}
34 changes: 17 additions & 17 deletions snippets/csharp/System/UInt16/MaxValue/MaxValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

public class Class1
{
public static void Main()
{
// <Snippet1>
int integerValue = 1216;
ushort uIntegerValue;
if (integerValue >= ushort.MinValue & integerValue <= ushort.MaxValue)
{
uIntegerValue = (ushort) integerValue;
Console.WriteLine(uIntegerValue);
}
else
{
Console.WriteLine("Unable to convert {0} to a UInt16t.", integerValue);
}
// </Snippet1>
}
public static void Main()
{
// <Snippet1>
int integerValue = 1216;
ushort uIntegerValue;

if (integerValue >= ushort.MinValue && integerValue <= ushort.MaxValue)
{
uIntegerValue = (ushort)integerValue;
Console.WriteLine(uIntegerValue);
}
else
{
Console.WriteLine($"Unable to convert {integerValue} to a UInt16.");
}
// </Snippet1>
}
}
4 changes: 4 additions & 0 deletions snippets/csharp/System/UInt16/Parse/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
UInt16ParseExample2.Run();
UInt16ParseExample3.Run();
UInt16ParseExample4.Run();
UInt16ParseExample5.Run();
8 changes: 8 additions & 0 deletions snippets/csharp/System/UInt16/Parse/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>
66 changes: 34 additions & 32 deletions snippets/csharp/System/UInt16/Parse/parseex2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,36 @@
using System;
using System.Globalization;

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

// Attempt to convert each number using each style combination.
foreach (string value in values)
{
Console.WriteLine("Attempting to convert '{0}':", value);
foreach (NumberStyles style in styles)
{
try {
ushort number = UInt16.Parse(value, style);
Console.WriteLine(" {0}: {1}", style, number);
}
catch (FormatException) {
Console.WriteLine(" {0}: Bad Format", style);
// Attempt to convert each number using each style combination.
foreach (string value in values)
{
Console.WriteLine($"Attempting to convert '{value}':");
foreach (NumberStyles style in styles)
{
try
{
ushort number = ushort.Parse(value, style);
Console.WriteLine($" {style}: {number}");
}
catch (FormatException)
{
Console.WriteLine($" {style}: Bad Format");
}
}
}
Console.WriteLine();
}
}
Console.WriteLine();
}
}
}
// The example display the following output:
// Attempting to convert ' 214 ':
Expand All @@ -38,56 +40,56 @@ public static void Main()
// Integer, AllowTrailingSign: 214
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
//
// Attempting to convert '1,064':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: 1064
// AllowDecimalPoint, AllowExponent: Bad Format
//
//
// Attempting to convert '(0)':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
//
// Attempting to convert '1241+':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: 1241
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
//
// Attempting to convert ' + 214 ':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
//
// Attempting to convert ' +214 ':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: 214
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
//
// Attempting to convert '2153.0':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: 2153
//
//
// Attempting to convert '1e03':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: 1000
//
//
// Attempting to convert '1300.0e-2':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
Expand Down
61 changes: 32 additions & 29 deletions snippets/csharp/System/UInt16/Parse/parseex3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,40 @@
using System;
using System.Globalization;

public class Example
public class UInt16ParseExample3
{
public static void Main()
{
// Define a custom culture that uses "++" as a positive sign.
CultureInfo ci = new CultureInfo("");
ci.NumberFormat.PositiveSign = "++";
// Create an array of cultures.
CultureInfo[] cultures = { ci, CultureInfo.InvariantCulture };
// Create an array of strings to parse.
string[] values = { "++1403", "-0", "+0", "+16034",
Int16.MinValue.ToString(), "14.0", "18012" };
// Parse the strings using each culture.
foreach (CultureInfo culture in cultures)
{
Console.WriteLine("Parsing with the '{0}' culture.", culture.Name);
foreach (string value in values)
{
try {
ushort number = UInt16.Parse(value, culture);
Console.WriteLine(" Converted '{0}' to {1}.", value, number);
public static void Run()
{
// Define a custom culture that uses "++" as a positive sign.
CultureInfo ci = new("");
ci.NumberFormat.PositiveSign = "++";
// Create an array of cultures.
CultureInfo[] cultures = { ci, CultureInfo.InvariantCulture };
// Create an array of strings to parse.
string[] values = { "++1403", "-0", "+0", "+16034",
short.MinValue.ToString(), "14.0", "18012" };
// Parse the strings using each culture.
foreach (CultureInfo culture in cultures)
{
Console.WriteLine($"Parsing with the '{culture.Name}' culture.");
foreach (string value in values)
{
try
{
ushort number = ushort.Parse(value, culture);
Console.WriteLine($" Converted '{value}' to {number}.");
}
catch (FormatException)
{
Console.WriteLine($" The format of '{value}' is invalid.");
}
catch (OverflowException)
{
Console.WriteLine($" '{value}' is outside the range of a UInt16 value.");
}
}
catch (FormatException) {
Console.WriteLine(" The format of '{0}' is invalid.", value);
}
catch (OverflowException) {
Console.WriteLine(" '{0}' is outside the range of a UInt16 value.", value);
}
}
}
}
}
}
}
// The example displays the following output:
// Parsing with the culture.
Expand Down
Loading
Loading