From b67485c5f2d5481b93750624304ea9d74bce619c Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Thu, 6 Aug 2026 16:58:31 -0700 Subject: [PATCH 1/2] Modernize C# code snippets --- .../System/Convert/ChangeType/changetype00.cs | 87 +- .../System/Convert/ChangeType/changetype01.cs | 12 +- .../System/Convert/ChangeType/changetype03.cs | 382 +++--- .../Convert/ChangeType/changetype_enum2.cs | 38 +- .../Convert/ChangeType/changetype_nullable.cs | 20 +- .../ChangeType/changetype_nullable_1.cs | 20 +- .../Convert/ChangeType/convertchangetype.cs | 14 +- .../csharp/System/Convert/DBNull/dbnull1.cs | 14 +- .../Convert/FromBase64CharArray/class1.cs | 537 ++++---- .../Convert/FromBase64CharArray/tb64ca.cs | 85 +- .../csharp/System/Convert/IsDBNull/Form1.cs | 177 ++- .../System/Convert/Overview/NonDecimal1.cs | 22 +- .../System/Convert/Overview/converter.cs | 120 +- .../System/Convert/ToBase64String/tb64s.cs | 108 +- .../System/Convert/ToBoolean/ToBoolean1.cs | 30 +- .../System/Convert/ToBoolean/objectifp.cs | 378 +++--- .../ToBoolean/system.convert snippet.cs | 1087 +++++++++-------- .../System/Convert/ToBoolean/toboolean2.cs | 502 ++++---- .../System/Convert/ToByte/Conversion.cs | 729 ++++++----- .../System/Convert/ToChar/strdatetime.cs | 46 +- .../System/Convert/ToChar/stringnonnum.cs | 62 +- .../csharp/System/Convert/ToChar/tochar1.cs | 548 +++++---- .../System/Convert/ToDateTime/ToDateTime1.cs | 76 +- .../System/Convert/ToDateTime/ToDateTime2.cs | 84 +- .../System/Convert/ToDateTime/ToDateTime3.cs | 52 +- .../System/Convert/ToDateTime/todatetime4.cs | 81 +- .../System/Convert/ToDecimal/ToDecimal1.cs | 46 +- .../System/Convert/ToDecimal/todecimal11.cs | 451 ++++--- .../System/Convert/ToDecimal/todecimal2.cs | 351 +++--- .../System/Convert/ToDecimal/todecimal3.cs | 41 +- .../System/Convert/ToDouble/example8.cs | 39 +- .../System/Convert/ToDouble/todouble.cs | 51 +- .../System/Convert/ToDouble/todouble1.cs | 409 +++---- .../csharp/System/Convert/ToInt16/toint16.cs | 64 +- .../System/Convert/ToInt16/toint16_1.cs | 712 ++++++----- .../System/Convert/ToInt16/toint16_2.cs | 45 +- .../csharp/System/Convert/ToInt16/toint32.cs | 64 +- .../csharp/System/Convert/ToInt16/toint64.cs | 64 +- .../csharp/System/Convert/ToInt16/tosbyte.cs | 60 +- .../System/Convert/ToInt32/toint32_1.cs | 777 ++++++------ .../System/Convert/ToInt32/toint32_2.cs | 67 +- .../System/Convert/ToInt64/toint64_1.cs | 738 ++++++----- .../System/Convert/ToInt64/toint64_2.cs | 45 +- .../System/Convert/ToInt64/toint64_3.cs | 67 +- .../csharp/System/Convert/ToSByte/tosbyte1.cs | 744 ++++++----- .../csharp/System/Convert/ToSByte/tosbyte2.cs | 441 +++---- .../csharp/System/Convert/ToSByte/tosbyte3.cs | 49 +- .../System/Convert/ToSingle/tosingle1.cs | 620 +++++----- .../System/Convert/ToSingle/tosingle2.cs | 351 +++--- .../System/Convert/ToSingle/tosingle3.cs | 50 +- .../System/Convert/ToString/ToString.Byte1.cs | 14 +- .../System/Convert/ToString/ToString_Bool1.cs | 32 +- .../System/Convert/ToString/nonnumeric.cs | 76 +- .../System/Convert/ToString/tostring1.cs | 382 +++--- .../System/Convert/ToString/tostring2.cs | 380 +++--- .../System/Convert/ToString/tostring3.cs | 650 +++++----- .../System/Convert/ToString/tostring5.cs | 57 +- .../System/Convert/ToString/tostring6.cs | 78 +- .../System/Convert/ToString/tostring7.cs | 42 +- .../System/Convert/ToString/tostring_obj30.cs | 103 +- .../Convert/ToString/tostring_string1.cs | 20 +- .../System/Convert/ToUInt16/touint16_1.cs | 804 ++++++------ .../System/Convert/ToUInt16/touint16_2.cs | 45 +- .../System/Convert/ToUInt16/touint16_3.cs | 438 +++---- .../System/Convert/ToUInt16/touint16_4.cs | 44 +- .../System/Convert/ToUInt32/touint32_1.cs | 788 ++++++------ .../System/Convert/ToUInt32/touint32_2.cs | 51 +- .../System/Convert/ToUInt32/touint32_3.cs | 47 +- .../System/Convert/ToUInt32/touint32_4.cs | 483 ++++---- .../System/Convert/ToUInt64/touint64_1.cs | 797 ++++++------ .../System/Convert/ToUInt64/touint64_2.cs | 51 +- .../System/Convert/ToUInt64/touint64_3.cs | 46 +- .../System/Convert/ToUInt64/touint64_4.cs | 521 ++++---- .../Overview/source.cs | 20 +- 74 files changed, 8717 insertions(+), 8909 deletions(-) diff --git a/snippets/csharp/System/Convert/ChangeType/changetype00.cs b/snippets/csharp/System/Convert/ChangeType/changetype00.cs index bd8bef711f6..a2a382e7580 100644 --- a/snippets/csharp/System/Convert/ChangeType/changetype00.cs +++ b/snippets/csharp/System/Convert/ChangeType/changetype00.cs @@ -4,56 +4,57 @@ public class InterceptProvider : IFormatProvider { - public object GetFormat(Type formatType) - { - if (formatType == typeof(NumberFormatInfo)) { - Console.WriteLine(" Returning a fr-FR numeric format provider."); - return new System.Globalization.CultureInfo("fr-FR").NumberFormat; - } - else if (formatType == typeof(DateTimeFormatInfo)) { - Console.WriteLine(" Returning an en-US date/time format provider."); - return new System.Globalization.CultureInfo("en-US").DateTimeFormat; - } - else { - Console.WriteLine(" Requesting a format provider of {0}.", formatType.Name); - return null; - } - } + public object GetFormat(Type formatType) + { + if (formatType == typeof(NumberFormatInfo)) + { + Console.WriteLine(" Returning a fr-FR numeric format provider."); + return new System.Globalization.CultureInfo("fr-FR").NumberFormat; + } + else if (formatType == typeof(DateTimeFormatInfo)) + { + Console.WriteLine(" Returning an en-US date/time format provider."); + return new System.Globalization.CultureInfo("en-US").DateTimeFormat; + } + else + { + Console.WriteLine($" Requesting a format provider of {formatType.Name}."); + return null; + } + } } public class Example { - public static void Main() - { - object[] values = { 103.5d, new DateTime(2010, 12, 26, 14, 34, 0) }; - IFormatProvider provider = new InterceptProvider(); + public static void Main() + { + object[] values = { 103.5d, new DateTime(2010, 12, 26, 14, 34, 0) }; + IFormatProvider provider = new InterceptProvider(); - // Convert value to each of the types represented in TypeCode enum. - foreach (object value in values) - { - // Iterate types in TypeCode enum. - foreach (TypeCode enumType in ((TypeCode[]) Enum.GetValues(typeof(TypeCode)))) - { - if (enumType == TypeCode.DBNull || enumType == TypeCode.Empty) continue; + // Convert value to each of the types represented in TypeCode enum. + foreach (object value in values) + { + // Iterate types in TypeCode enum. + foreach (TypeCode enumType in ((TypeCode[])Enum.GetValues(typeof(TypeCode)))) + { + if (enumType == TypeCode.DBNull || enumType == TypeCode.Empty) continue; - try { - Console.WriteLine("{0} ({1}) --> {2} ({3}).", - value, value.GetType().Name, - Convert.ChangeType(value, enumType, provider), - enumType.ToString()); + try + { + Console.WriteLine($"{value} ({value.GetType().Name}) --> {Convert.ChangeType(value, enumType, provider)} ({enumType.ToString()})."); + } + catch (InvalidCastException) + { + Console.WriteLine($"Cannot convert a {value.GetType().Name} to a {enumType.ToString()}"); + } + catch (OverflowException) + { + Console.WriteLine($"Overflow: {value} is out of the range of a {enumType.ToString()}"); + } } - catch (InvalidCastException) { - Console.WriteLine("Cannot convert a {0} to a {1}", - value.GetType().Name, enumType.ToString()); - } - catch (OverflowException) { - Console.WriteLine("Overflow: {0} is out of the range of a {1}", - value, enumType.ToString()); - } - } - Console.WriteLine(); - } - } + Console.WriteLine(); + } + } } // The example displays the following output: // 103.5 (Double) --> 103.5 (Object). diff --git a/snippets/csharp/System/Convert/ChangeType/changetype01.cs b/snippets/csharp/System/Convert/ChangeType/changetype01.cs index 0a239aaab1d..b82d64eea74 100644 --- a/snippets/csharp/System/Convert/ChangeType/changetype01.cs +++ b/snippets/csharp/System/Convert/ChangeType/changetype01.cs @@ -1,18 +1,20 @@ // using System; -public class ChangeTypeTest { - public static void Main() { +public class ChangeTypeTest +{ + public static void Main() + { - Double d = -2.345; + double d = -2.345; int i = (int)Convert.ChangeType(d, TypeCode.Int32); - Console.WriteLine("The Double {0} when converted to an Int32 is {1}", d, i); + Console.WriteLine($"The Double {d} when converted to an Int32 is {i}"); string s = "12/12/2009"; DateTime dt = (DateTime)Convert.ChangeType(s, typeof(DateTime)); - Console.WriteLine("The String {0} when converted to a Date is {1}", s, dt); + Console.WriteLine($"The String {s} when converted to a Date is {dt}"); } } // The example displays the following output: diff --git a/snippets/csharp/System/Convert/ChangeType/changetype03.cs b/snippets/csharp/System/Convert/ChangeType/changetype03.cs index ceb6c8f140a..d37f8a41aa5 100644 --- a/snippets/csharp/System/Convert/ChangeType/changetype03.cs +++ b/snippets/csharp/System/Convert/ChangeType/changetype03.cs @@ -4,226 +4,180 @@ public class Temperature : IConvertible { - private decimal m_Temp; - - public Temperature(decimal temperature) - { - this.m_Temp = temperature; - } - - public decimal Celsius - { - get { return this.m_Temp; } - } - - public decimal Kelvin - { - get { return this.m_Temp + 273.15m; } - } - - public decimal Fahrenheit - { - get { return Math.Round((decimal) (this.m_Temp * 9 / 5 + 32), 2); } - } - - public override string ToString() - { - return m_Temp.ToString("N2") + "°C"; - } - - // IConvertible implementations. - public TypeCode GetTypeCode() - { - return TypeCode.Object; - } - - public bool ToBoolean(IFormatProvider provider) - { - if (m_Temp == 0) - return false; - else - return true; - } - - public byte ToByte(IFormatProvider provider) - { - if (m_Temp < Byte.MinValue || m_Temp > Byte.MaxValue) - throw new OverflowException(String.Format("{0} is out of range of the Byte type.", - this.m_Temp)); - else - return Decimal.ToByte(this.m_Temp); - } - - public char ToChar(IFormatProvider provider) - { - throw new InvalidCastException("Temperature to Char conversion is not supported."); - } - - public DateTime ToDateTime(IFormatProvider provider) - { - throw new InvalidCastException("Temperature to DateTime conversion is not supported."); - } - - public decimal ToDecimal(IFormatProvider provider) - { - return this.m_Temp; - } - - public double ToDouble(IFormatProvider provider) - { - return Decimal.ToDouble(this.m_Temp); - } - - public short ToInt16(IFormatProvider provider) - { - if (this.m_Temp < Int16.MinValue || this.m_Temp > Int16.MaxValue) - throw new OverflowException(String.Format("{0} is out of range of the Int16 type.", - this.m_Temp)); - else - return Decimal.ToInt16(this.m_Temp); - } - - public int ToInt32(IFormatProvider provider) - { - if (this.m_Temp < Int32.MinValue || this.m_Temp > Int32.MaxValue) - throw new OverflowException(String.Format("{0} is out of range of the Int32 type.", - this.m_Temp)); - else - return Decimal.ToInt32(this.m_Temp); - } - - public long ToInt64(IFormatProvider provider) - { - if (this.m_Temp < Int64.MinValue || this.m_Temp > Int64.MaxValue) - throw new OverflowException(String.Format("{0} is out of range of the Int64 type.", - this.m_Temp)); - else - return Decimal.ToInt64(this.m_Temp); - } - - public sbyte ToSByte(IFormatProvider provider) - { - if (this.m_Temp < SByte.MinValue || this.m_Temp > SByte.MaxValue) - throw new OverflowException(String.Format("{0} is out of range of the SByte type.", - this.m_Temp)); - else - return Decimal.ToSByte(this.m_Temp); - } - - public float ToSingle(IFormatProvider provider) - { - return Decimal.ToSingle(this.m_Temp); - } - - public string ToString(IFormatProvider provider) - { - return m_Temp.ToString("N2", provider) + "°C"; - } - - public object ToType(Type conversionType, IFormatProvider provider) - { - switch (Type.GetTypeCode(conversionType)) - { - case TypeCode.Boolean: - return this.ToBoolean(null); - case TypeCode.Byte: - return this.ToByte(null); - case TypeCode.Char: - return this.ToChar(null); - case TypeCode.DateTime: - return this.ToDateTime(null); - case TypeCode.Decimal: - return this.ToDecimal(null); - case TypeCode.Double: - return this.ToDouble(null); - case TypeCode.Int16: - return this.ToInt16(null); - case TypeCode.Int32: - return this.ToInt32(null); - case TypeCode.Int64: - return this.ToInt64(null); - case TypeCode.Object: - if (typeof(Temperature).Equals(conversionType)) - return this; - else - throw new InvalidCastException(String.Format("Conversion to a {0} is not supported.", - conversionType.Name)); - case TypeCode.SByte: - return this.ToSByte(null); - case TypeCode.Single: - return this.ToSingle(null); - case TypeCode.String: - return this.ToString(provider); - case TypeCode.UInt16: - return this.ToUInt16(null); - case TypeCode.UInt32: - return this.ToUInt32(null); - case TypeCode.UInt64: - return this.ToUInt64(null); - default: - throw new InvalidCastException(String.Format("Conversion to {0} is not supported.", conversionType.Name)); - } - } - - public ushort ToUInt16(IFormatProvider provider) - { - if (this.m_Temp < UInt16.MinValue || this.m_Temp > UInt16.MaxValue) - throw new OverflowException(String.Format("{0} is out of range of the UInt16 type.", - this.m_Temp)); - else - return Decimal.ToUInt16(this.m_Temp); - } - - public uint ToUInt32(IFormatProvider provider) - { - if (this.m_Temp < UInt32.MinValue || this.m_Temp > UInt32.MaxValue) - throw new OverflowException(String.Format("{0} is out of range of the UInt32 type.", - this.m_Temp)); - else - return Decimal.ToUInt32(this.m_Temp); - } - - public ulong ToUInt64(IFormatProvider provider) - { - if (this.m_Temp < UInt64.MinValue || this.m_Temp > UInt64.MaxValue) - throw new OverflowException(String.Format("{0} is out of range of the UInt64 type.", - this.m_Temp)); - else - return Decimal.ToUInt64(this.m_Temp); - } + private decimal m_Temp; + + public Temperature(decimal temperature) => this.m_Temp = temperature; + + public decimal Celsius => this.m_Temp; + + public decimal Kelvin => this.m_Temp + 273.15m; + + public decimal Fahrenheit => Math.Round((decimal)(this.m_Temp * 9 / 5 + 32), 2); + + public override string ToString() => m_Temp.ToString("N2") + "°C"; + + // IConvertible implementations. + public TypeCode GetTypeCode() => TypeCode.Object; + + public bool ToBoolean(IFormatProvider provider) + { + if (m_Temp == 0) + return false; + else + return true; + } + + public byte ToByte(IFormatProvider provider) + { + if (m_Temp < byte.MinValue || m_Temp > byte.MaxValue) + throw new OverflowException($"{this.m_Temp} is out of range of the Byte type."); + else + return decimal.ToByte(this.m_Temp); + } + + public char ToChar(IFormatProvider provider) => throw new InvalidCastException("Temperature to Char conversion is not supported."); + + public DateTime ToDateTime(IFormatProvider provider) => throw new InvalidCastException("Temperature to DateTime conversion is not supported."); + + public decimal ToDecimal(IFormatProvider provider) => this.m_Temp; + + public double ToDouble(IFormatProvider provider) => decimal.ToDouble(this.m_Temp); + + public short ToInt16(IFormatProvider provider) + { + if (this.m_Temp < short.MinValue || this.m_Temp > short.MaxValue) + throw new OverflowException($"{this.m_Temp} is out of range of the Int16 type."); + else + return decimal.ToInt16(this.m_Temp); + } + + public int ToInt32(IFormatProvider provider) + { + if (this.m_Temp < int.MinValue || this.m_Temp > int.MaxValue) + throw new OverflowException($"{this.m_Temp} is out of range of the Int32 type."); + else + return decimal.ToInt32(this.m_Temp); + } + + public long ToInt64(IFormatProvider provider) + { + if (this.m_Temp < long.MinValue || this.m_Temp > long.MaxValue) + throw new OverflowException($"{this.m_Temp} is out of range of the Int64 type."); + else + return decimal.ToInt64(this.m_Temp); + } + + public sbyte ToSByte(IFormatProvider provider) + { + if (this.m_Temp < sbyte.MinValue || this.m_Temp > sbyte.MaxValue) + throw new OverflowException($"{this.m_Temp} is out of range of the SByte type."); + else + return decimal.ToSByte(this.m_Temp); + } + + public float ToSingle(IFormatProvider provider) => decimal.ToSingle(this.m_Temp); + + public string ToString(IFormatProvider provider) => m_Temp.ToString("N2", provider) + "°C"; + + public object ToType(Type conversionType, IFormatProvider provider) + { + switch (Type.GetTypeCode(conversionType)) + { + case TypeCode.Boolean: + return this.ToBoolean(null); + case TypeCode.Byte: + return this.ToByte(null); + case TypeCode.Char: + return this.ToChar(null); + case TypeCode.DateTime: + return this.ToDateTime(null); + case TypeCode.Decimal: + return this.ToDecimal(null); + case TypeCode.Double: + return this.ToDouble(null); + case TypeCode.Int16: + return this.ToInt16(null); + case TypeCode.Int32: + return this.ToInt32(null); + case TypeCode.Int64: + return this.ToInt64(null); + case TypeCode.Object: + if (typeof(Temperature).Equals(conversionType)) + return this; + else + throw new InvalidCastException($"Conversion to a {conversionType.Name} is not supported."); + case TypeCode.SByte: + return this.ToSByte(null); + case TypeCode.Single: + return this.ToSingle(null); + case TypeCode.String: + return this.ToString(provider); + case TypeCode.UInt16: + return this.ToUInt16(null); + case TypeCode.UInt32: + return this.ToUInt32(null); + case TypeCode.UInt64: + return this.ToUInt64(null); + default: + throw new InvalidCastException($"Conversion to {conversionType.Name} is not supported."); + } + } + + public ushort ToUInt16(IFormatProvider provider) + { + if (this.m_Temp < ushort.MinValue || this.m_Temp > ushort.MaxValue) + throw new OverflowException($"{this.m_Temp} is out of range of the UInt16 type."); + else + return decimal.ToUInt16(this.m_Temp); + } + + public uint ToUInt32(IFormatProvider provider) + { + if (this.m_Temp < uint.MinValue || this.m_Temp > uint.MaxValue) + throw new OverflowException($"{this.m_Temp} is out of range of the UInt32 type."); + else + return decimal.ToUInt32(this.m_Temp); + } + + public ulong ToUInt64(IFormatProvider provider) + { + if (this.m_Temp < ulong.MinValue || this.m_Temp > ulong.MaxValue) + throw new OverflowException($"{this.m_Temp} is out of range of the UInt64 type."); + else + return decimal.ToUInt64(this.m_Temp); + } } // // public class Example { - public static void Main() - { - Temperature cool = new Temperature(5); - Type[] targetTypes = { typeof(SByte), typeof(Int16), typeof(Int32), - typeof(Int64), typeof(Byte), typeof(UInt16), - typeof(UInt32), typeof(UInt64), typeof(Decimal), - typeof(Single), typeof(Double), typeof(String) }; - CultureInfo provider = new CultureInfo("fr-FR"); - - foreach (Type targetType in targetTypes) - { - try { - object value = Convert.ChangeType(cool, targetType, provider); - Console.WriteLine("Converted {0} {1} to {2} {3}.", - cool.GetType().Name, cool.ToString(), - targetType.Name, value); - } - catch (InvalidCastException) { - Console.WriteLine("Unsupported {0} --> {1} conversion.", - cool.GetType().Name, targetType.Name); - } - catch (OverflowException) { - Console.WriteLine("{0} is out of range of the {1} type.", - cool, targetType.Name); - } - } - } + public static void Main() + { + Temperature cool = new(5); + Type[] targetTypes = { typeof(sbyte), typeof(short), typeof(int), + typeof(long), typeof(byte), typeof(ushort), + typeof(uint), typeof(ulong), typeof(decimal), + typeof(float), typeof(double), typeof(string) }; + CultureInfo provider = new("fr-FR"); + + foreach (Type targetType in targetTypes) + { + try + { + object value = Convert.ChangeType(cool, targetType, provider); + Console.WriteLine($"Converted {cool.GetType().Name} {cool.ToString()} to {targetType.Name} {value}."); + } + catch (InvalidCastException) + { + Console.WriteLine($"Unsupported {cool.GetType().Name} --> {targetType.Name} conversion."); + } + catch (OverflowException) + { + Console.WriteLine($"{cool} is out of range of the {targetType.Name} type."); + } + } + } } // The example dosplays the following output: // Converted Temperature 5.00°C to SByte 5. diff --git a/snippets/csharp/System/Convert/ChangeType/changetype_enum2.cs b/snippets/csharp/System/Convert/ChangeType/changetype_enum2.cs index 088b41df9a7..a4ed9e76f79 100644 --- a/snippets/csharp/System/Convert/ChangeType/changetype_enum2.cs +++ b/snippets/csharp/System/Convert/ChangeType/changetype_enum2.cs @@ -3,31 +3,31 @@ public enum Continent { - Africa, Antarctica, Asia, Australia, Europe, - NorthAmerica, SouthAmerica + Africa, Antarctica, Asia, Australia, Europe, + NorthAmerica, SouthAmerica }; public class Example { - public static void Main() - { - // Convert a Continent to a Double. - Continent cont = Continent.NorthAmerica; - Console.WriteLine("{0:N2}", - Convert.ChangeType(cont, typeof(Double))); + public static void Main() + { + // Convert a Continent to a Double. + Continent cont = Continent.NorthAmerica; + Console.WriteLine($"{Convert.ChangeType(cont, typeof(double)):N2}"); - // Convert a Double to a Continent. - Double number = 6.0; - try { - Console.WriteLine("{0}", - Convert.ChangeType(number, typeof(Continent))); - } - catch (InvalidCastException) { - Console.WriteLine("Cannot convert a Double to a Continent"); - } + // Convert a Double to a Continent. + double number = 6.0; + try + { + Console.WriteLine($"{Convert.ChangeType(number, typeof(Continent))}"); + } + catch (InvalidCastException) + { + Console.WriteLine("Cannot convert a Double to a Continent"); + } - Console.WriteLine("{0}", (Continent) number); - } + Console.WriteLine($"{(Continent)number}"); + } } // The example displays the following output: // 5.00 diff --git a/snippets/csharp/System/Convert/ChangeType/changetype_nullable.cs b/snippets/csharp/System/Convert/ChangeType/changetype_nullable.cs index 2f67aeafa0f..d4333be34b0 100644 --- a/snippets/csharp/System/Convert/ChangeType/changetype_nullable.cs +++ b/snippets/csharp/System/Convert/ChangeType/changetype_nullable.cs @@ -3,18 +3,16 @@ public class Example { - public static void Main() - { - int? intValue1 = 12893; - double dValue1 = (double) Convert.ChangeType(intValue1, typeof(Double)); - Console.WriteLine("{0} ({1})--> {2} ({3})", intValue1, intValue1.GetType().Name, - dValue1, dValue1.GetType().Name); + public static void Main() + { + int? intValue1 = 12893; + double dValue1 = (double)Convert.ChangeType(intValue1, typeof(double)); + Console.WriteLine($"{intValue1} ({intValue1.GetType().Name})--> {dValue1} ({dValue1.GetType().Name})"); - float fValue1 = 16.3478f; - int? intValue2 = (int) fValue1; - Console.WriteLine("{0} ({1})--> {2} ({3})", fValue1, fValue1.GetType().Name, - intValue2, intValue2.GetType().Name); - } + float fValue1 = 16.3478f; + int? intValue2 = (int)fValue1; + Console.WriteLine($"{fValue1} ({fValue1.GetType().Name})--> {intValue2} ({intValue2.GetType().Name})"); + } } // The example displays the following output: // 12893 (Int32)--> 12893 (Double) diff --git a/snippets/csharp/System/Convert/ChangeType/changetype_nullable_1.cs b/snippets/csharp/System/Convert/ChangeType/changetype_nullable_1.cs index 7bd6805fc88..398e8ef7fac 100644 --- a/snippets/csharp/System/Convert/ChangeType/changetype_nullable_1.cs +++ b/snippets/csharp/System/Convert/ChangeType/changetype_nullable_1.cs @@ -3,18 +3,16 @@ public class Example { - public static void Main() - { - int? intValue1 = 12893; - double dValue1 = (double) Convert.ChangeType(intValue1, typeof(Double), null); - Console.WriteLine("{0} ({1})--> {2} ({3})", intValue1, intValue1.GetType().Name, - dValue1, dValue1.GetType().Name); + public static void Main() + { + int? intValue1 = 12893; + double dValue1 = (double)Convert.ChangeType(intValue1, typeof(double), null); + Console.WriteLine($"{intValue1} ({intValue1.GetType().Name})--> {dValue1} ({dValue1.GetType().Name})"); - float fValue1 = 16.3478f; - int? intValue2 = (int) fValue1; - Console.WriteLine("{0} ({1})--> {2} ({3})", fValue1, fValue1.GetType().Name, - intValue2, intValue2.GetType().Name); - } + float fValue1 = 16.3478f; + int? intValue2 = (int)fValue1; + Console.WriteLine($"{fValue1} ({fValue1.GetType().Name})--> {intValue2} ({intValue2.GetType().Name})"); + } } // The example displays the following output: // 12893 (Int32)--> 12893 (Double) diff --git a/snippets/csharp/System/Convert/ChangeType/convertchangetype.cs b/snippets/csharp/System/Convert/ChangeType/convertchangetype.cs index 412d98bac62..4537c59010b 100644 --- a/snippets/csharp/System/Convert/ChangeType/convertchangetype.cs +++ b/snippets/csharp/System/Convert/ChangeType/convertchangetype.cs @@ -1,18 +1,20 @@ // using System; -public class ChangeTypeTest { - public static void Main() { +public class ChangeTypeTest +{ + public static void Main() + { - Double d = -2.345; + double d = -2.345; int i = (int)Convert.ChangeType(d, typeof(int)); - Console.WriteLine("The double value {0} when converted to an int becomes {1}", d, i); + Console.WriteLine($"The double value {d} when converted to an int becomes {i}"); string s = "12/12/98"; DateTime dt = (DateTime)Convert.ChangeType(s, typeof(DateTime)); - Console.WriteLine("The string value {0} when converted to a Date becomes {1}", s, dt); + Console.WriteLine($"The string value {s} when converted to a Date becomes {dt}"); } } -// \ No newline at end of file +// diff --git a/snippets/csharp/System/Convert/DBNull/dbnull1.cs b/snippets/csharp/System/Convert/DBNull/dbnull1.cs index c1866a50aa6..2854916c219 100644 --- a/snippets/csharp/System/Convert/DBNull/dbnull1.cs +++ b/snippets/csharp/System/Convert/DBNull/dbnull1.cs @@ -2,11 +2,11 @@ public class Example { - public static void Main() - { - // - Console.WriteLine(Convert.DBNull.Equals(DBNull.Value)); - // Displays True. - // - } + public static void Main() + { + // + Console.WriteLine(Convert.DBNull.Equals(DBNull.Value)); + // Displays True. + // + } } diff --git a/snippets/csharp/System/Convert/FromBase64CharArray/class1.cs b/snippets/csharp/System/Convert/FromBase64CharArray/class1.cs index 758e9d9ed94..2cca78e0742 100644 --- a/snippets/csharp/System/Convert/FromBase64CharArray/class1.cs +++ b/snippets/csharp/System/Convert/FromBase64CharArray/class1.cs @@ -1,274 +1,315 @@ -using System; + namespace UUCodeC { - class Coder - { - private string inputFileName; - private string outputFileName; + class Coder + { + private string inputFileName; + private string outputFileName; - static void Main(string[] args) - { - if (args.Length != 4 ) { - System.Console.WriteLine("Usage: UUCodeC -d | -e " + - "-s | -c inputFile outputFile"); - return; - } + static void Main(string[] args) + { + if (args.Length != 4) + { + System.Console.WriteLine("Usage: UUCodeC -d | -e " + + "-s | -c inputFile outputFile"); + return; + } - string inputFileName = args[2]; - string outputFileName = args[3]; - Coder coder = new Coder(inputFileName, outputFileName); + string inputFileName = args[2]; + string outputFileName = args[3]; + Coder coder = new(inputFileName, outputFileName); - switch (args[0]) { - case "-d": - if (args[1] == "-s") { - coder.DecodeWithString(); - } - else if (args[1] == "-c") { - coder.DecodeWithCharArray(); - } - else { - System.Console.WriteLine("Second arg must be -s or -c"); - return; - } - break; - case "-e": - if (args[1] == "-s") { - coder.EncodeWithString(); - } - else if (args[1] == "-c") { - coder.EncodeWithCharArray(); - } - else { - System.Console.WriteLine("Second arg must be -s or -c"); - return; - } - break; - default: - System.Console.WriteLine("First arg must be -d or -e"); - break; - } - } + switch (args[0]) + { + case "-d": + if (args[1] == "-s") + { + coder.DecodeWithString(); + } + else if (args[1] == "-c") + { + coder.DecodeWithCharArray(); + } + else + { + System.Console.WriteLine("Second arg must be -s or -c"); + return; + } + break; + case "-e": + if (args[1] == "-s") + { + coder.EncodeWithString(); + } + else if (args[1] == "-c") + { + coder.EncodeWithCharArray(); + } + else + { + System.Console.WriteLine("Second arg must be -s or -c"); + return; + } + break; + default: + System.Console.WriteLine("First arg must be -d or -e"); + break; + } + } - public Coder (string inFile, string outFile) { - inputFileName = (string) inFile.Clone(); - outputFileName = (string) outFile.Clone(); - } + public Coder(string inFile, string outFile) + { + inputFileName = (string)inFile.Clone(); + outputFileName = (string)outFile.Clone(); + } - // - public void EncodeWithString() { - System.IO.FileStream inFile; - byte[] binaryData; + // + public void EncodeWithString() + { + System.IO.FileStream inFile; + byte[] binaryData; - try { - inFile = new System.IO.FileStream(inputFileName, - System.IO.FileMode.Open, - System.IO.FileAccess.Read); - binaryData = new Byte[inFile.Length]; - long bytesRead = inFile.Read(binaryData, 0, - (int)inFile.Length); - inFile.Close(); - } - catch (System.Exception exp) { - // Error creating stream or reading from it. - System.Console.WriteLine("{0}", exp.Message); - return; - } + try + { + inFile = new(inputFileName, + System.IO.FileMode.Open, + System.IO.FileAccess.Read); + binaryData = new byte[inFile.Length]; + long bytesRead = inFile.Read(binaryData, 0, + (int)inFile.Length); + inFile.Close(); + } + catch (System.Exception exp) + { + // Error creating stream or reading from it. + System.Console.WriteLine($"{exp.Message}"); + return; + } - // Convert the binary input into Base64 UUEncoded output. - string base64String; - try { - base64String = - System.Convert.ToBase64String(binaryData, - 0, - binaryData.Length); - } - catch (System.ArgumentNullException) { - System.Console.WriteLine("Binary data array is null."); - return; - } + // Convert the binary input into Base64 UUEncoded output. + string base64String; + try + { + base64String = + System.Convert.ToBase64String(binaryData, + 0, + binaryData.Length); + } + catch (System.ArgumentNullException) + { + System.Console.WriteLine("Binary data array is null."); + return; + } - // Write the UUEncoded version to the output file. - System.IO.StreamWriter outFile; - try { - outFile = new System.IO.StreamWriter(outputFileName, - false, - System.Text.Encoding.ASCII); - outFile.Write(base64String); - outFile.Close(); - } - catch (System.Exception exp) { - // Error creating stream or writing to it. - System.Console.WriteLine("{0}", exp.Message); - } - } - // + // Write the UUEncoded version to the output file. + System.IO.StreamWriter outFile; + try + { + outFile = new(outputFileName, + false, + System.Text.Encoding.ASCII); + outFile.Write(base64String); + outFile.Close(); + } + catch (System.Exception exp) + { + // Error creating stream or writing to it. + System.Console.WriteLine($"{exp.Message}"); + } + } + // - // - public void EncodeWithCharArray() { - System.IO.FileStream inFile; - byte[] binaryData; + // + public void EncodeWithCharArray() + { + System.IO.FileStream inFile; + byte[] binaryData; - try { - inFile = new System.IO.FileStream(inputFileName, - System.IO.FileMode.Open, - System.IO.FileAccess.Read); - binaryData = new Byte[inFile.Length]; - long bytesRead = inFile.Read(binaryData, 0, - (int) inFile.Length); - inFile.Close(); - } - catch (System.Exception exp) { - // Error creating stream or reading from it. - System.Console.WriteLine("{0}", exp.Message); - return; - } + try + { + inFile = new(inputFileName, + System.IO.FileMode.Open, + System.IO.FileAccess.Read); + binaryData = new byte[inFile.Length]; + long bytesRead = inFile.Read(binaryData, 0, + (int)inFile.Length); + inFile.Close(); + } + catch (System.Exception exp) + { + // Error creating stream or reading from it. + System.Console.WriteLine($"{exp.Message}"); + return; + } - // Convert the binary input into Base64 UUEncoded output. - // Each 3 byte sequence in the source data becomes a 4 byte - // sequence in the character array. - long arrayLength = (long) ((4.0d/3.0d) * binaryData.Length); + // Convert the binary input into Base64 UUEncoded output. + // Each 3 byte sequence in the source data becomes a 4 byte + // sequence in the character array. + long arrayLength = (long)((4.0d / 3.0d) * binaryData.Length); - // If array length is not divisible by 4, go up to the next - // multiple of 4. - if (arrayLength % 4 != 0) { - arrayLength += 4 - arrayLength % 4; - } + // If array length is not divisible by 4, go up to the next + // multiple of 4. + if (arrayLength % 4 != 0) + { + arrayLength += 4 - arrayLength % 4; + } - char[] base64CharArray = new char[arrayLength]; - try { - System.Convert.ToBase64CharArray(binaryData, - 0, - binaryData.Length, - base64CharArray, - 0); - } - catch (System.ArgumentNullException) { - System.Console.WriteLine("Binary data array is null."); - return; - } - catch (System.ArgumentOutOfRangeException) { - System.Console.WriteLine("Char Array is not large enough."); - return; - } + char[] base64CharArray = new char[arrayLength]; + try + { + System.Convert.ToBase64CharArray(binaryData, + 0, + binaryData.Length, + base64CharArray, + 0); + } + catch (System.ArgumentNullException) + { + System.Console.WriteLine("Binary data array is null."); + return; + } + catch (System.ArgumentOutOfRangeException) + { + System.Console.WriteLine("Char Array is not large enough."); + return; + } - // Write the UUEncoded version to the output file. - System.IO.StreamWriter outFile; - try { - outFile = new System.IO.StreamWriter(outputFileName, - false, - System.Text.Encoding.ASCII); - outFile.Write(base64CharArray); - outFile.Close(); - } - catch (System.Exception exp) { - // Error creating stream or writing to it. - System.Console.WriteLine("{0}", exp.Message); - } - } - // + // Write the UUEncoded version to the output file. + System.IO.StreamWriter outFile; + try + { + outFile = new(outputFileName, + false, + System.Text.Encoding.ASCII); + outFile.Write(base64CharArray); + outFile.Close(); + } + catch (System.Exception exp) + { + // Error creating stream or writing to it. + System.Console.WriteLine($"{exp.Message}"); + } + } + // - // - public void DecodeWithCharArray() { - System.IO.StreamReader inFile; - char[] base64CharArray; + // + public void DecodeWithCharArray() + { + System.IO.StreamReader inFile; + char[] base64CharArray; - try { - inFile = new System.IO.StreamReader(inputFileName, - System.Text.Encoding.ASCII); - base64CharArray = new char[inFile.BaseStream.Length]; - inFile.Read(base64CharArray, 0, (int)inFile.BaseStream.Length); - inFile.Close(); - } - catch (System.Exception exp) { - // Error creating stream or reading from it. - System.Console.WriteLine("{0}", exp.Message); - return; - } + try + { + inFile = new(inputFileName, + System.Text.Encoding.ASCII); + base64CharArray = new char[inFile.BaseStream.Length]; + inFile.Read(base64CharArray, 0, (int)inFile.BaseStream.Length); + inFile.Close(); + } + catch (System.Exception exp) + { + // Error creating stream or reading from it. + System.Console.WriteLine($"{exp.Message}"); + return; + } - // Convert the Base64 UUEncoded input into binary output. - byte[] binaryData; - try { - binaryData = - System.Convert.FromBase64CharArray(base64CharArray, - 0, - base64CharArray.Length); - } - catch ( System.ArgumentNullException ) { - System.Console.WriteLine("Base 64 character array is null."); - return; - } - catch ( System.FormatException ) { - System.Console.WriteLine("Base 64 Char Array length is not " + - "4 or is not an even multiple of 4." ); - return; - } + // Convert the Base64 UUEncoded input into binary output. + byte[] binaryData; + try + { + binaryData = + System.Convert.FromBase64CharArray(base64CharArray, + 0, + base64CharArray.Length); + } + catch (System.ArgumentNullException) + { + System.Console.WriteLine("Base 64 character array is null."); + return; + } + catch (System.FormatException) + { + System.Console.WriteLine("Base 64 Char Array length is not " + + "4 or is not an even multiple of 4."); + return; + } - // Write out the decoded data. - System.IO.FileStream outFile; - try { - outFile = new System.IO.FileStream(outputFileName, - System.IO.FileMode.Create, - System.IO.FileAccess.Write); - outFile.Write(binaryData, 0, binaryData.Length); - outFile.Close(); - } - catch (System.Exception exp) { - // Error creating stream or writing to it. - System.Console.WriteLine("{0}", exp.Message); - } - } - // + // Write out the decoded data. + System.IO.FileStream outFile; + try + { + outFile = new(outputFileName, + System.IO.FileMode.Create, + System.IO.FileAccess.Write); + outFile.Write(binaryData, 0, binaryData.Length); + outFile.Close(); + } + catch (System.Exception exp) + { + // Error creating stream or writing to it. + System.Console.WriteLine($"{exp.Message}"); + } + } + // - // - public void DecodeWithString() { - System.IO.StreamReader inFile; - string base64String; + // + public void DecodeWithString() + { + System.IO.StreamReader inFile; + string base64String; - try { - char[] base64CharArray; - inFile = new System.IO.StreamReader(inputFileName, - System.Text.Encoding.ASCII); - base64CharArray = new char[inFile.BaseStream.Length]; - inFile.Read(base64CharArray, 0, (int)inFile.BaseStream.Length); - base64String = new string(base64CharArray); - } - catch (System.Exception exp) { - // Error creating stream or reading from it. - System.Console.WriteLine("{0}", exp.Message); - return; - } + try + { + char[] base64CharArray; + inFile = new(inputFileName, + System.Text.Encoding.ASCII); + base64CharArray = new char[inFile.BaseStream.Length]; + inFile.Read(base64CharArray, 0, (int)inFile.BaseStream.Length); + base64String = new(base64CharArray); + } + catch (System.Exception exp) + { + // Error creating stream or reading from it. + System.Console.WriteLine($"{exp.Message}"); + return; + } - // Convert the Base64 UUEncoded input into binary output. - byte[] binaryData; - try { - binaryData = - System.Convert.FromBase64String(base64String); - } - catch (System.ArgumentNullException) { - System.Console.WriteLine("Base 64 string is null."); - return; - } - catch (System.FormatException) { - System.Console.WriteLine("Base 64 string length is not " + - "4 or is not an even multiple of 4." ); - return; - } + // Convert the Base64 UUEncoded input into binary output. + byte[] binaryData; + try + { + binaryData = + System.Convert.FromBase64String(base64String); + } + catch (System.ArgumentNullException) + { + System.Console.WriteLine("Base 64 string is null."); + return; + } + catch (System.FormatException) + { + System.Console.WriteLine("Base 64 string length is not " + + "4 or is not an even multiple of 4."); + return; + } - // Write out the decoded data. - System.IO.FileStream outFile; - try { - outFile = new System.IO.FileStream(outputFileName, - System.IO.FileMode.Create, - System.IO.FileAccess.Write); - outFile.Write(binaryData, 0, binaryData.Length); - outFile.Close(); - } - catch (System.Exception exp) { - // Error creating stream or writing to it. - System.Console.WriteLine("{0}", exp.Message); - } - } - // - } + // Write out the decoded data. + System.IO.FileStream outFile; + try + { + outFile = new(outputFileName, + System.IO.FileMode.Create, + System.IO.FileAccess.Write); + outFile.Write(binaryData, 0, binaryData.Length); + outFile.Close(); + } + catch (System.Exception exp) + { + // Error creating stream or writing to it. + System.Console.WriteLine($"{exp.Message}"); + } + } + // + } } diff --git a/snippets/csharp/System/Convert/FromBase64CharArray/tb64ca.cs b/snippets/csharp/System/Convert/FromBase64CharArray/tb64ca.cs index 88d6ec98831..c6e998da5c7 100644 --- a/snippets/csharp/System/Convert/FromBase64CharArray/tb64ca.cs +++ b/snippets/csharp/System/Convert/FromBase64CharArray/tb64ca.cs @@ -8,58 +8,57 @@ class Sample { public static void Main() { - byte[] byteArray1 = new byte[256]; - byte[] byteArray2 = new byte[256]; - char[] charArray = new char[352]; - int charArrayLength; - string nl = Environment.NewLine; + byte[] byteArray1 = new byte[256]; + byte[] byteArray2 = new byte[256]; + char[] charArray = new char[352]; + int charArrayLength; + string nl = Environment.NewLine; - string ruler1a = " 1 2 3 4"; - string ruler2a = "1234567890123456789012345678901234567890"; - string ruler3a = "----+----+----+----+----+----+----+----+"; - string ruler1b = " 5 6 7 "; - string ruler2b = "123456789012345678901234567890123456"; - string ruler3b = "----+----+----+----+----+----+----+-"; - string ruler = String.Concat(ruler1a, ruler1b, nl, - ruler2a, ruler2b, nl, - ruler3a, ruler3b); + string ruler1a = " 1 2 3 4"; + string ruler2a = "1234567890123456789012345678901234567890"; + string ruler3a = "----+----+----+----+----+----+----+----+"; + string ruler1b = " 5 6 7 "; + string ruler2b = "123456789012345678901234567890123456"; + string ruler3b = "----+----+----+----+----+----+----+-"; + string ruler = string.Concat(ruler1a, ruler1b, nl, + ruler2a, ruler2b, nl, + ruler3a, ruler3b); -// 1) Initialize and display a Byte array of arbitrary data. - Console.WriteLine("1) Input: A Byte array of arbitrary data.{0}", nl); - for (int x = 0; x < byteArray1.Length; x++) - { - byteArray1[x] = (byte)x; - Console.Write("{0:X2} ", byteArray1[x]); - if (((x+1)%20) == 0) Console.WriteLine(); - } - Console.Write("{0}{0}", nl); + // 1) Initialize and display a Byte array of arbitrary data. + Console.WriteLine($"1) Input: A Byte array of arbitrary data.{nl}"); + for (int x = 0; x < byteArray1.Length; x++) + { + byteArray1[x] = (byte)x; + Console.Write($"{byteArray1[x]:X2} "); + if (((x + 1) % 20) == 0) Console.WriteLine(); + } + Console.Write("{0}{0}", nl); -// 2) Convert the input Byte array to a Char array, with newlines inserted. - charArrayLength = - Convert.ToBase64CharArray(byteArray1, 0, byteArray1.Length, - charArray, 0, Base64FormattingOptions.InsertLineBreaks); - Console.WriteLine("2) Convert the input Byte array to a Char array with newlines."); - Console.Write(" Output: A Char array (length = {0}). ", charArrayLength); - Console.WriteLine("The elements of the array are:{0}", nl); - Console.WriteLine(ruler); - Console.WriteLine(new String(charArray)); - Console.WriteLine(); + // 2) Convert the input Byte array to a Char array, with newlines inserted. + charArrayLength = + Convert.ToBase64CharArray(byteArray1, 0, byteArray1.Length, + charArray, 0, Base64FormattingOptions.InsertLineBreaks); + Console.WriteLine("2) Convert the input Byte array to a Char array with newlines."); + Console.Write($" Output: A Char array (length = {charArrayLength}). "); + Console.WriteLine($"The elements of the array are:{nl}"); + Console.WriteLine(ruler); + Console.WriteLine(new string(charArray)); + Console.WriteLine(); -// 3) Convert the Char array back to a Byte array. - Console.WriteLine("3) Convert the Char array to an output Byte array."); - byteArray2 = Convert.FromBase64CharArray(charArray, 0, charArrayLength); + // 3) Convert the Char array back to a Byte array. + Console.WriteLine("3) Convert the Char array to an output Byte array."); + byteArray2 = Convert.FromBase64CharArray(charArray, 0, charArrayLength); -// 4) Are the input and output Byte arrays equivalent? - Console.WriteLine("4) The output Byte array is equal to the input Byte array: {0}", - ArraysAreEqual(byteArray1, byteArray2)); + // 4) Are the input and output Byte arrays equivalent? + Console.WriteLine($"4) The output Byte array is equal to the input Byte array: {ArraysAreEqual(byteArray1, byteArray2)}"); } public static bool ArraysAreEqual(byte[] a1, byte[] a2) { - if (a1.Length != a2.Length) return false; - for (int i = 0; i < a1.Length; i++) - if (a1[i] != a2[i]) return false; - return true; + if (a1.Length != a2.Length) return false; + for (int i = 0; i < a1.Length; i++) + if (a1[i] != a2[i]) return false; + return true; } } /* diff --git a/snippets/csharp/System/Convert/IsDBNull/Form1.cs b/snippets/csharp/System/Convert/IsDBNull/Form1.cs index 04c539969aa..da38e4fbcd6 100644 --- a/snippets/csharp/System/Convert/IsDBNull/Form1.cs +++ b/snippets/csharp/System/Convert/IsDBNull/Form1.cs @@ -1,102 +1,95 @@ using System; -using System.Collections.Generic; -using System.ComponentModel; using System.Data; -using Microsoft.Data.SqlClient; using System.Drawing; -using System.Linq; -using System.Text; using System.Windows.Forms; +using Microsoft.Data.SqlClient; namespace IsDBNull_To_NA_CS { - static class Program - { - /// - /// The main entry point for the application. - /// - [STAThread] - static void Main() - { - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new Form1()); - } - } - - public partial class Form1 : Form - { - private string connectionString = @"Data Source=RONPET59\SQLEXPRESS;Initial Catalog=SurveyDB;Integrated Security=True"; - - public Form1() - { - InitializeComponent(); - } - - private bool CompareForMissing(object value) - { - // - return DBNull.Value.Equals(value); - // - } - - // - private void Form1_Load(object sender, EventArgs e) - { - // Define ADO.NET objects. - SqlConnection conn = new SqlConnection(connectionString); - SqlCommand cmd = new SqlCommand(); - SqlDataReader dr; - - // Open connection, and retrieve dataset. - conn.Open(); - - // Define Command object. - cmd.CommandText = "Select * From Responses"; - cmd.CommandType = CommandType.Text; - cmd.Connection = conn; - - // Retrieve data reader. - dr = cmd.ExecuteReader(); - - int fieldCount = dr.FieldCount; - object[] fieldValues = new object[fieldCount]; - string[] headers = new string[fieldCount]; - - // Get names of fields. - for (int ctr = 0; ctr < fieldCount; ctr++) - headers[ctr] = dr.GetName(ctr); - - // Set up data grid. - this.grid.ColumnCount = fieldCount; - - this.grid.ColumnHeadersDefaultCellStyle.BackColor = Color.Navy; - this.grid.ColumnHeadersDefaultCellStyle.ForeColor = Color.White; - this.grid.ColumnHeadersDefaultCellStyle.Font = new Font(this.grid.Font, FontStyle.Bold); - - this.grid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders; - this.grid.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single; - this.grid.CellBorderStyle = DataGridViewCellBorderStyle.Single; - this.grid.GridColor = Color.Black; - this.grid.RowHeadersVisible = true; - - for (int columnNumber = 0; columnNumber < headers.Length; columnNumber++) - this.grid.Columns[columnNumber].Name = headers[columnNumber]; - - // Get data, replace missing values with "NA", and display it. - while (dr.Read()) - { - dr.GetValues(fieldValues); - - for (int fieldCounter = 0; fieldCounter < fieldCount; fieldCounter++) + static class Program + { + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new Form1()); + } + } + + public partial class Form1 : Form + { + private string connectionString = @"Data Source=RONPET59\SQLEXPRESS;Initial Catalog=SurveyDB;Integrated Security=True"; + + public Form1() => InitializeComponent(); + + private bool CompareForMissing(object value) + { + // + return DBNull.Value.Equals(value); + // + } + + // + private void Form1_Load(object sender, EventArgs e) + { + // Define ADO.NET objects. + SqlConnection conn = new SqlConnection(connectionString); + SqlCommand cmd = new SqlCommand(); + SqlDataReader dr; + + // Open connection, and retrieve dataset. + conn.Open(); + + // Define Command object. + cmd.CommandText = "Select * From Responses"; + cmd.CommandType = CommandType.Text; + cmd.Connection = conn; + + // Retrieve data reader. + dr = cmd.ExecuteReader(); + + int fieldCount = dr.FieldCount; + object[] fieldValues = new object[fieldCount]; + string[] headers = new string[fieldCount]; + + // Get names of fields. + for (int ctr = 0; ctr < fieldCount; ctr++) + headers[ctr] = dr.GetName(ctr); + + // Set up data grid. + this.grid.ColumnCount = fieldCount; + + this.grid.ColumnHeadersDefaultCellStyle.BackColor = Color.Navy; + this.grid.ColumnHeadersDefaultCellStyle.ForeColor = Color.White; + this.grid.ColumnHeadersDefaultCellStyle.Font = new(this.grid.Font, FontStyle.Bold); + + this.grid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders; + this.grid.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single; + this.grid.CellBorderStyle = DataGridViewCellBorderStyle.Single; + this.grid.GridColor = Color.Black; + this.grid.RowHeadersVisible = true; + + for (int columnNumber = 0; columnNumber < headers.Length; columnNumber++) + this.grid.Columns[columnNumber].Name = headers[columnNumber]; + + // Get data, replace missing values with "NA", and display it. + while (dr.Read()) { - if (Convert.IsDBNull(fieldValues[fieldCounter])) - fieldValues[fieldCounter] = "NA"; + dr.GetValues(fieldValues); + + for (int fieldCounter = 0; fieldCounter < fieldCount; fieldCounter++) + { + if (Convert.IsDBNull(fieldValues[fieldCounter])) + fieldValues[fieldCounter] = "NA"; + } + grid.Rows.Add(fieldValues); } - grid.Rows.Add(fieldValues); - } - dr.Close(); - } - // - } + dr.Close(); + } + // + } } diff --git a/snippets/csharp/System/Convert/Overview/NonDecimal1.cs b/snippets/csharp/System/Convert/Overview/NonDecimal1.cs index 98820cc225f..38b4220bba3 100644 --- a/snippets/csharp/System/Convert/Overview/NonDecimal1.cs +++ b/snippets/csharp/System/Convert/Overview/NonDecimal1.cs @@ -3,18 +3,18 @@ public class Example { - public static void Main() - { - int[] baseValues = { 2, 8, 10, 16 }; - short value = Int16.MaxValue; - foreach (var baseValue in baseValues) { - String s = Convert.ToString(value, baseValue); - short value2 = Convert.ToInt16(s, baseValue); + public static void Main() + { + int[] baseValues = { 2, 8, 10, 16 }; + short value = short.MaxValue; + foreach (int baseValue in baseValues) + { + string s = Convert.ToString(value, baseValue); + short value2 = Convert.ToInt16(s, baseValue); - Console.WriteLine("{0} --> {1} (base {2}) --> {3}", - value, s, baseValue, value2); - } - } + Console.WriteLine($"{value} --> {s} (base {baseValue}) --> {value2}"); + } + } } // The example displays the following output: // 32767 --> 111111111111111 (base 2) --> 32767 diff --git a/snippets/csharp/System/Convert/Overview/converter.cs b/snippets/csharp/System/Convert/Overview/converter.cs index 6b63efc93a5..c367ffec417 100644 --- a/snippets/csharp/System/Convert/Overview/converter.cs +++ b/snippets/csharp/System/Convert/Overview/converter.cs @@ -1,63 +1,71 @@ -using System; + -namespace ConvertSnippet +namespace ConvertSnippet { - //This class is the snippet for the class System.Convert - class Converter - { - static void Main(string[] args) - { - // - double dNumber = 23.15; + //This class is the snippet for the class System.Convert + class Converter + { + static void Main(string[] args) + { + // + double dNumber = 23.15; - try { - // Returns 23 - int iNumber = System.Convert.ToInt32(dNumber); - } - catch (System.OverflowException) { - System.Console.WriteLine( - "Overflow in double to int conversion."); - } - // Returns True - bool bNumber = System.Convert.ToBoolean(dNumber); - - // Returns "23.15" - string strNumber = System.Convert.ToString(dNumber); + try + { + // Returns 23 + int iNumber = System.Convert.ToInt32(dNumber); + } + catch (System.OverflowException) + { + System.Console.WriteLine( + "Overflow in double to int conversion."); + } + // Returns True + bool bNumber = System.Convert.ToBoolean(dNumber); - try { - // Returns '2' - char chrNumber = System.Convert.ToChar(strNumber[0]); - } - catch (System.ArgumentNullException) { - System.Console.WriteLine("String is null"); - } - catch (System.FormatException) { - System.Console.WriteLine("String length is greater than 1."); - } + // Returns "23.15" + string strNumber = System.Convert.ToString(dNumber); - // System.Console.ReadLine() returns a string and it - // must be converted. - int newInteger = 0; - try { - System.Console.WriteLine("Enter an integer:"); - newInteger = System.Convert.ToInt32( - System.Console.ReadLine()); - } - catch (System.ArgumentNullException) { - System.Console.WriteLine("String is null."); - } - catch (System.FormatException) { - System.Console.WriteLine("String does not consist of an " + - "optional sign followed by a series of digits."); - } - catch (System.OverflowException) { - System.Console.WriteLine( - "Overflow in string to int conversion."); - } + try + { + // Returns '2' + char chrNumber = System.Convert.ToChar(strNumber[0]); + } + catch (System.ArgumentNullException) + { + System.Console.WriteLine("String is null"); + } + catch (System.FormatException) + { + System.Console.WriteLine("String length is greater than 1."); + } - System.Console.WriteLine("Your integer as a double is {0}", - System.Convert.ToDouble(newInteger)); - // - } - } + // System.Console.ReadLine() returns a string and it + // must be converted. + int newInteger = 0; + try + { + System.Console.WriteLine("Enter an integer:"); + newInteger = System.Convert.ToInt32( + System.Console.ReadLine()); + } + catch (System.ArgumentNullException) + { + System.Console.WriteLine("String is null."); + } + catch (System.FormatException) + { + System.Console.WriteLine("String does not consist of an " + + "optional sign followed by a series of digits."); + } + catch (System.OverflowException) + { + System.Console.WriteLine( + "Overflow in string to int conversion."); + } + + System.Console.WriteLine($"Your integer as a double is {System.Convert.ToDouble(newInteger)}"); + // + } + } } diff --git a/snippets/csharp/System/Convert/ToBase64String/tb64s.cs b/snippets/csharp/System/Convert/ToBase64String/tb64s.cs index aba49aede33..2f5f2b25b7d 100644 --- a/snippets/csharp/System/Convert/ToBase64String/tb64s.cs +++ b/snippets/csharp/System/Convert/ToBase64String/tb64s.cs @@ -7,72 +7,72 @@ class Sample { public static void Main() { - byte[] inArray = new byte[256]; - byte[] outArray = new byte[256]; - string s2; - string s3; - string step1 = "1) The input is a byte array (inArray) of arbitrary data."; - string step2 = "2) Convert a subarray of the input data array to a base 64 string."; - string step3 = "3) Convert the entire input data array to a base 64 string."; - string step4 = "4) The two methods in steps 2 and 3 produce the same result: {0}"; - string step5 = "5) Convert the base 64 string to an output byte array (outArray)."; - string step6 = "6) The input and output arrays, inArray and outArray, are equal: {0}"; - int x; - string nl = Environment.NewLine; - string ruler1a = " 1 2 3 4"; - string ruler2a = "1234567890123456789012345678901234567890"; - string ruler3a = "----+----+----+----+----+----+----+----+"; - string ruler1b = " 5 6 7 "; - string ruler2b = "123456789012345678901234567890123456"; - string ruler3b = "----+----+----+----+----+----+----+-"; - string ruler = String.Concat(ruler1a, ruler1b, nl, - ruler2a, ruler2b, nl, - ruler3a, ruler3b, nl); - -// 1) Display an arbitrary array of input data (inArray). The data could be -// derived from user input, a file, an algorithm, etc. - - Console.WriteLine(step1); - Console.WriteLine(); - for (x = 0; x < inArray.Length; x++) + byte[] inArray = new byte[256]; + byte[] outArray = new byte[256]; + string s2; + string s3; + string step1 = "1) The input is a byte array (inArray) of arbitrary data."; + string step2 = "2) Convert a subarray of the input data array to a base 64 string."; + string step3 = "3) Convert the entire input data array to a base 64 string."; + string step4 = "4) The two methods in steps 2 and 3 produce the same result: {0}"; + string step5 = "5) Convert the base 64 string to an output byte array (outArray)."; + string step6 = "6) The input and output arrays, inArray and outArray, are equal: {0}"; + int x; + string nl = Environment.NewLine; + string ruler1a = " 1 2 3 4"; + string ruler2a = "1234567890123456789012345678901234567890"; + string ruler3a = "----+----+----+----+----+----+----+----+"; + string ruler1b = " 5 6 7 "; + string ruler2b = "123456789012345678901234567890123456"; + string ruler3b = "----+----+----+----+----+----+----+-"; + string ruler = string.Concat(ruler1a, ruler1b, nl, + ruler2a, ruler2b, nl, + ruler3a, ruler3b, nl); + + // 1) Display an arbitrary array of input data (inArray). The data could be + // derived from user input, a file, an algorithm, etc. + + Console.WriteLine(step1); + Console.WriteLine(); + for (x = 0; x < inArray.Length; x++) { - inArray[x] = (byte)x; - Console.Write("{0:X2} ", inArray[x]); - if (((x+1)%20) == 0) Console.WriteLine(); + inArray[x] = (byte)x; + Console.Write($"{inArray[x]:X2} "); + if (((x + 1) % 20) == 0) Console.WriteLine(); } - Console.Write("{0}{0}", nl); + Console.Write("{0}{0}", nl); -// 2) Convert a subarray of the input data to a base64 string. In this case, -// the subarray is the entire input data array. New lines (CRLF) are inserted. + // 2) Convert a subarray of the input data to a base64 string. In this case, + // the subarray is the entire input data array. New lines (CRLF) are inserted. - Console.WriteLine(step2); - s2 = Convert.ToBase64String(inArray, 0, inArray.Length, - Base64FormattingOptions.InsertLineBreaks); - Console.WriteLine("{0}{1}{2}{3}", nl, ruler, s2, nl); + Console.WriteLine(step2); + s2 = Convert.ToBase64String(inArray, 0, inArray.Length, + Base64FormattingOptions.InsertLineBreaks); + Console.WriteLine($"{nl}{ruler}{s2}{nl}"); -// 3) Convert the input data to a base64 string. In this case, the entire -// input data array is converted by default. New lines (CRLF) are inserted. + // 3) Convert the input data to a base64 string. In this case, the entire + // input data array is converted by default. New lines (CRLF) are inserted. - Console.WriteLine(step3); - s3 = Convert.ToBase64String(inArray, Base64FormattingOptions.InsertLineBreaks); + Console.WriteLine(step3); + s3 = Convert.ToBase64String(inArray, Base64FormattingOptions.InsertLineBreaks); -// 4) Test whether the methods in steps 2 and 3 produce the same result. - Console.WriteLine(step4, s2.Equals(s3)); + // 4) Test whether the methods in steps 2 and 3 produce the same result. + Console.WriteLine(step4, s2.Equals(s3)); -// 5) Convert the base 64 string to an output array (outArray). - Console.WriteLine(step5); - outArray = Convert.FromBase64String(s2); + // 5) Convert the base 64 string to an output array (outArray). + Console.WriteLine(step5); + outArray = Convert.FromBase64String(s2); -// 6) Is outArray equal to inArray? - Console.WriteLine(step6, ArraysAreEqual(inArray, outArray)); - } + // 6) Is outArray equal to inArray? + Console.WriteLine(step6, ArraysAreEqual(inArray, outArray)); + } public static bool ArraysAreEqual(byte[] a1, byte[] a2) { - if (a1.Length != a2.Length) return false; - for (int i = 0; i < a1.Length; i++) - if (a1[i] != a2[i]) return false; - return true; + if (a1.Length != a2.Length) return false; + for (int i = 0; i < a1.Length; i++) + if (a1[i] != a2[i]) return false; + return true; } } /* diff --git a/snippets/csharp/System/Convert/ToBoolean/ToBoolean1.cs b/snippets/csharp/System/Convert/ToBoolean/ToBoolean1.cs index f2150ebc941..6b84b5f1733 100644 --- a/snippets/csharp/System/Convert/ToBoolean/ToBoolean1.cs +++ b/snippets/csharp/System/Convert/ToBoolean/ToBoolean1.cs @@ -3,22 +3,22 @@ public class BooleanConversion { - public static void Main() - { - String[] values = { null, String.Empty, "true", "TrueString", + public static void Main() + { + string[] values = { null, string.Empty, "true", "TrueString", "False", " false ", "-1", "0" }; - foreach (var value in values) { - try - { - Console.WriteLine("Converted '{0}' to {1}.", value, - Convert.ToBoolean(value)); - } - catch (FormatException) - { - Console.WriteLine("Unable to convert '{0}' to a Boolean.", value); - } - } - } + foreach (string value in values) + { + try + { + Console.WriteLine($"Converted '{value}' to {Convert.ToBoolean(value)}."); + } + catch (FormatException) + { + Console.WriteLine($"Unable to convert '{value}' to a Boolean."); + } + } + } } // The example displays the following output: // Converted '' to False. diff --git a/snippets/csharp/System/Convert/ToBoolean/objectifp.cs b/snippets/csharp/System/Convert/ToBoolean/objectifp.cs index d6aa162ef58..5e3f7f50e27 100644 --- a/snippets/csharp/System/Convert/ToBoolean/objectifp.cs +++ b/snippets/csharp/System/Convert/ToBoolean/objectifp.cs @@ -20,16 +20,13 @@ public class AverageInfo : IFormatProvider protected AverageType AvgType; // Specify the type of averaging in the constructor. - public AverageInfo( AverageType avgType ) - { - this.AvgType = avgType; - } + public AverageInfo(AverageType avgType) => this.AvgType = avgType; // This method returns a reference to the containing object // if an object of AverageInfo type is requested. - public object GetFormat( Type argType ) + public object GetFormat(Type argType) { - if ( argType == typeof( AverageInfo ) ) + if (argType == typeof(AverageInfo)) return this; else return null; @@ -38,8 +35,8 @@ public object GetFormat( Type argType ) // Use this property to set or get the type of averaging. public AverageType TypeOfAverage { - get { return this.AvgType; } - set { this.AvgType = value; } + get => this.AvgType; + set => this.AvgType = value; } } @@ -49,91 +46,88 @@ public AverageType TypeOfAverage // arithmetic mean, geometric mean, or median. public class DataSet : IConvertible { - protected ArrayList data; - protected AverageInfo defaultProvider; + protected ArrayList data; + protected AverageInfo defaultProvider; // Construct the object and add an initial list of values. // Create a default format provider. - public DataSet( params double[ ] values ) + public DataSet(params double[] values) { - data = new ArrayList( values ); + data = new(values); defaultProvider = - new AverageInfo( AverageType.ArithmeticMean ); + new(AverageType.ArithmeticMean); } // Add additional values with this method. - public int Add( double value ) + public int Add(double value) { - data.Add( value ); + data.Add(value); return data.Count; } // Get, set, and add values with this indexer property. - public double this[ int index ] + public double this[int index] { get { - if( index >= 0 && index < data.Count ) - return (double)data[ index ]; + if (index >= 0 && index < data.Count) + return (double)data[index]; else throw new InvalidOperationException( - "[DataSet.get] Index out of range." ); + "[DataSet.get] Index out of range."); } set { - if( index >= 0 && index < data.Count ) - data[ index ] = value; + if (index >= 0 && index < data.Count) + data[index] = value; - else if( index == data.Count ) - data.Add( value ); + else if (index == data.Count) + data.Add(value); else throw new InvalidOperationException( - "[DataSet.set] Index out of range." ); + "[DataSet.set] Index out of range."); } } // This property returns the number of elements in the object. - public int Count - { - get { return data.Count; } - } + public int Count => data.Count; // This method calculates the average of the object's elements. - protected double Average( AverageType avgType ) + protected double Average(AverageType avgType) { - double SumProd; + double SumProd; - if( data.Count == 0 ) + if (data.Count == 0) return 0.0; - switch( avgType ) + switch (avgType) { case AverageType.GeometricMean: SumProd = 1.0; - for( int Index = 0; Index < data.Count; Index++ ) - SumProd *= (double)data[ Index ]; + for (int Index = 0; Index < data.Count; Index++) + SumProd *= (double)data[Index]; // This calculation will not fail with negative // elements. - return Math.Sign( SumProd ) * Math.Pow( - Math.Abs( SumProd ), 1.0 / data.Count ); + return Math.Sign(SumProd) * Math.Pow( + Math.Abs(SumProd), 1.0 / data.Count); case AverageType.ArithmeticMean: SumProd = 0.0; - for( int Index = 0; Index < data.Count; Index++ ) - SumProd += (double)data[ Index ]; + for (int Index = 0; Index < data.Count; Index++) + SumProd += (double)data[Index]; return SumProd / data.Count; case AverageType.Median: - if( data.Count % 2 == 0 ) - return ( (double)data[ data.Count / 2 ] + - (double)data[ data.Count / 2 - 1 ] ) / 2.0; + if (data.Count % 2 == 0) + return ((double)data[data.Count / 2] + + (double)data[data.Count / 2 - 1]) / 2.0; else - return (double)data[ data.Count / 2 ]; + return (double)data[data.Count / 2]; default: return 0.0; @@ -142,27 +136,27 @@ protected double Average( AverageType avgType ) // Get the AverageInfo object from the caller's format provider, // or use the local default. - protected AverageInfo GetAverageInfo( IFormatProvider provider ) + protected AverageInfo GetAverageInfo(IFormatProvider provider) { AverageInfo avgInfo = null; - if( provider != null ) + if (provider != null) avgInfo = (AverageInfo)provider.GetFormat( - typeof( AverageInfo ) ); + typeof(AverageInfo)); - if ( avgInfo == null ) + if (avgInfo == null) return defaultProvider; else return avgInfo; } // Calculate the average and limit the range. - protected double CalcNLimitAverage( double min, double max, - IFormatProvider provider ) + protected double CalcNLimitAverage(double min, double max, + IFormatProvider provider) { // Get the format provider and calculate the average. - AverageInfo avgInfo = GetAverageInfo( provider ); - double avg = Average( avgInfo.TypeOfAverage ); + AverageInfo avgInfo = GetAverageInfo(provider); + double avg = Average(avgInfo.TypeOfAverage); // Limit the range, based on the minimum and maximum values // for the type. @@ -174,252 +168,218 @@ protected double CalcNLimitAverage( double min, double max, // None of these conversion functions throw exceptions. When // the data is out of range for the type, the appropriate // MinValue or MaxValue is used. - public TypeCode GetTypeCode( ) - { - return TypeCode.Object; - } + public TypeCode GetTypeCode() => TypeCode.Object; - public bool ToBoolean( IFormatProvider provider ) + public bool ToBoolean(IFormatProvider provider) { // ToBoolean is false if the dataset is empty. - if( data.Count <= 0 ) + if (data.Count <= 0) { return false; } // For median averaging, ToBoolean is true if any // non-discarded elements are nonzero. - else if( AverageType.Median == - GetAverageInfo( provider ).TypeOfAverage ) + else if (AverageType.Median == + GetAverageInfo(provider).TypeOfAverage) { - if (data.Count % 2 == 0 ) - return ( (double)data[ data.Count / 2 ] != 0.0 || - (double)data[ data.Count / 2 - 1 ] != 0.0 ); + if (data.Count % 2 == 0) + return ((double)data[data.Count / 2] != 0.0 || + (double)data[data.Count / 2 - 1] != 0.0); else - return (double)data[ data.Count / 2 ] != 0.0; + return (double)data[data.Count / 2] != 0.0; } // For arithmetic or geometric mean averaging, ToBoolean is // true if any element of the dataset is nonzero. else { - for( int Index = 0; Index < data.Count; Index++ ) - if( (double)data[ Index ] != 0.0 ) + for (int Index = 0; Index < data.Count; Index++) + if ((double)data[Index] != 0.0) return true; return false; } } - public byte ToByte( IFormatProvider provider ) - { - return Convert.ToByte( CalcNLimitAverage( - Byte.MinValue, Byte.MaxValue, provider ) ); - } + public byte ToByte(IFormatProvider provider) => Convert.ToByte(CalcNLimitAverage( + byte.MinValue, byte.MaxValue, provider)); - public char ToChar( IFormatProvider provider ) - { - return Convert.ToChar( Convert.ToUInt16( CalcNLimitAverage( - Char.MinValue, Char.MaxValue, provider ) ) ); - } + public char ToChar(IFormatProvider provider) => Convert.ToChar(Convert.ToUInt16(CalcNLimitAverage( + char.MinValue, char.MaxValue, provider))); // Convert to DateTime by adding the calculated average as // seconds to the current date and time. A valid DateTime is // always returned. - public DateTime ToDateTime( IFormatProvider provider ) + public DateTime ToDateTime(IFormatProvider provider) { double seconds = - Average( GetAverageInfo( provider ).TypeOfAverage ); + Average(GetAverageInfo(provider).TypeOfAverage); try { - return DateTime.Now.AddSeconds( seconds ); + return DateTime.Now.AddSeconds(seconds); } - catch( ArgumentOutOfRangeException ) + catch (ArgumentOutOfRangeException) { return seconds < 0.0 ? DateTime.MinValue : DateTime.MaxValue; } } - public decimal ToDecimal( IFormatProvider provider ) + public decimal ToDecimal(IFormatProvider provider) { // The Double conversion rounds Decimal.MinValue and // Decimal.MaxValue to invalid Decimal values, so the // following limits must be used. - return Convert.ToDecimal( CalcNLimitAverage( + return Convert.ToDecimal(CalcNLimitAverage( -79228162514264330000000000000.0, - 79228162514264330000000000000.0, provider ) ); + 79228162514264330000000000000.0, provider)); } - public double ToDouble( IFormatProvider provider ) - { - return Average( GetAverageInfo(provider).TypeOfAverage ); - } + public double ToDouble(IFormatProvider provider) => Average(GetAverageInfo(provider).TypeOfAverage); - public short ToInt16( IFormatProvider provider ) - { - return Convert.ToInt16( CalcNLimitAverage( - Int16.MinValue, Int16.MaxValue, provider ) ); - } + public short ToInt16(IFormatProvider provider) => Convert.ToInt16(CalcNLimitAverage( + short.MinValue, short.MaxValue, provider)); - public int ToInt32( IFormatProvider provider ) - { - return Convert.ToInt32( CalcNLimitAverage( - Int32.MinValue, Int32.MaxValue, provider ) ); - } + public int ToInt32(IFormatProvider provider) => Convert.ToInt32(CalcNLimitAverage( + int.MinValue, int.MaxValue, provider)); - public long ToInt64( IFormatProvider provider ) + public long ToInt64(IFormatProvider provider) { // The Double conversion rounds Int64.MinValue and // Int64.MaxValue to invalid Int64 values, so the following // limits must be used. - return Convert.ToInt64( CalcNLimitAverage( - -9223372036854775000, 9223372036854775000, provider ) ); + return Convert.ToInt64(CalcNLimitAverage( + -9223372036854775000, 9223372036854775000, provider)); } - public SByte ToSByte( IFormatProvider provider ) - { - return Convert.ToSByte( CalcNLimitAverage( - SByte.MinValue, SByte.MaxValue, provider ) ); - } + public sbyte ToSByte(IFormatProvider provider) => Convert.ToSByte(CalcNLimitAverage( + sbyte.MinValue, sbyte.MaxValue, provider)); - public float ToSingle( IFormatProvider provider ) - { - return Convert.ToSingle( CalcNLimitAverage( - Single.MinValue, Single.MaxValue, provider ) ); - } + public float ToSingle(IFormatProvider provider) => Convert.ToSingle(CalcNLimitAverage( + float.MinValue, float.MaxValue, provider)); - public UInt16 ToUInt16( IFormatProvider provider ) - { - return Convert.ToUInt16( CalcNLimitAverage( - UInt16.MinValue, UInt16.MaxValue, provider ) ); - } + public ushort ToUInt16(IFormatProvider provider) => Convert.ToUInt16(CalcNLimitAverage( + ushort.MinValue, ushort.MaxValue, provider)); - public UInt32 ToUInt32( IFormatProvider provider ) - { - return Convert.ToUInt32( CalcNLimitAverage( - UInt32.MinValue, UInt32.MaxValue, provider ) ); - } + public uint ToUInt32(IFormatProvider provider) => Convert.ToUInt32(CalcNLimitAverage( + uint.MinValue, uint.MaxValue, provider)); - public UInt64 ToUInt64( IFormatProvider provider ) + public ulong ToUInt64(IFormatProvider provider) { // The Double conversion rounds UInt64.MaxValue to an invalid // UInt64 value, so the following limit must be used. - return Convert.ToUInt64( CalcNLimitAverage( - 0, 18446744073709550000.0, provider ) ); + return Convert.ToUInt64(CalcNLimitAverage( + 0, 18446744073709550000.0, provider)); } - public object ToType( Type conversionType, - IFormatProvider provider ) - { - return Convert.ChangeType( Average( - GetAverageInfo( provider ).TypeOfAverage ), - conversionType ); - } + public object ToType(Type conversionType, + IFormatProvider provider) => Convert.ChangeType(Average( + GetAverageInfo(provider).TypeOfAverage), + conversionType); - public string ToString( IFormatProvider provider ) + public string ToString(IFormatProvider provider) { - AverageType avgType = GetAverageInfo( provider ).TypeOfAverage; - return String.Format( "( {0}: {1:G10} )", avgType, - Average( avgType ) ); + AverageType avgType = GetAverageInfo(provider).TypeOfAverage; + return $"( {avgType}: {Average(avgType):G10} )"; } } class IConvertibleProviderDemo { // Display a DataSet with three different format providers. - public static void DisplayDataSet( DataSet ds ) + public static void DisplayDataSet(DataSet ds) { - string fmt = "{0,-12}{1,20}{2,20}{3,20}"; - AverageInfo median = new AverageInfo( AverageType.Median ); + string fmt = "{0,-12}{1,20}{2,20}{3,20}"; + AverageInfo median = new(AverageType.Median); AverageInfo geMean = - new AverageInfo( AverageType.GeometricMean ); + new(AverageType.GeometricMean); - // Display the dataset elements. - if( ds.Count > 0 ) + // Display the dataset elements. + if (ds.Count > 0) { - Console.Write( "\nDataSet: [{0}", ds[ 0 ] ); - for( int iX = 1; iX < ds.Count; iX++ ) - Console.Write( ", {0}", ds[ iX ] ); - Console.WriteLine( "]\n" ); + Console.Write($"\nDataSet: [{ds[0]}"); + for (int iX = 1; iX < ds.Count; iX++) + Console.Write($", {ds[iX]}"); + Console.WriteLine("]\n"); } - Console.WriteLine( fmt, "Convert.", "Default", + Console.WriteLine(fmt, "Convert.", "Default", "Geometric Mean", "Median"); - Console.WriteLine( fmt, "--------", "-------", + Console.WriteLine(fmt, "--------", "-------", "--------------", "------"); - Console.WriteLine( fmt, "ToBoolean", - Convert.ToBoolean( ds, null ), - Convert.ToBoolean( ds, geMean ), - Convert.ToBoolean( ds, median ) ); - Console.WriteLine( fmt, "ToByte", - Convert.ToByte( ds, null ), - Convert.ToByte( ds, geMean ), - Convert.ToByte( ds, median ) ); - Console.WriteLine( fmt, "ToChar", - Convert.ToChar( ds, null ), - Convert.ToChar( ds, geMean ), - Convert.ToChar( ds, median ) ); - Console.WriteLine( "{0,-12}{1,20:yyyy-MM-dd HH:mm:ss}" + + Console.WriteLine(fmt, "ToBoolean", + Convert.ToBoolean(ds, null), + Convert.ToBoolean(ds, geMean), + Convert.ToBoolean(ds, median)); + Console.WriteLine(fmt, "ToByte", + Convert.ToByte(ds, null), + Convert.ToByte(ds, geMean), + Convert.ToByte(ds, median)); + Console.WriteLine(fmt, "ToChar", + Convert.ToChar(ds, null), + Convert.ToChar(ds, geMean), + Convert.ToChar(ds, median)); + Console.WriteLine("{0,-12}{1,20:yyyy-MM-dd HH:mm:ss}" + "{2,20:yyyy-MM-dd HH:mm:ss}{3,20:yyyy-MM-dd HH:mm:ss}", - "ToDateTime", Convert.ToDateTime( ds, null ), - Convert.ToDateTime( ds, geMean ), - Convert.ToDateTime( ds, median ) ); - Console.WriteLine( fmt, "ToDecimal", - Convert.ToDecimal( ds, null ), - Convert.ToDecimal( ds, geMean ), - Convert.ToDecimal( ds, median ) ); - Console.WriteLine( fmt, "ToDouble", - Convert.ToDouble( ds, null ), - Convert.ToDouble( ds, geMean ), - Convert.ToDouble( ds, median ) ); - Console.WriteLine( fmt, "ToInt16", - Convert.ToInt16( ds, null ), - Convert.ToInt16( ds, geMean ), - Convert.ToInt16( ds, median ) ); - Console.WriteLine( fmt, "ToInt32", - Convert.ToInt32( ds, null ), - Convert.ToInt32( ds, geMean ), - Convert.ToInt32( ds, median ) ); - Console.WriteLine( fmt, "ToInt64", - Convert.ToInt64( ds, null ), - Convert.ToInt64( ds, geMean ), - Convert.ToInt64( ds, median ) ); - Console.WriteLine( fmt, "ToSByte", - Convert.ToSByte( ds, null ), - Convert.ToSByte( ds, geMean ), - Convert.ToSByte( ds, median ) ); - Console.WriteLine( fmt, "ToSingle", - Convert.ToSingle( ds, null ), - Convert.ToSingle( ds, geMean ), - Convert.ToSingle( ds, median ) ); - Console.WriteLine( fmt, "ToUInt16", - Convert.ToUInt16( ds, null ), - Convert.ToUInt16( ds, geMean ), - Convert.ToUInt16( ds, median ) ); - Console.WriteLine( fmt, "ToUInt32", - Convert.ToUInt32( ds, null ), - Convert.ToUInt32( ds, geMean ), - Convert.ToUInt32( ds, median ) ); - Console.WriteLine( fmt, "ToUInt64", - Convert.ToUInt64( ds, null ), - Convert.ToUInt64( ds, geMean ), - Convert.ToUInt64( ds, median ) ); + "ToDateTime", Convert.ToDateTime(ds, null), + Convert.ToDateTime(ds, geMean), + Convert.ToDateTime(ds, median)); + Console.WriteLine(fmt, "ToDecimal", + Convert.ToDecimal(ds, null), + Convert.ToDecimal(ds, geMean), + Convert.ToDecimal(ds, median)); + Console.WriteLine(fmt, "ToDouble", + Convert.ToDouble(ds, null), + Convert.ToDouble(ds, geMean), + Convert.ToDouble(ds, median)); + Console.WriteLine(fmt, "ToInt16", + Convert.ToInt16(ds, null), + Convert.ToInt16(ds, geMean), + Convert.ToInt16(ds, median)); + Console.WriteLine(fmt, "ToInt32", + Convert.ToInt32(ds, null), + Convert.ToInt32(ds, geMean), + Convert.ToInt32(ds, median)); + Console.WriteLine(fmt, "ToInt64", + Convert.ToInt64(ds, null), + Convert.ToInt64(ds, geMean), + Convert.ToInt64(ds, median)); + Console.WriteLine(fmt, "ToSByte", + Convert.ToSByte(ds, null), + Convert.ToSByte(ds, geMean), + Convert.ToSByte(ds, median)); + Console.WriteLine(fmt, "ToSingle", + Convert.ToSingle(ds, null), + Convert.ToSingle(ds, geMean), + Convert.ToSingle(ds, median)); + Console.WriteLine(fmt, "ToUInt16", + Convert.ToUInt16(ds, null), + Convert.ToUInt16(ds, geMean), + Convert.ToUInt16(ds, median)); + Console.WriteLine(fmt, "ToUInt32", + Convert.ToUInt32(ds, null), + Convert.ToUInt32(ds, geMean), + Convert.ToUInt32(ds, median)); + Console.WriteLine(fmt, "ToUInt64", + Convert.ToUInt64(ds, null), + Convert.ToUInt64(ds, geMean), + Convert.ToUInt64(ds, median)); } - public static void Main( ) + public static void Main() { - Console.WriteLine( "This example of " + + Console.WriteLine("This example of " + "the Convert.To( object, IFormatProvider ) methods " + "\ngenerates the following output. The example " + "displays the values \nreturned by the methods, " + - "using several IFormatProvider objects.\n" ); + "using several IFormatProvider objects.\n"); - DataSet ds1 = new DataSet( - 10.5, 22.2, 45.9, 88.7, 156.05, 297.6 ); - DisplayDataSet( ds1 ); + DataSet ds1 = new( + 10.5, 22.2, 45.9, 88.7, 156.05, 297.6); + DisplayDataSet(ds1); - DataSet ds2 = new DataSet( - 359999.95, 425000, 499999.5, 775000, 1695000 ); - DisplayDataSet( ds2 ); + DataSet ds2 = new( + 359999.95, 425000, 499999.5, 775000, 1695000); + DisplayDataSet(ds2); } } diff --git a/snippets/csharp/System/Convert/ToBoolean/system.convert snippet.cs b/snippets/csharp/System/Convert/ToBoolean/system.convert snippet.cs index 343d3880bc4..bcf2779eea3 100644 --- a/snippets/csharp/System/Convert/ToBoolean/system.convert snippet.cs +++ b/snippets/csharp/System/Convert/ToBoolean/system.convert snippet.cs @@ -1,528 +1,565 @@ using System; -namespace BasicSnippetC { - - class ConvertSnippet { - - static void Main(string[] args) { - - ConvertSnippet snippet = new ConvertSnippet(); - - double doubleVal; - System.Console.WriteLine("Enter the double value: "); - doubleVal = System.Convert.ToDouble(System.Console.ReadLine()); - snippet.ConvertDoubles(doubleVal); - - long longVal; - System.Console.WriteLine("Enter the Int64 value: "); - longVal = System.Convert.ToInt64(System.Console.ReadLine()); - snippet.ConvertLongs(longVal); - - string stringVal; - System.Console.WriteLine("Enter the String value: "); - stringVal = System.Console.ReadLine(); - snippet.ConvertStrings(stringVal); - - char charVal; - System.Console.WriteLine("Enter the char value: "); - charVal = System.Convert.ToChar(System.Console.ReadLine()); - snippet.ConvertChars(charVal); - - byte byteVal; - System.Console.WriteLine("Enter the byte value: "); - byteVal = System.Convert.ToByte(System.Console.ReadLine()); - snippet.ConvertBytes(byteVal); - - snippet.ConvertBoolean(); - } - - public void ConvertDoubles(double doubleVal) { - ConvertDoubleBool(doubleVal); - ConvertDoubleByte(doubleVal); - ConvertDoubleInt(doubleVal); - ConvertDoubleDecimal((decimal) doubleVal); - CovertDoubleFloat(doubleVal); - ConvertDoubleString(doubleVal); - } - - public void ConvertLongs(long longVal) { - ConvertLongChar(longVal); - ConvertLongByte(longVal); - ConvertLongDecimal(longVal); - ConvertLongFloat(longVal); - } - - public void ConvertStrings(string stringVal) { - ConvertStringBoolean(stringVal); - ConvertStringByte(stringVal); - ConvertStringChar(stringVal); - ConvertStringDecimal(stringVal); - ConvertStringFloat(stringVal); - } - - public void ConvertChars(char charVal) { - ConvertCharDecimal(charVal); - } - - public void ConvertBytes(byte byteVal) { - ConvertByteDecimal(byteVal); - ConvertByteSingle(byteVal); - } - - // - public void ConvertDoubleBool(double doubleVal) { - bool boolVal; - // Double to bool conversion cannot overflow. - boolVal = System.Convert.ToBoolean(doubleVal); - System.Console.WriteLine("{0} as a Boolean is: {1}.", - doubleVal, boolVal); - - // bool to double conversion cannot overflow. - doubleVal = System.Convert.ToDouble(boolVal); - System.Console.WriteLine("{0} as a double is: {1}.", - boolVal, doubleVal); - } - // - - // - public void ConvertDoubleByte(double doubleVal) { - byte byteVal = 0; - - // Double to byte conversion can overflow. - try { - byteVal = System.Convert.ToByte(doubleVal); - System.Console.WriteLine("{0} as a byte is: {1}.", - doubleVal, byteVal); - } - catch (System.OverflowException) { - System.Console.WriteLine( - "Overflow in double-to-byte conversion."); - } - - // Byte to double conversion cannot overflow. - doubleVal = System.Convert.ToDouble(byteVal); - System.Console.WriteLine("{0} as a double is: {1}.", - byteVal, doubleVal); - } - // - - // - public void ConvertDoubleInt(double doubleVal) { - - int intVal = 0; - // Double to int conversion can overflow. - try { - intVal = System.Convert.ToInt32(doubleVal); - System.Console.WriteLine("{0} as an int is: {1}", - doubleVal, intVal); - } - catch (System.OverflowException) { - System.Console.WriteLine( - "Overflow in double-to-int conversion."); - } - - // Int to double conversion cannot overflow. - doubleVal = System.Convert.ToDouble(intVal); - System.Console.WriteLine("{0} as a double is: {1}", - intVal, doubleVal); - } - // - - // - public void ConvertDoubleDecimal(decimal decimalVal){ - - double doubleVal; - - // Decimal to double conversion cannot overflow. - doubleVal = System.Convert.ToDouble(decimalVal); - System.Console.WriteLine("{0} as a double is: {1}", - decimalVal, doubleVal); - - // Conversion from double to decimal can overflow. - try - { - decimalVal = System.Convert.ToDecimal(doubleVal); - System.Console.WriteLine ("{0} as a decimal is: {1}", - doubleVal, decimalVal); - } - catch (System.OverflowException) { - System.Console.WriteLine( - "Overflow in double-to-double conversion."); - } - } - // - - // - public void CovertDoubleFloat(double doubleVal) { - float floatVal = 0; - - // Double to float conversion cannot overflow. - floatVal = System.Convert.ToSingle(doubleVal); - System.Console.WriteLine("{0} as a float is {1}", - doubleVal, floatVal); - - // Conversion from float to double cannot overflow. - doubleVal = System.Convert.ToDouble(floatVal); - System.Console.WriteLine("{0} as a double is: {1}", - floatVal, doubleVal); - } - // - - // - public void ConvertDoubleString(double doubleVal) { - - string stringVal; - - // A conversion from Double to string cannot overflow. - stringVal = System.Convert.ToString(doubleVal); - System.Console.WriteLine("{0} as a string is: {1}", - doubleVal, stringVal); - - try { - doubleVal = System.Convert.ToDouble(stringVal); - System.Console.WriteLine("{0} as a double is: {1}", - stringVal, doubleVal); - } - catch (System.OverflowException) { - System.Console.WriteLine( - "Conversion from string-to-double overflowed."); - } - catch (System.FormatException) { - System.Console.WriteLine( - "The string was not formatted as a double."); - } - catch (System.ArgumentException) { - System.Console.WriteLine( - "The string pointed to null."); - } - } - // - - // - public void ConvertLongChar(long longVal) { - - char charVal = 'a'; - - try { - charVal = System.Convert.ToChar(longVal); - System.Console.WriteLine("{0} as a char is {1}", - longVal, charVal); - } - catch (System.OverflowException) { - System.Console.WriteLine( - "Overflow in long-to-char conversion."); - } - - // A conversion from Char to long cannot overflow. - longVal = System.Convert.ToInt64(charVal); - System.Console.WriteLine("{0} as an Int64 is {1}", - charVal, longVal); - } - // - - // - public void ConvertLongByte(long longVal) { - - byte byteVal = 0; - - // A conversion from Long to byte can overflow. - try { - byteVal = System.Convert.ToByte(longVal); - System.Console.WriteLine("{0} as a byte is {1}", - longVal, byteVal); - } - catch (System.OverflowException) { - System.Console.WriteLine( - "Overflow in long-to-byte conversion."); - } - - // A conversion from Byte to long cannot overflow. - longVal = System.Convert.ToInt64(byteVal); - System.Console.WriteLine("{0} as an Int64 is {1}", - byteVal, longVal); - } - // - - // - public void ConvertLongDecimal(long longVal) { - - decimal decimalVal; - - // Long to decimal conversion cannot overflow. - decimalVal = System.Convert.ToDecimal(longVal); - System.Console.WriteLine("{0} as a decimal is {1}", - longVal, decimalVal); - - // Decimal to long conversion can overflow. - try { - longVal = System.Convert.ToInt64(decimalVal); - System.Console.WriteLine("{0} as a long is {1}", - decimalVal, longVal); - } - catch (System.OverflowException) { - System.Console.WriteLine( - "Overflow in decimal-to-long conversion."); - } - } - // - - // - public void ConvertLongFloat(long longVal) { - - float floatVal; - - // A conversion from Long to float cannot overflow. - floatVal = System.Convert.ToSingle(longVal); - System.Console.WriteLine("{0} as a float is {1}", - longVal, floatVal); - - // A conversion from float to long can overflow. - try { - longVal = System.Convert.ToInt64(floatVal); - System.Console.WriteLine("{0} as a long is {1}", - floatVal, longVal); - } - catch (System.OverflowException) { - System.Console.WriteLine( - "Overflow in float-to-long conversion."); - } - } - // - - // - public void ConvertStringBoolean(string stringVal) { - - bool boolVal = false; - - try { - boolVal = System.Convert.ToBoolean(stringVal); - if (boolVal) { - System.Console.WriteLine( - "String was equal to System.Boolean.TrueString."); - } - else { - System.Console.WriteLine( - "String was equal to System.Boolean.FalseString."); - } - } - catch (System.FormatException){ - System.Console.WriteLine( - "The string must equal System.Boolean.TrueString " + - "or System.Boolean.FalseString."); - } - - // A conversion from bool to string will always succeed. - stringVal = System.Convert.ToString(boolVal); - System.Console.WriteLine("{0} as a string is {1}", - boolVal, stringVal); - } - // - - // - public void ConvertStringByte(string stringVal) { - byte byteVal = 0; - - try { - byteVal = System.Convert.ToByte(stringVal); - System.Console.WriteLine("{0} as a byte is: {1}", - stringVal, byteVal); - } - catch (System.OverflowException) { - System.Console.WriteLine( - "Conversion from string to byte overflowed."); - } - catch (System.FormatException) { - System.Console.WriteLine( - "The string is not formatted as a byte."); - } - catch (System.ArgumentNullException) { - System.Console.WriteLine( - "The string is null."); - } - - //The conversion from byte to string is always valid. - stringVal = System.Convert.ToString(byteVal); - System.Console.WriteLine("{0} as a string is {1}", - byteVal, stringVal); - } - // - - // - public void ConvertStringChar(string stringVal) { - char charVal = 'a'; - - // A string must be one character long to convert to char. - try { - charVal = System.Convert.ToChar(stringVal); - System.Console.WriteLine("{0} as a char is {1}", - stringVal, charVal); - } - catch (System.FormatException) { - System.Console.WriteLine( - "The string is longer than one character."); - } - catch (System.ArgumentNullException) { - System.Console.WriteLine("The string is null."); - } - - // A char to string conversion will always succeed. - stringVal = System.Convert.ToString(charVal); - System.Console.WriteLine("The character as a string is {0}", - stringVal); - } - // - - // - public void ConvertStringDecimal(string stringVal) { - decimal decimalVal = 0; - - try { - decimalVal = System.Convert.ToDecimal(stringVal); - System.Console.WriteLine( - "The string as a decimal is {0}.", decimalVal); - } - catch (System.OverflowException){ - System.Console.WriteLine( - "The conversion from string to decimal overflowed."); - } - catch (System.FormatException) { - System.Console.WriteLine( - "The string is not formatted as a decimal."); - } - catch (System.ArgumentNullException) { - System.Console.WriteLine( - "The string is null."); - } - - // Decimal to string conversion will not overflow. - stringVal = System.Convert.ToString(decimalVal); - System.Console.WriteLine( - "The decimal as a string is {0}.", stringVal); - } - // - - // - public void ConvertStringFloat(string stringVal) { - float floatVal = 0; - - try { - floatVal = System.Convert.ToSingle(stringVal); - System.Console.WriteLine( - "The string as a float is {0}.", floatVal); - } - catch (System.OverflowException){ - System.Console.WriteLine( - "The conversion from string-to-float overflowed."); - } - catch (System.FormatException) { - System.Console.WriteLine( - "The string is not formatted as a float."); - } - catch (System.ArgumentNullException) { - System.Console.WriteLine( - "The string is null."); - } - - // Float to string conversion will not overflow. - stringVal = System.Convert.ToString(floatVal); - System.Console.WriteLine( - "The float as a string is {0}.", stringVal); - } - // - - // - public void ConvertCharDecimal(char charVal) { - Decimal decimalVal = 0; - - // Char to decimal conversion is not supported and will always - // throw an InvalidCastException. - try { - decimalVal = System.Convert.ToDecimal(charVal); - } - catch (System.InvalidCastException) { - System.Console.WriteLine( - "Char-to-Decimal conversion is not supported by .NET."); - } - - //Decimal to char conversion is also not supported. - try { - charVal = System.Convert.ToChar(decimalVal); - } - catch (System.InvalidCastException) { - System.Console.WriteLine( - "Decimal-to-Char conversion is not supported by .NET."); - } - } - // - - // - public void ConvertByteDecimal(byte byteVal) { - decimal decimalVal; - - // Byte to decimal conversion will not overflow. - decimalVal = System.Convert.ToDecimal(byteVal); - System.Console.WriteLine("The byte as a decimal is {0}.", - decimalVal); - - // Decimal to byte conversion can overflow. - try { - byteVal = System.Convert.ToByte(decimalVal); - System.Console.WriteLine("The Decimal as a byte is {0}.", - byteVal); - } - catch (System.OverflowException) { - System.Console.WriteLine( - "The decimal value is too large for a byte."); - } - } - // - - // - public void ConvertByteSingle(byte byteVal) { - float floatVal; - - // Byte to float conversion will not overflow. - floatVal = System.Convert.ToSingle(byteVal); - System.Console.WriteLine("The byte as a float is {0}.", - floatVal); - - // Float to byte conversion can overflow. - try { - byteVal = System.Convert.ToByte(floatVal); - System.Console.WriteLine("The float as a byte is {0}.", - byteVal); - } - catch (System.OverflowException) { - System.Console.WriteLine( - "The float value is too large for a byte."); - } - } - // - - // - public void ConvertBoolean() { - const int year = 1979; - const int month = 7; - const int day = 28; - const int hour = 13; - const int minute = 26; - const int second = 15; - const int millisecond = 53; - - DateTime dateTime = new DateTime(year, month, day, hour, - minute, second, millisecond); - - bool boolVal; - - // System.InvalidCastException is always thrown. - try { - boolVal = System.Convert.ToBoolean(dateTime); - } - catch (System.InvalidCastException) { - System.Console.WriteLine("Conversion from DateTime to " + - "Boolean is not supported by .NET."); - } - } - // - } +namespace BasicSnippetC +{ + + class ConvertSnippet + { + + static void Main(string[] args) + { + + ConvertSnippet snippet = new(); + + double doubleVal; + System.Console.WriteLine("Enter the double value: "); + doubleVal = System.Convert.ToDouble(System.Console.ReadLine()); + snippet.ConvertDoubles(doubleVal); + + long longVal; + System.Console.WriteLine("Enter the Int64 value: "); + longVal = System.Convert.ToInt64(System.Console.ReadLine()); + snippet.ConvertLongs(longVal); + + string stringVal; + System.Console.WriteLine("Enter the String value: "); + stringVal = System.Console.ReadLine(); + snippet.ConvertStrings(stringVal); + + char charVal; + System.Console.WriteLine("Enter the char value: "); + charVal = System.Convert.ToChar(System.Console.ReadLine()); + snippet.ConvertChars(charVal); + + byte byteVal; + System.Console.WriteLine("Enter the byte value: "); + byteVal = System.Convert.ToByte(System.Console.ReadLine()); + snippet.ConvertBytes(byteVal); + + snippet.ConvertBoolean(); + } + + public void ConvertDoubles(double doubleVal) + { + ConvertDoubleBool(doubleVal); + ConvertDoubleByte(doubleVal); + ConvertDoubleInt(doubleVal); + ConvertDoubleDecimal((decimal)doubleVal); + CovertDoubleFloat(doubleVal); + ConvertDoubleString(doubleVal); + } + + public void ConvertLongs(long longVal) + { + ConvertLongChar(longVal); + ConvertLongByte(longVal); + ConvertLongDecimal(longVal); + ConvertLongFloat(longVal); + } + + public void ConvertStrings(string stringVal) + { + ConvertStringBoolean(stringVal); + ConvertStringByte(stringVal); + ConvertStringChar(stringVal); + ConvertStringDecimal(stringVal); + ConvertStringFloat(stringVal); + } + + public void ConvertChars(char charVal) => ConvertCharDecimal(charVal); + + public void ConvertBytes(byte byteVal) + { + ConvertByteDecimal(byteVal); + ConvertByteSingle(byteVal); + } + + // + public void ConvertDoubleBool(double doubleVal) + { + bool boolVal; + // Double to bool conversion cannot overflow. + boolVal = System.Convert.ToBoolean(doubleVal); + System.Console.WriteLine($"{doubleVal} as a Boolean is: {boolVal}."); + + // bool to double conversion cannot overflow. + doubleVal = System.Convert.ToDouble(boolVal); + System.Console.WriteLine($"{boolVal} as a double is: {doubleVal}."); + } + // + + // + public void ConvertDoubleByte(double doubleVal) + { + byte byteVal = 0; + + // Double to byte conversion can overflow. + try + { + byteVal = System.Convert.ToByte(doubleVal); + System.Console.WriteLine($"{doubleVal} as a byte is: {byteVal}."); + } + catch (System.OverflowException) + { + System.Console.WriteLine( + "Overflow in double-to-byte conversion."); + } + + // Byte to double conversion cannot overflow. + doubleVal = System.Convert.ToDouble(byteVal); + System.Console.WriteLine($"{byteVal} as a double is: {doubleVal}."); + } + // + + // + public void ConvertDoubleInt(double doubleVal) + { + + int intVal = 0; + // Double to int conversion can overflow. + try + { + intVal = System.Convert.ToInt32(doubleVal); + System.Console.WriteLine($"{doubleVal} as an int is: {intVal}"); + } + catch (System.OverflowException) + { + System.Console.WriteLine( + "Overflow in double-to-int conversion."); + } + + // Int to double conversion cannot overflow. + doubleVal = System.Convert.ToDouble(intVal); + System.Console.WriteLine($"{intVal} as a double is: {doubleVal}"); + } + // + + // + public void ConvertDoubleDecimal(decimal decimalVal) + { + + double doubleVal; + + // Decimal to double conversion cannot overflow. + doubleVal = System.Convert.ToDouble(decimalVal); + System.Console.WriteLine($"{decimalVal} as a double is: {doubleVal}"); + + // Conversion from double to decimal can overflow. + try + { + decimalVal = System.Convert.ToDecimal(doubleVal); + System.Console.WriteLine($"{doubleVal} as a decimal is: {decimalVal}"); + } + catch (System.OverflowException) + { + System.Console.WriteLine( + "Overflow in double-to-double conversion."); + } + } + // + + // + public void CovertDoubleFloat(double doubleVal) + { + float floatVal = 0; + + // Double to float conversion cannot overflow. + floatVal = System.Convert.ToSingle(doubleVal); + System.Console.WriteLine($"{doubleVal} as a float is {floatVal}"); + + // Conversion from float to double cannot overflow. + doubleVal = System.Convert.ToDouble(floatVal); + System.Console.WriteLine($"{floatVal} as a double is: {doubleVal}"); + } + // + + // + public void ConvertDoubleString(double doubleVal) + { + + string stringVal; + + // A conversion from Double to string cannot overflow. + stringVal = System.Convert.ToString(doubleVal); + System.Console.WriteLine($"{doubleVal} as a string is: {stringVal}"); + + try + { + doubleVal = System.Convert.ToDouble(stringVal); + System.Console.WriteLine($"{stringVal} as a double is: {doubleVal}"); + } + catch (System.OverflowException) + { + System.Console.WriteLine( + "Conversion from string-to-double overflowed."); + } + catch (System.FormatException) + { + System.Console.WriteLine( + "The string was not formatted as a double."); + } + catch (System.ArgumentException) + { + System.Console.WriteLine( + "The string pointed to null."); + } + } + // + + // + public void ConvertLongChar(long longVal) + { + + char charVal = 'a'; + + try + { + charVal = System.Convert.ToChar(longVal); + System.Console.WriteLine($"{longVal} as a char is {charVal}"); + } + catch (System.OverflowException) + { + System.Console.WriteLine( + "Overflow in long-to-char conversion."); + } + + // A conversion from Char to long cannot overflow. + longVal = System.Convert.ToInt64(charVal); + System.Console.WriteLine($"{charVal} as an Int64 is {longVal}"); + } + // + + // + public void ConvertLongByte(long longVal) + { + + byte byteVal = 0; + + // A conversion from Long to byte can overflow. + try + { + byteVal = System.Convert.ToByte(longVal); + System.Console.WriteLine($"{longVal} as a byte is {byteVal}"); + } + catch (System.OverflowException) + { + System.Console.WriteLine( + "Overflow in long-to-byte conversion."); + } + + // A conversion from Byte to long cannot overflow. + longVal = System.Convert.ToInt64(byteVal); + System.Console.WriteLine($"{byteVal} as an Int64 is {longVal}"); + } + // + + // + public void ConvertLongDecimal(long longVal) + { + + decimal decimalVal; + + // Long to decimal conversion cannot overflow. + decimalVal = System.Convert.ToDecimal(longVal); + System.Console.WriteLine($"{longVal} as a decimal is {decimalVal}"); + + // Decimal to long conversion can overflow. + try + { + longVal = System.Convert.ToInt64(decimalVal); + System.Console.WriteLine($"{decimalVal} as a long is {longVal}"); + } + catch (System.OverflowException) + { + System.Console.WriteLine( + "Overflow in decimal-to-long conversion."); + } + } + // + + // + public void ConvertLongFloat(long longVal) + { + + float floatVal; + + // A conversion from Long to float cannot overflow. + floatVal = System.Convert.ToSingle(longVal); + System.Console.WriteLine($"{longVal} as a float is {floatVal}"); + + // A conversion from float to long can overflow. + try + { + longVal = System.Convert.ToInt64(floatVal); + System.Console.WriteLine($"{floatVal} as a long is {longVal}"); + } + catch (System.OverflowException) + { + System.Console.WriteLine( + "Overflow in float-to-long conversion."); + } + } + // + + // + public void ConvertStringBoolean(string stringVal) + { + + bool boolVal = false; + + try + { + boolVal = System.Convert.ToBoolean(stringVal); + if (boolVal) + { + System.Console.WriteLine( + "String was equal to System.Boolean.TrueString."); + } + else + { + System.Console.WriteLine( + "String was equal to System.Boolean.FalseString."); + } + } + catch (System.FormatException) + { + System.Console.WriteLine( + "The string must equal System.Boolean.TrueString " + + "or System.Boolean.FalseString."); + } + + // A conversion from bool to string will always succeed. + stringVal = System.Convert.ToString(boolVal); + System.Console.WriteLine($"{boolVal} as a string is {stringVal}"); + } + // + + // + public void ConvertStringByte(string stringVal) + { + byte byteVal = 0; + + try + { + byteVal = System.Convert.ToByte(stringVal); + System.Console.WriteLine($"{stringVal} as a byte is: {byteVal}"); + } + catch (System.OverflowException) + { + System.Console.WriteLine( + "Conversion from string to byte overflowed."); + } + catch (System.FormatException) + { + System.Console.WriteLine( + "The string is not formatted as a byte."); + } + catch (System.ArgumentNullException) + { + System.Console.WriteLine( + "The string is null."); + } + + //The conversion from byte to string is always valid. + stringVal = System.Convert.ToString(byteVal); + System.Console.WriteLine($"{byteVal} as a string is {stringVal}"); + } + // + + // + public void ConvertStringChar(string stringVal) + { + char charVal = 'a'; + + // A string must be one character long to convert to char. + try + { + charVal = System.Convert.ToChar(stringVal); + System.Console.WriteLine($"{stringVal} as a char is {charVal}"); + } + catch (System.FormatException) + { + System.Console.WriteLine( + "The string is longer than one character."); + } + catch (System.ArgumentNullException) + { + System.Console.WriteLine("The string is null."); + } + + // A char to string conversion will always succeed. + stringVal = System.Convert.ToString(charVal); + System.Console.WriteLine($"The character as a string is {stringVal}"); + } + // + + // + public void ConvertStringDecimal(string stringVal) + { + decimal decimalVal = 0; + + try + { + decimalVal = System.Convert.ToDecimal(stringVal); + System.Console.WriteLine($"The string as a decimal is {decimalVal}."); + } + catch (System.OverflowException) + { + System.Console.WriteLine( + "The conversion from string to decimal overflowed."); + } + catch (System.FormatException) + { + System.Console.WriteLine( + "The string is not formatted as a decimal."); + } + catch (System.ArgumentNullException) + { + System.Console.WriteLine( + "The string is null."); + } + + // Decimal to string conversion will not overflow. + stringVal = System.Convert.ToString(decimalVal); + System.Console.WriteLine($"The decimal as a string is {stringVal}."); + } + // + + // + public void ConvertStringFloat(string stringVal) + { + float floatVal = 0; + + try + { + floatVal = System.Convert.ToSingle(stringVal); + System.Console.WriteLine($"The string as a float is {floatVal}."); + } + catch (System.OverflowException) + { + System.Console.WriteLine( + "The conversion from string-to-float overflowed."); + } + catch (System.FormatException) + { + System.Console.WriteLine( + "The string is not formatted as a float."); + } + catch (System.ArgumentNullException) + { + System.Console.WriteLine( + "The string is null."); + } + + // Float to string conversion will not overflow. + stringVal = System.Convert.ToString(floatVal); + System.Console.WriteLine($"The float as a string is {stringVal}."); + } + // + + // + public void ConvertCharDecimal(char charVal) + { + decimal decimalVal = 0; + + // Char to decimal conversion is not supported and will always + // throw an InvalidCastException. + try + { + decimalVal = System.Convert.ToDecimal(charVal); + } + catch (System.InvalidCastException) + { + System.Console.WriteLine( + "Char-to-Decimal conversion is not supported by .NET."); + } + + //Decimal to char conversion is also not supported. + try + { + charVal = System.Convert.ToChar(decimalVal); + } + catch (System.InvalidCastException) + { + System.Console.WriteLine( + "Decimal-to-Char conversion is not supported by .NET."); + } + } + // + + // + public void ConvertByteDecimal(byte byteVal) + { + decimal decimalVal; + + // Byte to decimal conversion will not overflow. + decimalVal = System.Convert.ToDecimal(byteVal); + System.Console.WriteLine($"The byte as a decimal is {decimalVal}."); + + // Decimal to byte conversion can overflow. + try + { + byteVal = System.Convert.ToByte(decimalVal); + System.Console.WriteLine($"The Decimal as a byte is {byteVal}."); + } + catch (System.OverflowException) + { + System.Console.WriteLine( + "The decimal value is too large for a byte."); + } + } + // + + // + public void ConvertByteSingle(byte byteVal) + { + float floatVal; + + // Byte to float conversion will not overflow. + floatVal = System.Convert.ToSingle(byteVal); + System.Console.WriteLine($"The byte as a float is {floatVal}."); + + // Float to byte conversion can overflow. + try + { + byteVal = System.Convert.ToByte(floatVal); + System.Console.WriteLine($"The float as a byte is {byteVal}."); + } + catch (System.OverflowException) + { + System.Console.WriteLine( + "The float value is too large for a byte."); + } + } + // + + // + public void ConvertBoolean() + { + const int year = 1979; + const int month = 7; + const int day = 28; + const int hour = 13; + const int minute = 26; + const int second = 15; + const int millisecond = 53; + + DateTime dateTime = new(year, month, day, hour, + minute, second, millisecond); + + bool boolVal; + + // System.InvalidCastException is always thrown. + try + { + boolVal = System.Convert.ToBoolean(dateTime); + } + catch (System.InvalidCastException) + { + System.Console.WriteLine("Conversion from DateTime to " + + "Boolean is not supported by .NET."); + } + } + // + } } diff --git a/snippets/csharp/System/Convert/ToBoolean/toboolean2.cs b/snippets/csharp/System/Convert/ToBoolean/toboolean2.cs index 239de1b50ea..22b2de4d95a 100644 --- a/snippets/csharp/System/Convert/ToBoolean/toboolean2.cs +++ b/snippets/csharp/System/Convert/ToBoolean/toboolean2.cs @@ -2,278 +2,280 @@ public class Example { - public static void Main() - { - ConvertByte(); - Console.WriteLine("-----"); - ConvertDecimal(); - Console.WriteLine("-----"); - ConvertInt16(); - Console.WriteLine("-----"); - ConvertInt32(); - Console.WriteLine("-----"); - ConvertInt64(); - Console.WriteLine("-----"); - ConvertSByte(); - Console.WriteLine("-----"); - ConvertSingle(); - Console.WriteLine("-----"); - ConvertUInt16(); - Console.WriteLine("-----"); - ConvertUInt32(); - Console.WriteLine("-----"); - ConvertUInt64(); - Console.WriteLine("-----"); - ConvertObject(); - } + public static void Main() + { + ConvertByte(); + Console.WriteLine("-----"); + ConvertDecimal(); + Console.WriteLine("-----"); + ConvertInt16(); + Console.WriteLine("-----"); + ConvertInt32(); + Console.WriteLine("-----"); + ConvertInt64(); + Console.WriteLine("-----"); + ConvertSByte(); + Console.WriteLine("-----"); + ConvertSingle(); + Console.WriteLine("-----"); + ConvertUInt16(); + Console.WriteLine("-----"); + ConvertUInt32(); + Console.WriteLine("-----"); + ConvertUInt64(); + Console.WriteLine("-----"); + ConvertObject(); + } - private static void ConvertByte() - { - // - byte[] bytes = { Byte.MinValue, 100, 200, Byte.MaxValue }; - bool result; + private static void ConvertByte() + { + // + byte[] bytes = { byte.MinValue, 100, 200, byte.MaxValue }; + bool result; - foreach (byte byteValue in bytes) - { - result = Convert.ToBoolean(byteValue); - Console.WriteLine("{0,-5} --> {1}", byteValue, result); - } - // The example displays the following output: - // 0 --> False - // 100 --> True - // 200 --> True - // 255 --> True - // - } + foreach (byte byteValue in bytes) + { + result = Convert.ToBoolean(byteValue); + Console.WriteLine($"{byteValue,-5} --> {result}"); + } + // The example displays the following output: + // 0 --> False + // 100 --> True + // 200 --> True + // 255 --> True + // + } - private static void ConvertDecimal() - { - // - decimal[] numbers = { Decimal.MinValue, -12034.87m, -100m, 0m, - 300m, 6790823.45m, Decimal.MaxValue }; - bool result; + private static void ConvertDecimal() + { + // + decimal[] numbers = { decimal.MinValue, -12034.87m, -100m, 0m, + 300m, 6790823.45m, decimal.MaxValue }; + bool result; - foreach (decimal number in numbers) - { - result = Convert.ToBoolean(number); - Console.WriteLine("{0,-30} --> {1}", number, result); - } - // The example displays the following output: - // -79228162514264337593543950335 --> True - // -12034.87 --> True - // -100 --> True - // 0 --> False - // 300 --> True - // 6790823.45 --> True - // 79228162514264337593543950335 --> True - // - } + foreach (decimal number in numbers) + { + result = Convert.ToBoolean(number); + Console.WriteLine($"{number,-30} --> {result}"); + } + // The example displays the following output: + // -79228162514264337593543950335 --> True + // -12034.87 --> True + // -100 --> True + // 0 --> False + // 300 --> True + // 6790823.45 --> True + // 79228162514264337593543950335 --> True + // + } - private static void ConvertInt16() - { - // - short[] numbers = { Int16.MinValue, -10000, -154, 0, 216, 21453, - Int16.MaxValue }; - bool result; + private static void ConvertInt16() + { + // + short[] numbers = { short.MinValue, -10000, -154, 0, 216, 21453, + short.MaxValue }; + bool result; - foreach (short number in numbers) - { - result = Convert.ToBoolean(number); - Console.WriteLine("{0,-7:N0} --> {1}", number, result); - } - // The example displays the following output: - // -32,768 --> True - // -10,000 --> True - // -154 --> True - // 0 --> False - // 216 --> True - // 21,453 --> True - // 32,767 --> True - // - } + foreach (short number in numbers) + { + result = Convert.ToBoolean(number); + Console.WriteLine($"{number,-7:N0} --> {result}"); + } + // The example displays the following output: + // -32,768 --> True + // -10,000 --> True + // -154 --> True + // 0 --> False + // 216 --> True + // 21,453 --> True + // 32,767 --> True + // + } - private static void ConvertInt32() - { - // - int[] numbers = { Int32.MinValue, -201649, -68, 0, 612, 4038907, - Int32.MaxValue }; - bool result; + private static void ConvertInt32() + { + // + int[] numbers = { int.MinValue, -201649, -68, 0, 612, 4038907, + int.MaxValue }; + bool result; - foreach (int number in numbers) - { - result = Convert.ToBoolean(number); - Console.WriteLine("{0,-15:N0} --> {1}", number, result); - } - // The example displays the following output: - // -2,147,483,648 --> True - // -201,649 --> True - // -68 --> True - // 0 --> False - // 612 --> True - // 4,038,907 --> True - // 2,147,483,647 --> True - // - } + foreach (int number in numbers) + { + result = Convert.ToBoolean(number); + Console.WriteLine($"{number,-15:N0} --> {result}"); + } + // The example displays the following output: + // -2,147,483,648 --> True + // -201,649 --> True + // -68 --> True + // 0 --> False + // 612 --> True + // 4,038,907 --> True + // 2,147,483,647 --> True + // + } - private static void ConvertInt64() - { - // - long[] numbers = { Int64.MinValue, -2016493, -689, 0, 6121, - 403890774, Int64.MaxValue }; - bool result; + private static void ConvertInt64() + { + // + long[] numbers = { long.MinValue, -2016493, -689, 0, 6121, + 403890774, long.MaxValue }; + bool result; - foreach (long number in numbers) - { - result = Convert.ToBoolean(number); - Console.WriteLine("{0,-26:N0} --> {1}", number, result); - } - // The example displays the following output: - // -9,223,372,036,854,775,808 --> True - // -2,016,493 --> True - // -689 --> True - // 0 --> False - // 6,121 --> True - // 403,890,774 --> True - // 9,223,372,036,854,775,807 --> True - // - } + foreach (long number in numbers) + { + result = Convert.ToBoolean(number); + Console.WriteLine($"{number,-26:N0} --> {result}"); + } + // The example displays the following output: + // -9,223,372,036,854,775,808 --> True + // -2,016,493 --> True + // -689 --> True + // 0 --> False + // 6,121 --> True + // 403,890,774 --> True + // 9,223,372,036,854,775,807 --> True + // + } - private static void ConvertSByte() - { - // - sbyte[] numbers = { SByte.MinValue, -1, 0, 10, 100, SByte.MaxValue }; - bool result; + private static void ConvertSByte() + { + // + sbyte[] numbers = { sbyte.MinValue, -1, 0, 10, 100, sbyte.MaxValue }; + bool result; - foreach (sbyte number in numbers) - { - result = Convert.ToBoolean(number); - Console.WriteLine("{0,-5} --> {1}", number, result); - } - // The example displays the following output: - // -128 --> True - // -1 --> True - // 0 --> False - // 10 --> True - // 100 --> True - // 127 --> True - // - } + foreach (sbyte number in numbers) + { + result = Convert.ToBoolean(number); + Console.WriteLine($"{number,-5} --> {result}"); + } + // The example displays the following output: + // -128 --> True + // -1 --> True + // 0 --> False + // 10 --> True + // 100 --> True + // 127 --> True + // + } - private static void ConvertSingle() - { - // - float[] numbers = { Single.MinValue, -193.0012f, 20e-15f, 0f, - 10551e-10f, 100.3398f, Single.MaxValue }; - bool result; + private static void ConvertSingle() + { + // + float[] numbers = { float.MinValue, -193.0012f, 20e-15f, 0f, + 10551e-10f, 100.3398f, float.MaxValue }; + bool result; - foreach (float number in numbers) - { - result = Convert.ToBoolean(number); - Console.WriteLine("{0,-15} --> {1}", number, result); - } - // The example displays the following output: - // -3.402823E+38 --> True - // -193.0012 --> True - // 2E-14 --> True - // 0 --> False - // 1.0551E-06 --> True - // 100.3398 --> True - // 3.402823E+38 --> True - // - } + foreach (float number in numbers) + { + result = Convert.ToBoolean(number); + Console.WriteLine($"{number,-15} --> {result}"); + } + // The example displays the following output: + // -3.402823E+38 --> True + // -193.0012 --> True + // 2E-14 --> True + // 0 --> False + // 1.0551E-06 --> True + // 100.3398 --> True + // 3.402823E+38 --> True + // + } - private static void ConvertUInt16() - { - // - ushort[] numbers = { UInt16.MinValue, 216, 21453, UInt16.MaxValue }; - bool result; + private static void ConvertUInt16() + { + // + ushort[] numbers = { ushort.MinValue, 216, 21453, ushort.MaxValue }; + bool result; - foreach (ushort number in numbers) - { - result = Convert.ToBoolean(number); - Console.WriteLine("{0,-7:N0} --> {1}", number, result); - } - // The example displays the following output: - // 0 --> False - // 216 --> True - // 21,453 --> True - // 65,535 --> True - // - } + foreach (ushort number in numbers) + { + result = Convert.ToBoolean(number); + Console.WriteLine($"{number,-7:N0} --> {result}"); + } + // The example displays the following output: + // 0 --> False + // 216 --> True + // 21,453 --> True + // 65,535 --> True + // + } - private static void ConvertUInt32() - { - // - uint[] numbers = { UInt32.MinValue, 612, 4038907, Int32.MaxValue }; - bool result; + private static void ConvertUInt32() + { + // + uint[] numbers = { uint.MinValue, 612, 4038907, int.MaxValue }; + bool result; - foreach (uint number in numbers) - { - result = Convert.ToBoolean(number); - Console.WriteLine("{0,-15:N0} --> {1}", number, result); - } - // The example displays the following output: - // 0 --> False - // 612 --> True - // 4,038,907 --> True - // 2,147,483,647 --> True - // - } + foreach (uint number in numbers) + { + result = Convert.ToBoolean(number); + Console.WriteLine($"{number,-15:N0} --> {result}"); + } + // The example displays the following output: + // 0 --> False + // 612 --> True + // 4,038,907 --> True + // 2,147,483,647 --> True + // + } - private static void ConvertUInt64() - { - // - ulong[] numbers = { UInt64.MinValue, 6121, 403890774, UInt64.MaxValue }; - bool result; + private static void ConvertUInt64() + { + // + ulong[] numbers = { ulong.MinValue, 6121, 403890774, ulong.MaxValue }; + bool result; - foreach (ulong number in numbers) - { - result = Convert.ToBoolean(number); - Console.WriteLine("{0,-26:N0} --> {1}", number, result); - } - // The example displays the following output: - // 0 --> False - // 6,121 --> True - // 403,890,774 --> True - // 18,446,744,073,709,551,615 --> True - // - } + foreach (ulong number in numbers) + { + result = Convert.ToBoolean(number); + Console.WriteLine($"{number,-26:N0} --> {result}"); + } + // The example displays the following output: + // 0 --> False + // 6,121 --> True + // 403,890,774 --> True + // 18,446,744,073,709,551,615 --> True + // + } - private static void ConvertObject() - { - // - object[] objects = { 16.33, -24, 0, "12", "12.7", String.Empty, + private static void ConvertObject() + { + // + object[] objects = { 16.33, -24, 0, "12", "12.7", string.Empty, "1String", "True", "false", null, new System.Collections.ArrayList() }; - foreach (object obj in objects) - { - Console.Write("{0,-40} --> ", - obj != null ? - String.Format("{0} ({1})", obj, obj.GetType().Name) : - "null"); - try { - Console.WriteLine("{0}", Convert.ToBoolean(obj)); - } - catch (FormatException) { - Console.WriteLine("Bad Format"); - } - catch (InvalidCastException) { - Console.WriteLine("No Conversion"); - } - } - // The example displays the following output: - // 16.33 (Double) --> True - // -24 (Int32) --> True - // 0 (Int32) --> False - // 12 (String) --> Bad Format - // 12.7 (String) --> Bad Format - // (String) --> Bad Format - // 1String (String) --> Bad Format - // True (String) --> True - // false (String) --> False - // null --> False - // System.Collections.ArrayList (ArrayList) --> No Conversion - // - } + foreach (object obj in objects) + { + Console.Write($"{(obj != null ? + string.Format("{0} ({1})", obj, obj.GetType().Name) : + "null"),-40} --> "); + try + { + Console.WriteLine($"{Convert.ToBoolean(obj)}"); + } + catch (FormatException) + { + Console.WriteLine("Bad Format"); + } + catch (InvalidCastException) + { + Console.WriteLine("No Conversion"); + } + } + // The example displays the following output: + // 16.33 (Double) --> True + // -24 (Int32) --> True + // 0 (Int32) --> False + // 12 (String) --> Bad Format + // 12.7 (String) --> Bad Format + // (String) --> Bad Format + // 1String (String) --> Bad Format + // True (String) --> True + // false (String) --> False + // null --> False + // System.Collections.ArrayList (ArrayList) --> No Conversion + // + } } diff --git a/snippets/csharp/System/Convert/ToByte/Conversion.cs b/snippets/csharp/System/Convert/ToByte/Conversion.cs index 0e6df0a2124..6d62107af92 100644 --- a/snippets/csharp/System/Convert/ToByte/Conversion.cs +++ b/snippets/csharp/System/Convert/ToByte/Conversion.cs @@ -2,387 +2,382 @@ public class Class1 { - public static void Main() - { - ConvertHexToNegativeInteger(); - Console.WriteLine(); - ConvertHexToInteger(); - Console.WriteLine(); - ConvertNegativeHexToByte(); - Console.WriteLine(); - ConvertHexToByte(); - Console.WriteLine(); - ConvertHexToNegativeShort(); - Console.WriteLine(); - ConvertHexToShort(); - Console.WriteLine(); - ConvertHexToNegativeLong(); - Console.WriteLine(); - ConvertHexToLong(); - Console.WriteLine(); - ConvertHexToNegativeSByte(); - Console.WriteLine(); - ConvertHexToSByte(); - Console.WriteLine(); - ConvertNegativeHexToUInt16(); - Console.WriteLine(); - ConvertHexToUInt16(); - Console.WriteLine(); - ConvertNegativeHexToUInt32(); - Console.WriteLine(); - ConvertHexToUInt32(); - Console.WriteLine(); - ConvertNegativeHexToUInt64(); - Console.WriteLine(); - ConvertHexToUInt64(); - } + public static void Main() + { + ConvertHexToNegativeInteger(); + Console.WriteLine(); + ConvertHexToInteger(); + Console.WriteLine(); + ConvertNegativeHexToByte(); + Console.WriteLine(); + ConvertHexToByte(); + Console.WriteLine(); + ConvertHexToNegativeShort(); + Console.WriteLine(); + ConvertHexToShort(); + Console.WriteLine(); + ConvertHexToNegativeLong(); + Console.WriteLine(); + ConvertHexToLong(); + Console.WriteLine(); + ConvertHexToNegativeSByte(); + Console.WriteLine(); + ConvertHexToSByte(); + Console.WriteLine(); + ConvertNegativeHexToUInt16(); + Console.WriteLine(); + ConvertHexToUInt16(); + Console.WriteLine(); + ConvertNegativeHexToUInt32(); + Console.WriteLine(); + ConvertHexToUInt32(); + Console.WriteLine(); + ConvertNegativeHexToUInt64(); + Console.WriteLine(); + ConvertHexToUInt64(); + } - private static void ConvertHexToNegativeInteger() - { - // - // Create a hexadecimal value out of range of the Integer type. - string value = Convert.ToString((long) int.MaxValue + 1, 16); - // Convert it back to a number. - try - { - int number = Convert.ToInt32(value, 16); - Console.WriteLine("0x{0} converts to {1}.", value, number.ToString()); - } - catch (OverflowException) - { - Console.WriteLine("Unable to convert '0x{0}' to an integer.", value); - } - // - } + private static void ConvertHexToNegativeInteger() + { + // + // Create a hexadecimal value out of range of the Integer type. + string value = Convert.ToString((long)int.MaxValue + 1, 16); + // Convert it back to a number. + try + { + int number = Convert.ToInt32(value, 16); + Console.WriteLine($"0x{value} converts to {number.ToString()}."); + } + catch (OverflowException) + { + Console.WriteLine($"Unable to convert '0x{value}' to an integer."); + } + // + } - private static void ConvertHexToInteger() - { - // - // Create a hexadecimal value out of range of the Integer type. - long sourceNumber = (long) int.MaxValue + 1; - bool isNegative = Math.Sign(sourceNumber) == -1; - string value = Convert.ToString(sourceNumber, 16); - int targetNumber; - try - { - targetNumber = Convert.ToInt32(value, 16); - if (!(isNegative) & (targetNumber & 0x80000000) != 0) - throw new OverflowException(); - else - Console.WriteLine("0x{0} converts to {1}.", value, targetNumber); - } - catch (OverflowException) - { - Console.WriteLine("Unable to convert '0x{0}' to an integer.", value); - } - // Displays the following to the console: - // Unable to convert '0x80000000' to an integer. - // - } + private static void ConvertHexToInteger() + { + // + // Create a hexadecimal value out of range of the Integer type. + long sourceNumber = (long)int.MaxValue + 1; + bool isNegative = Math.Sign(sourceNumber) == -1; + string value = Convert.ToString(sourceNumber, 16); + int targetNumber; + try + { + targetNumber = Convert.ToInt32(value, 16); + if (!(isNegative) & (targetNumber & 0x80000000) != 0) + throw new OverflowException(); + else + Console.WriteLine($"0x{value} converts to {targetNumber}."); + } + catch (OverflowException) + { + Console.WriteLine($"Unable to convert '0x{value}' to an integer."); + } + // Displays the following to the console: + // Unable to convert '0x80000000' to an integer. + // + } - private static void ConvertNegativeHexToByte() - { - // - // Create a hexadecimal value out of range of the Byte type. - string value = SByte.MinValue.ToString("X"); - // Convert it back to a number. - try - { - byte number = Convert.ToByte(value, 16); - Console.WriteLine("0x{0} converts to {1}.", value, number); - } - catch (OverflowException) - { - Console.WriteLine("Unable to convert '0x{0}' to a byte.", value); - } - // - } + private static void ConvertNegativeHexToByte() + { + // + // Create a hexadecimal value out of range of the Byte type. + string value = sbyte.MinValue.ToString("X"); + // Convert it back to a number. + try + { + byte number = Convert.ToByte(value, 16); + Console.WriteLine($"0x{value} converts to {number}."); + } + catch (OverflowException) + { + Console.WriteLine($"Unable to convert '0x{value}' to a byte."); + } + // + } - private static void ConvertHexToByte() - { - // - // Create a negative hexadecimal value out of range of the Byte type. - sbyte sourceNumber = SByte.MinValue; - bool isSigned = Math.Sign((sbyte)sourceNumber.GetType().GetField("MinValue").GetValue(null)) == -1; - string value = sourceNumber.ToString("X"); - byte targetNumber; - try - { - targetNumber = Convert.ToByte(value, 16); - if (isSigned && ((targetNumber & 0x80) != 0)) - throw new OverflowException(); - else - Console.WriteLine("0x{0} converts to {1}.", value, targetNumber); - } - catch (OverflowException) - { - Console.WriteLine("Unable to convert '0x{0}' to an unsigned byte.", value); - } - // Displays the following to the console: - // Unable to convert '0x80' to an unsigned byte. - // - } + private static void ConvertHexToByte() + { + // + // Create a negative hexadecimal value out of range of the Byte type. + sbyte sourceNumber = sbyte.MinValue; + bool isSigned = Math.Sign((sbyte)sourceNumber.GetType().GetField("MinValue").GetValue(null)) == -1; + string value = sourceNumber.ToString("X"); + byte targetNumber; + try + { + targetNumber = Convert.ToByte(value, 16); + if (isSigned && ((targetNumber & 0x80) != 0)) + throw new OverflowException(); + else + Console.WriteLine($"0x{value} converts to {targetNumber}."); + } + catch (OverflowException) + { + Console.WriteLine($"Unable to convert '0x{value}' to an unsigned byte."); + } + // Displays the following to the console: + // Unable to convert '0x80' to an unsigned byte. + // + } - private static void ConvertHexToNegativeShort() - { - // - // Create a hexadecimal value out of range of the Int16 type. - string value = Convert.ToString((int) short.MaxValue + 1, 16); - // Convert it back to a number. - try - { - short number = Convert.ToInt16(value, 16); - Console.WriteLine("0x{0} converts to {1}.", value, number); - } - catch (OverflowException) - { - Console.WriteLine("Unable to convert '0x{0}' to a 16-bit integer.", value); - } - // - } + private static void ConvertHexToNegativeShort() + { + // + // Create a hexadecimal value out of range of the Int16 type. + string value = Convert.ToString((int)short.MaxValue + 1, 16); + // Convert it back to a number. + try + { + short number = Convert.ToInt16(value, 16); + Console.WriteLine($"0x{value} converts to {number}."); + } + catch (OverflowException) + { + Console.WriteLine($"Unable to convert '0x{value}' to a 16-bit integer."); + } + // + } - private static void ConvertHexToShort() - { - // - // Create a hexadecimal value out of range of the Short type. - int sourceNumber = (int) short.MaxValue + 1; - bool isNegative = (Math.Sign(sourceNumber) == -1); - string value = Convert.ToString(sourceNumber, 16); - short targetNumber; - try - { - targetNumber = Convert.ToInt16(value, 16); - if (!isNegative && ((targetNumber & 0x8000) != 0)) - throw new OverflowException(); - else - Console.WriteLine("0x{0} converts to {1}.", value, targetNumber); - } - catch (OverflowException) - { - Console.WriteLine("Unable to convert '0x{0}' to a 16-bit integer.", value); - } - // Displays the following to the console: - // Unable to convert '0x8000' to a 16-bit integer. - // - } + private static void ConvertHexToShort() + { + // + // Create a hexadecimal value out of range of the Short type. + int sourceNumber = (int)short.MaxValue + 1; + bool isNegative = (Math.Sign(sourceNumber) == -1); + string value = Convert.ToString(sourceNumber, 16); + short targetNumber; + try + { + targetNumber = Convert.ToInt16(value, 16); + if (!isNegative && ((targetNumber & 0x8000) != 0)) + throw new OverflowException(); + else + Console.WriteLine($"0x{value} converts to {targetNumber}."); + } + catch (OverflowException) + { + Console.WriteLine($"Unable to convert '0x{value}' to a 16-bit integer."); + } + // Displays the following to the console: + // Unable to convert '0x8000' to a 16-bit integer. + // + } - private static void ConvertHexToNegativeLong() - { - // - // Create a hexadecimal value out of range of the long type. - string value = ulong.MaxValue.ToString("X"); - // Use Convert.ToInt64 to convert it back to a number. - try - { - long number = Convert.ToInt64(value, 16); - Console.WriteLine("0x{0} converts to {1}.", value, number); - } - catch (OverflowException) - { - Console.WriteLine("Unable to convert '0x{0}' to a long integer.", value); - } - // - } + private static void ConvertHexToNegativeLong() + { + // + // Create a hexadecimal value out of range of the long type. + string value = ulong.MaxValue.ToString("X"); + // Use Convert.ToInt64 to convert it back to a number. + try + { + long number = Convert.ToInt64(value, 16); + Console.WriteLine($"0x{value} converts to {number}."); + } + catch (OverflowException) + { + Console.WriteLine($"Unable to convert '0x{value}' to a long integer."); + } + // + } - private static void ConvertHexToLong() - { - // - // Create a negative hexadecimal value out of range of the Byte type. - ulong sourceNumber = ulong.MaxValue; - bool isSigned = Math.Sign(Convert.ToDouble(sourceNumber.GetType().GetField("MinValue").GetValue(null))) == -1; - string value = sourceNumber.ToString("X"); - long targetNumber; - try - { - targetNumber = Convert.ToInt64(value, 16); - if (!isSigned && ((targetNumber & 0x80000000) != 0)) - throw new OverflowException(); - else - Console.WriteLine("0x{0} converts to {1}.", value, targetNumber); - } - catch (OverflowException) - { - Console.WriteLine("Unable to convert '0x{0}' to a long integer.", value); - } - // Displays the following to the console: - // Unable to convert '0xFFFFFFFFFFFFFFFF' to a long integer. - // - } + private static void ConvertHexToLong() + { + // + // Create a negative hexadecimal value out of range of the Byte type. + ulong sourceNumber = ulong.MaxValue; + bool isSigned = Math.Sign(Convert.ToDouble(sourceNumber.GetType().GetField("MinValue").GetValue(null))) == -1; + string value = sourceNumber.ToString("X"); + long targetNumber; + try + { + targetNumber = Convert.ToInt64(value, 16); + if (!isSigned && ((targetNumber & 0x80000000) != 0)) + throw new OverflowException(); + else + Console.WriteLine($"0x{value} converts to {targetNumber}."); + } + catch (OverflowException) + { + Console.WriteLine($"Unable to convert '0x{value}' to a long integer."); + } + // Displays the following to the console: + // Unable to convert '0xFFFFFFFFFFFFFFFF' to a long integer. + // + } - private static void ConvertHexToNegativeSByte() - { - // - // Create a hexadecimal value out of range of the SByte type. - string value = Convert.ToString(byte.MaxValue, 16); - // Convert it back to a number. - try - { - sbyte number = Convert.ToSByte(value, 16); - Console.WriteLine("0x{0} converts to {1}.", value, number); - } - catch (OverflowException) - { - Console.WriteLine("Unable to convert '0x{0}' to a signed byte.", value); - } - // - } + private static void ConvertHexToNegativeSByte() + { + // + // Create a hexadecimal value out of range of the SByte type. + string value = Convert.ToString(byte.MaxValue, 16); + // Convert it back to a number. + try + { + sbyte number = Convert.ToSByte(value, 16); + Console.WriteLine($"0x{value} converts to {number}."); + } + catch (OverflowException) + { + Console.WriteLine($"Unable to convert '0x{value}' to a signed byte."); + } + // + } - private static void ConvertHexToSByte() - { - // - // Create a hexadecimal value out of range of the SByte type. - byte sourceNumber = byte.MaxValue; - bool isSigned = Math.Sign(Convert.ToDouble(sourceNumber.GetType().GetField("MinValue").GetValue(null))) == -1; - string value = Convert.ToString(sourceNumber, 16); - sbyte targetNumber; - try - { - targetNumber = Convert.ToSByte(value, 16); - if (!isSigned && ((targetNumber & 0x80) != 0)) - throw new OverflowException(); - else - Console.WriteLine("0x{0} converts to {1}.", value, targetNumber); - } - catch (OverflowException) - { - Console.WriteLine("Unable to convert '0x{0}' to a signed byte.", value); - } - // Displays the following to the console: - // Unable to convert '0xff' to a signed byte. - // - } + private static void ConvertHexToSByte() + { + // + // Create a hexadecimal value out of range of the SByte type. + byte sourceNumber = byte.MaxValue; + bool isSigned = Math.Sign(Convert.ToDouble(sourceNumber.GetType().GetField("MinValue").GetValue(null))) == -1; + string value = Convert.ToString(sourceNumber, 16); + sbyte targetNumber; + try + { + targetNumber = Convert.ToSByte(value, 16); + if (!isSigned && ((targetNumber & 0x80) != 0)) + throw new OverflowException(); + else + Console.WriteLine($"0x{value} converts to {targetNumber}."); + } + catch (OverflowException) + { + Console.WriteLine($"Unable to convert '0x{value}' to a signed byte."); + } + // Displays the following to the console: + // Unable to convert '0xff' to a signed byte. + // + } - private static void ConvertNegativeHexToUInt16() - { - // - // Create a hexadecimal value out of range of the UInt16 type. - string value = Convert.ToString(Int16.MinValue, 16); - // Convert it back to a number. - try - { - UInt16 number = Convert.ToUInt16(value, 16); - Console.WriteLine("0x{0} converts to {1}.", value, number); - } - catch (OverflowException) - { - Console.WriteLine("Unable to convert '0x{0}' to an unsigned short integer.", - value); - } - // - } + private static void ConvertNegativeHexToUInt16() + { + // + // Create a hexadecimal value out of range of the UInt16 type. + string value = Convert.ToString(short.MinValue, 16); + // Convert it back to a number. + try + { + ushort number = Convert.ToUInt16(value, 16); + Console.WriteLine($"0x{value} converts to {number}."); + } + catch (OverflowException) + { + Console.WriteLine($"Unable to convert '0x{value}' to an unsigned short integer."); + } + // + } - private static void ConvertHexToUInt16() - { - // - // Create a negative hexadecimal value out of range of the UInt16 type. - short sourceNumber = Int16.MinValue; - bool isSigned = Math.Sign((short)sourceNumber.GetType().GetField("MinValue").GetValue(null)) == -1; - string value = Convert.ToString(sourceNumber, 16); - UInt16 targetNumber; - try - { - targetNumber = Convert.ToUInt16(value, 16); - if (isSigned && ((targetNumber & 0x8000) != 0)) - throw new OverflowException(); - else - Console.WriteLine("0x{0} converts to {1}.", value, targetNumber); - } - catch (OverflowException) - { - Console.WriteLine("Unable to convert '0x{0}' to an unsigned short integer.", value); - } - // Displays the following to the console: - // Unable to convert '0x8000' to an unsigned short integer. - // - } + private static void ConvertHexToUInt16() + { + // + // Create a negative hexadecimal value out of range of the UInt16 type. + short sourceNumber = short.MinValue; + bool isSigned = Math.Sign((short)sourceNumber.GetType().GetField("MinValue").GetValue(null)) == -1; + string value = Convert.ToString(sourceNumber, 16); + ushort targetNumber; + try + { + targetNumber = Convert.ToUInt16(value, 16); + if (isSigned && ((targetNumber & 0x8000) != 0)) + throw new OverflowException(); + else + Console.WriteLine($"0x{value} converts to {targetNumber}."); + } + catch (OverflowException) + { + Console.WriteLine($"Unable to convert '0x{value}' to an unsigned short integer."); + } + // Displays the following to the console: + // Unable to convert '0x8000' to an unsigned short integer. + // + } - private static void ConvertNegativeHexToUInt32() - { - // - // Create a hexadecimal value out of range of the UInt32 type. - string value = Convert.ToString(Int32.MinValue, 16); - // Convert it back to a number. - try - { - UInt32 number = Convert.ToUInt32(value, 16); - Console.WriteLine("0x{0} converts to {1}.", value, number); - } - catch (OverflowException) - { - Console.WriteLine("Unable to convert '0x{0}' to an unsigned integer.", - value); - } - // - } + private static void ConvertNegativeHexToUInt32() + { + // + // Create a hexadecimal value out of range of the UInt32 type. + string value = Convert.ToString(int.MinValue, 16); + // Convert it back to a number. + try + { + uint number = Convert.ToUInt32(value, 16); + Console.WriteLine($"0x{value} converts to {number}."); + } + catch (OverflowException) + { + Console.WriteLine($"Unable to convert '0x{value}' to an unsigned integer."); + } + // + } - private static void ConvertHexToUInt32() - { - // - // Create a negative hexadecimal value out of range of the UInt32 type. - int sourceNumber = Int32.MinValue; - bool isSigned = Math.Sign((int)sourceNumber.GetType().GetField("MinValue").GetValue(null)) == -1; - string value = Convert.ToString(sourceNumber, 16); - UInt32 targetNumber; - try - { - targetNumber = Convert.ToUInt32(value, 16); - if (isSigned && ((targetNumber & 0x80000000) != 0)) - throw new OverflowException(); - else - Console.WriteLine("0x{0} converts to {1}.", value, targetNumber); - } - catch (OverflowException) - { - Console.WriteLine("Unable to convert '0x{0}' to an unsigned integer.", - value); - } - // Displays the following to the console: - // Unable to convert '0x80000000' to an unsigned integer. - // - } + private static void ConvertHexToUInt32() + { + // + // Create a negative hexadecimal value out of range of the UInt32 type. + int sourceNumber = int.MinValue; + bool isSigned = Math.Sign((int)sourceNumber.GetType().GetField("MinValue").GetValue(null)) == -1; + string value = Convert.ToString(sourceNumber, 16); + uint targetNumber; + try + { + targetNumber = Convert.ToUInt32(value, 16); + if (isSigned && ((targetNumber & 0x80000000) != 0)) + throw new OverflowException(); + else + Console.WriteLine($"0x{value} converts to {targetNumber}."); + } + catch (OverflowException) + { + Console.WriteLine($"Unable to convert '0x{value}' to an unsigned integer."); + } + // Displays the following to the console: + // Unable to convert '0x80000000' to an unsigned integer. + // + } - private static void ConvertNegativeHexToUInt64() - { - // - // Create a hexadecimal value out of range of the UInt64 type. - string value = Convert.ToString(Int64.MinValue, 16); - // Convert it back to a number. - try - { - UInt64 number = Convert.ToUInt64(value, 16); - Console.WriteLine("0x{0} converts to {1}.", value, number); - } - catch (OverflowException) - { - Console.WriteLine("Unable to convert '0x{0}' to an unsigned long integer.", - value); - } - // - } + private static void ConvertNegativeHexToUInt64() + { + // + // Create a hexadecimal value out of range of the UInt64 type. + string value = Convert.ToString(long.MinValue, 16); + // Convert it back to a number. + try + { + ulong number = Convert.ToUInt64(value, 16); + Console.WriteLine($"0x{value} converts to {number}."); + } + catch (OverflowException) + { + Console.WriteLine($"Unable to convert '0x{value}' to an unsigned long integer."); + } + // + } - private static void ConvertHexToUInt64() - { - // - // Create a negative hexadecimal value out of range of the UInt64 type. - long sourceNumber = Int64.MinValue; - bool isSigned = Math.Sign((long)sourceNumber.GetType().GetField("MinValue").GetValue(null)) == -1; - string value = Convert.ToString(sourceNumber, 16); - UInt64 targetNumber; - try - { - targetNumber = Convert.ToUInt64(value, 16); - if (isSigned && ((targetNumber & 0x8000000000000000) != 0)) - throw new OverflowException(); - else - Console.WriteLine("0x{0} converts to {1}.", value, targetNumber); - } - catch (OverflowException) - { - Console.WriteLine("Unable to convert '0x{0}' to an unsigned long integer.", - value); - } - // Displays the following to the console: - // Unable to convert '0x8000000000000000' to an unsigned long integer. - // - } + private static void ConvertHexToUInt64() + { + // + // Create a negative hexadecimal value out of range of the UInt64 type. + long sourceNumber = long.MinValue; + bool isSigned = Math.Sign((long)sourceNumber.GetType().GetField("MinValue").GetValue(null)) == -1; + string value = Convert.ToString(sourceNumber, 16); + ulong targetNumber; + try + { + targetNumber = Convert.ToUInt64(value, 16); + if (isSigned && ((targetNumber & 0x8000000000000000) != 0)) + throw new OverflowException(); + else + Console.WriteLine($"0x{value} converts to {targetNumber}."); + } + catch (OverflowException) + { + Console.WriteLine($"Unable to convert '0x{value}' to an unsigned long integer."); + } + // Displays the following to the console: + // Unable to convert '0x8000000000000000' to an unsigned long integer. + // + } } diff --git a/snippets/csharp/System/Convert/ToChar/strdatetime.cs b/snippets/csharp/System/Convert/ToChar/strdatetime.cs index 3e45806f9d8..4137b581518 100644 --- a/snippets/csharp/System/Convert/ToChar/strdatetime.cs +++ b/snippets/csharp/System/Convert/ToChar/strdatetime.cs @@ -8,63 +8,63 @@ class StringToDateTimeDemo const string lineFmt = "{0,-18}{1,-12}{2}"; // Get the exception type name; remove the namespace prefix. - public static string GetExceptionType( Exception ex ) + public static string GetExceptionType(Exception ex) { - string exceptionType = ex.GetType( ).ToString( ); + string exceptionType = ex.GetType().ToString(); return exceptionType.Substring( - exceptionType.LastIndexOf( '.' ) + 1 ); + exceptionType.LastIndexOf('.') + 1); } - public static void StringToDateTime( string cultureName ) + public static void StringToDateTime(string cultureName) { - string[ ] dateStrings = { "01/02/03", + string[] dateStrings = { "01/02/03", "2001/02/03", "01/2002/03", "01/02/2003", "21/02/03", "01/22/03", "01/02/23" }; - CultureInfo culture = new CultureInfo( cultureName ); + CultureInfo culture = new(cultureName); - Console.WriteLine( ); + Console.WriteLine(); // Convert each string in the dateStrings array. - foreach( string dateStr in dateStrings ) + foreach (string dateStr in dateStrings) { DateTime dateTimeValue; // Display the first part of the output line. - Console.Write( lineFmt, dateStr, cultureName, null ); + Console.Write(lineFmt, dateStr, cultureName, null); try { // Convert the string to a DateTime object. - dateTimeValue = Convert.ToDateTime( dateStr, culture ); + dateTimeValue = Convert.ToDateTime(dateStr, culture); // Display the DateTime object in a fixed format // if Convert succeeded. - Console.WriteLine( "{0:yyyy-MMM-dd}", dateTimeValue ); + Console.WriteLine($"{dateTimeValue:yyyy-MMM-dd}"); } - catch( Exception ex ) + catch (Exception ex) { // Display the exception type if Parse failed. - Console.WriteLine( "{0}", GetExceptionType( ex ) ); + Console.WriteLine($"{GetExceptionType(ex)}"); } } } - public static void Main( ) + public static void Main() { - Console.WriteLine( "This example of " + + Console.WriteLine("This example of " + "Convert.ToDateTime( String, IFormatProvider ) " + "\ngenerates the following output. Several strings are " + "converted \nto DateTime objects using formatting " + "information from different \ncultures, and then the " + - "strings are displayed in a \nculture-invariant form.\n" ); - Console.WriteLine( lineFmt, "Date String", "Culture", - "DateTime or Exception" ); - Console.WriteLine( lineFmt, "-----------", "-------", - "---------------------" ); + "strings are displayed in a \nculture-invariant form.\n"); + Console.WriteLine(lineFmt, "Date String", "Culture", + "DateTime or Exception"); + Console.WriteLine(lineFmt, "-----------", "-------", + "---------------------"); - StringToDateTime( "en-US" ); - StringToDateTime( "ru-RU" ); - StringToDateTime( "ja-JP" ); + StringToDateTime("en-US"); + StringToDateTime("ru-RU"); + StringToDateTime("ja-JP"); } } diff --git a/snippets/csharp/System/Convert/ToChar/stringnonnum.cs b/snippets/csharp/System/Convert/ToChar/stringnonnum.cs index 725bd94f0e8..d9831472787 100644 --- a/snippets/csharp/System/Convert/ToChar/stringnonnum.cs +++ b/snippets/csharp/System/Convert/ToChar/stringnonnum.cs @@ -1,6 +1,6 @@ // using System; -using System.Globalization; + public class DummyProvider : IFormatProvider { @@ -10,58 +10,58 @@ public object GetFormat(Type argType) { // Here, GetFormat displays the name of argType, after removing // the namespace information. GetFormat always returns null. - string argStr = argType.ToString( ); - if( argStr == "" ) + string argStr = argType.ToString(); + if (argStr == "") argStr = "Empty"; - argStr = argStr.Substring( argStr.LastIndexOf( '.' ) + 1 ); + argStr = argStr.Substring(argStr.LastIndexOf('.') + 1); - Console.Write( "{0,-20}", argStr ); + Console.Write($"{argStr,-20}"); return null; } } class ConvertNonNumericProviderDemo { - public static void Main( ) + public static void Main() { // Create an instance of IFormatProvider. - DummyProvider provider = new DummyProvider( ); - string format = "{0,-17}{1,-17}{2}"; + DummyProvider provider = new(); + string format = "{0,-17}{1,-17}{2}"; // Convert these values using DummyProvider. - string Int32A = "-252645135"; - string DoubleA = "61680.3855"; + string Int32A = "-252645135"; + string DoubleA = "61680.3855"; string DayTimeA = "2001/9/11 13:45"; - string BoolA = "True"; - string StringA = "Qwerty"; - string CharA = "$"; + string BoolA = "True"; + string StringA = "Qwerty"; + string CharA = "$"; - Console.WriteLine( "This example of selected " + + Console.WriteLine("This example of selected " + "Convert.To( String, IFormatProvider ) \nmethods " + "generates the following output. The example displays " + - "the \nprovider type if the IFormatProvider is called." ); - Console.WriteLine( "\nNote: For the " + + "the \nprovider type if the IFormatProvider is called."); + Console.WriteLine("\nNote: For the " + "ToBoolean, ToString, and ToChar methods, the \n" + - "IFormatProvider object is not referenced." ); + "IFormatProvider object is not referenced."); // The format provider is called for the following conversions. - Console.WriteLine( ); - Console.WriteLine( format, "ToInt32", Int32A, - Convert.ToInt32( Int32A, provider ) ); - Console.WriteLine( format, "ToDouble", DoubleA, - Convert.ToDouble( DoubleA, provider ) ); - Console.WriteLine( format, "ToDateTime", DayTimeA, - Convert.ToDateTime( DayTimeA, provider ) ); + Console.WriteLine(); + Console.WriteLine(format, "ToInt32", Int32A, + Convert.ToInt32(Int32A, provider)); + Console.WriteLine(format, "ToDouble", DoubleA, + Convert.ToDouble(DoubleA, provider)); + Console.WriteLine(format, "ToDateTime", DayTimeA, + Convert.ToDateTime(DayTimeA, provider)); // The format provider is not called for these conversions. - Console.WriteLine( ); - Console.WriteLine( format, "ToBoolean", BoolA, - Convert.ToBoolean( BoolA, provider ) ); - Console.WriteLine( format, "ToString", StringA, - Convert.ToString( StringA, provider ) ); - Console.WriteLine( format, "ToChar", CharA, - Convert.ToChar( CharA, provider ) ); + Console.WriteLine(); + Console.WriteLine(format, "ToBoolean", BoolA, + Convert.ToBoolean(BoolA, provider)); + Console.WriteLine(format, "ToString", StringA, + Convert.ToString(StringA, provider)); + Console.WriteLine(format, "ToChar", CharA, + Convert.ToChar(CharA, provider)); } } diff --git a/snippets/csharp/System/Convert/ToChar/tochar1.cs b/snippets/csharp/System/Convert/ToChar/tochar1.cs index 22721aabaa9..4797c4d0215 100644 --- a/snippets/csharp/System/Convert/ToChar/tochar1.cs +++ b/snippets/csharp/System/Convert/ToChar/tochar1.cs @@ -2,287 +2,295 @@ public class Example { - public static void Main() - { - ConvertByte(); - Console.WriteLine("-----"); - ConvertInt16(); - Console.WriteLine("-----"); - ConvertInt32(); - Console.WriteLine("-----"); - ConvertSByte(); - Console.WriteLine("-----"); - ConvertString(); - Console.WriteLine("-----"); - ConvertUInt16(); - Console.WriteLine("-----"); - ConvertUInt32(); - Console.WriteLine("-----"); - ConvertUInt64(); - Console.WriteLine("-----"); - ConvertObject(); - } + public static void Main() + { + ConvertByte(); + Console.WriteLine("-----"); + ConvertInt16(); + Console.WriteLine("-----"); + ConvertInt32(); + Console.WriteLine("-----"); + ConvertSByte(); + Console.WriteLine("-----"); + ConvertString(); + Console.WriteLine("-----"); + ConvertUInt16(); + Console.WriteLine("-----"); + ConvertUInt32(); + Console.WriteLine("-----"); + ConvertUInt64(); + Console.WriteLine("-----"); + ConvertObject(); + } - private static void ConvertByte() - { - // - byte[] bytes = {Byte.MinValue, 40, 80, 120, 180, Byte.MaxValue}; - char result; - foreach (byte number in bytes) - { - result = Convert.ToChar(number); - Console.WriteLine("{0} converts to '{1}'.", number, result); - } - // The example displays the following output: - // 0 converts to ' '. - // 40 converts to '('. - // 80 converts to 'P'. - // 120 converts to 'x'. - // 180 converts to '''. - // 255 converts to 'ÿ'. - // - } - - private static void ConvertInt16() - { - // - short[] numbers = { Int16.MinValue, 0, 40, 160, 255, 1028, - 2011, Int16.MaxValue }; - char result; - foreach (short number in numbers) - { - try { + private static void ConvertByte() + { + // + byte[] bytes = { byte.MinValue, 40, 80, 120, 180, byte.MaxValue }; + char result; + foreach (byte number in bytes) + { result = Convert.ToChar(number); - Console.WriteLine("{0} converts to '{1}'.", number, result); - } - catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the Char data type.", - number); - } - } - // The example displays the following output: - // -32768 is outside the range of the Char data type. - // 0 converts to ' '. - // 40 converts to '('. - // 160 converts to ' '. - // 255 converts to 'ÿ'. - // 1028 converts to 'Є'. - // 2011 converts to 'ߛ'. - // 32767 converts to '翿'. - // - } + Console.WriteLine($"{number} converts to '{result}'."); + } + // The example displays the following output: + // 0 converts to ' '. + // 40 converts to '('. + // 80 converts to 'P'. + // 120 converts to 'x'. + // 180 converts to '''. + // 255 converts to 'ÿ'. + // + } - private static void ConvertInt32() - { - // - int[] numbers = { -1, 0, 40, 160, 255, 1028, - 2011, 30001, 207154, Int32.MaxValue }; - char result; - foreach (int number in numbers) - { - try { - result = Convert.ToChar(number); - Console.WriteLine("{0} converts to '{1}'.", number, result); - } - catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the Char data type.", - number); - } - } - // -1 is outside the range of the Char data type. - // 0 converts to ' '. - // 40 converts to '('. - // 160 converts to ' '. - // 255 converts to 'ÿ'. - // 1028 converts to 'Є'. - // 2011 converts to 'ߛ'. - // 30001 converts to '由'. - // 207154 is outside the range of the Char data type. - // 2147483647 is outside the range of the Char data type. - // - } + private static void ConvertInt16() + { + // + short[] numbers = { short.MinValue, 0, 40, 160, 255, 1028, + 2011, short.MaxValue }; + char result; + foreach (short number in numbers) + { + try + { + result = Convert.ToChar(number); + Console.WriteLine($"{number} converts to '{result}'."); + } + catch (OverflowException) + { + Console.WriteLine($"{number} is outside the range of the Char data type."); + } + } + // The example displays the following output: + // -32768 is outside the range of the Char data type. + // 0 converts to ' '. + // 40 converts to '('. + // 160 converts to ' '. + // 255 converts to 'ÿ'. + // 1028 converts to 'Є'. + // 2011 converts to 'ߛ'. + // 32767 converts to '翿'. + // + } - private static void ConvertSByte() - { - // - sbyte[] numbers = { SByte.MinValue, -1, 40, 80, 120, SByte.MaxValue }; - char result; - foreach (sbyte number in numbers) - { - try { - result = Convert.ToChar(number); - Console.WriteLine("{0} converts to '{1}'.", number, result); - } - catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the Char data type.", - number); - } - } - // The example displays the following output: - // -128 is outside the range of the Char data type. - // -1 is outside the range of the Char data type. - // 40 converts to '('. - // 80 converts to 'P'. - // 120 converts to 'x'. - // 127 converts to '⌂'. - // - } + private static void ConvertInt32() + { + // + int[] numbers = { -1, 0, 40, 160, 255, 1028, + 2011, 30001, 207154, int.MaxValue }; + char result; + foreach (int number in numbers) + { + try + { + result = Convert.ToChar(number); + Console.WriteLine($"{number} converts to '{result}'."); + } + catch (OverflowException) + { + Console.WriteLine($"{number} is outside the range of the Char data type."); + } + } + // -1 is outside the range of the Char data type. + // 0 converts to ' '. + // 40 converts to '('. + // 160 converts to ' '. + // 255 converts to 'ÿ'. + // 1028 converts to 'Є'. + // 2011 converts to 'ߛ'. + // 30001 converts to '由'. + // 207154 is outside the range of the Char data type. + // 2147483647 is outside the range of the Char data type. + // + } - private static void ConvertString() - { - // - string nullString = null; - string[] strings = { "A", "This", '\u0007'.ToString(), nullString }; - char result; - foreach (string strng in strings) - { - try { - result = Convert.ToChar(strng); - Console.WriteLine("'{0}' converts to '{1}'.", strng, result); - } - catch (FormatException) - { - Console.WriteLine("'{0}' is not in the correct format for conversion to a Char.", - strng); - } - catch (ArgumentNullException) { - Console.WriteLine("A null string cannot be converted to a Char."); - } - } - // The example displays the following output: - // 'A' converts to 'A'. - // 'This' is not in the correct format for conversion to a Char. - // ' ' converts to ' '. - // A null string cannot be converted to a Char. - // - } + private static void ConvertSByte() + { + // + sbyte[] numbers = { sbyte.MinValue, -1, 40, 80, 120, sbyte.MaxValue }; + char result; + foreach (sbyte number in numbers) + { + try + { + result = Convert.ToChar(number); + Console.WriteLine($"{number} converts to '{result}'."); + } + catch (OverflowException) + { + Console.WriteLine($"{number} is outside the range of the Char data type."); + } + } + // The example displays the following output: + // -128 is outside the range of the Char data type. + // -1 is outside the range of the Char data type. + // 40 converts to '('. + // 80 converts to 'P'. + // 120 converts to 'x'. + // 127 converts to '⌂'. + // + } - private static void ConvertUInt16() - { - // - ushort[] numbers = { UInt16.MinValue, 40, 160, 255, 1028, - 2011, UInt16.MaxValue }; - char result; - foreach (ushort number in numbers) - { - result = Convert.ToChar(number); - Console.WriteLine("{0} converts to '{1}'.", number, result); - } - // The example displays the following output: - // 0 converts to ' '. - // 40 converts to '('. - // 160 converts to ' '. - // 255 converts to 'ÿ'. - // 1028 converts to 'Є'. - // 2011 converts to 'ߛ'. - // 65535 converts to '￿'. - // - } + private static void ConvertString() + { + // + string nullString = null; + string[] strings = { "A", "This", '\u0007'.ToString(), nullString }; + char result; + foreach (string strng in strings) + { + try + { + result = Convert.ToChar(strng); + Console.WriteLine($"'{strng}' converts to '{result}'."); + } + catch (FormatException) + { + Console.WriteLine($"'{strng}' is not in the correct format for conversion to a Char."); + } + catch (ArgumentNullException) + { + Console.WriteLine("A null string cannot be converted to a Char."); + } + } + // The example displays the following output: + // 'A' converts to 'A'. + // 'This' is not in the correct format for conversion to a Char. + // ' ' converts to ' '. + // A null string cannot be converted to a Char. + // + } - private static void ConvertUInt32() - { - // - uint[] numbers = { UInt32.MinValue, 40, 160, 255, 1028, - 2011, 30001, 207154, Int32.MaxValue }; - char result; - foreach (uint number in numbers) - { - try { + private static void ConvertUInt16() + { + // + ushort[] numbers = { ushort.MinValue, 40, 160, 255, 1028, + 2011, ushort.MaxValue }; + char result; + foreach (ushort number in numbers) + { result = Convert.ToChar(number); - Console.WriteLine("{0} converts to '{1}'.", number, result); - } - catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the Char data type.", - number); - } - } - // The example displays the following output: - // 0 converts to ' '. - // 40 converts to '('. - // 160 converts to ' '. - // 255 converts to 'ÿ'. - // 1028 converts to 'Є'. - // 2011 converts to 'ߛ'. - // 30001 converts to '由'. - // 207154 is outside the range of the Char data type. - // 2147483647 is outside the range of the Char data type. - // - } + Console.WriteLine($"{number} converts to '{result}'."); + } + // The example displays the following output: + // 0 converts to ' '. + // 40 converts to '('. + // 160 converts to ' '. + // 255 converts to 'ÿ'. + // 1028 converts to 'Є'. + // 2011 converts to 'ߛ'. + // 65535 converts to '￿'. + // + } - private static void ConvertUInt64() - { - // - ulong[] numbers = { UInt64.MinValue, 40, 160, 255, 1028, - 2011, 30001, 207154, Int64.MaxValue }; - char result; - foreach (ulong number in numbers) - { - try { - result = Convert.ToChar(number); - Console.WriteLine("{0} converts to '{1}'.", number, result); - } - catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the Char data type.", - number); - } - } - // The example displays the following output: - // 0 converts to ' '. - // 40 converts to '('. - // 160 converts to ' '. - // 255 converts to 'ÿ'. - // 1028 converts to 'Є'. - // 2011 converts to 'ߛ'. - // 30001 converts to '由'. - // 207154 is outside the range of the Char data type. - // 9223372036854775807 is outside the range of the Char data type. - // - } + private static void ConvertUInt32() + { + // + uint[] numbers = { uint.MinValue, 40, 160, 255, 1028, + 2011, 30001, 207154, int.MaxValue }; + char result; + foreach (uint number in numbers) + { + try + { + result = Convert.ToChar(number); + Console.WriteLine($"{number} converts to '{result}'."); + } + catch (OverflowException) + { + Console.WriteLine($"{number} is outside the range of the Char data type."); + } + } + // The example displays the following output: + // 0 converts to ' '. + // 40 converts to '('. + // 160 converts to ' '. + // 255 converts to 'ÿ'. + // 1028 converts to 'Є'. + // 2011 converts to 'ߛ'. + // 30001 converts to '由'. + // 207154 is outside the range of the Char data type. + // 2147483647 is outside the range of the Char data type. + // + } + + private static void ConvertUInt64() + { + // + ulong[] numbers = { ulong.MinValue, 40, 160, 255, 1028, + 2011, 30001, 207154, long.MaxValue }; + char result; + foreach (ulong number in numbers) + { + try + { + result = Convert.ToChar(number); + Console.WriteLine($"{number} converts to '{result}'."); + } + catch (OverflowException) + { + Console.WriteLine($"{number} is outside the range of the Char data type."); + } + } + // The example displays the following output: + // 0 converts to ' '. + // 40 converts to '('. + // 160 converts to ' '. + // 255 converts to 'ÿ'. + // 1028 converts to 'Є'. + // 2011 converts to 'ߛ'. + // 30001 converts to '由'. + // 207154 is outside the range of the Char data type. + // 9223372036854775807 is outside the range of the Char data type. + // + } - private static void ConvertObject() - { - // - object[] values = { 'r', "s", "word", (byte) 83, 77, 109324, 335812911, + private static void ConvertObject() + { + // + object[] values = { 'r', "s", "word", (byte) 83, 77, 109324, 335812911, new DateTime(2009, 3, 10), (uint) 1934, (sbyte) -17, 169.34, 175.6m, null }; - char result; + char result; - foreach (object value in values) - { - try { - result = Convert.ToChar(value); - Console.WriteLine("The {0} value {1} converts to {2}.", - value.GetType().Name, value, result); - } - catch (FormatException e) { - Console.WriteLine(e.Message); - } - catch (InvalidCastException) { - Console.WriteLine("Conversion of the {0} value {1} to a Char is not supported.", - value.GetType().Name, value); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Char data type.", - value.GetType().Name, value); - } - catch (NullReferenceException) { - Console.WriteLine("Cannot convert a null reference to a Char."); - } - } - // The example displays the following output: - // The Char value r converts to r. - // The String value s converts to s. - // String must be exactly one character long. - // The Byte value 83 converts to S. - // The Int32 value 77 converts to M. - // The Int32 value 109324 is outside the range of the Char data type. - // The Int32 value 335812911 is outside the range of the Char data type. - // Conversion of the DateTime value 3/10/2009 12:00:00 AM to a Char is not supported. - // The UInt32 value 1934 converts to ?. - // The SByte value -17 is outside the range of the Char data type. - // Conversion of the Double value 169.34 to a Char is not supported. - // Conversion of the Decimal value 175.6 to a Char is not supported. - // Cannot convert a null reference to a Char. - // - } + foreach (object value in values) + { + try + { + result = Convert.ToChar(value); + Console.WriteLine($"The {value.GetType().Name} value {value} converts to {result}."); + } + catch (FormatException e) + { + Console.WriteLine(e.Message); + } + catch (InvalidCastException) + { + Console.WriteLine($"Conversion of the {value.GetType().Name} value {value} to a Char is not supported."); + } + catch (OverflowException) + { + Console.WriteLine($"The {value.GetType().Name} value {value} is outside the range of the Char data type."); + } + catch (NullReferenceException) + { + Console.WriteLine("Cannot convert a null reference to a Char."); + } + } + // The example displays the following output: + // The Char value r converts to r. + // The String value s converts to s. + // String must be exactly one character long. + // The Byte value 83 converts to S. + // The Int32 value 77 converts to M. + // The Int32 value 109324 is outside the range of the Char data type. + // The Int32 value 335812911 is outside the range of the Char data type. + // Conversion of the DateTime value 3/10/2009 12:00:00 AM to a Char is not supported. + // The UInt32 value 1934 converts to ?. + // The SByte value -17 is outside the range of the Char data type. + // Conversion of the Double value 169.34 to a Char is not supported. + // Conversion of the Decimal value 175.6 to a Char is not supported. + // Cannot convert a null reference to a Char. + // + } } diff --git a/snippets/csharp/System/Convert/ToDateTime/ToDateTime1.cs b/snippets/csharp/System/Convert/ToDateTime/ToDateTime1.cs index 94965d38d49..ea780a87b36 100644 --- a/snippets/csharp/System/Convert/ToDateTime/ToDateTime1.cs +++ b/snippets/csharp/System/Convert/ToDateTime/ToDateTime1.cs @@ -5,47 +5,49 @@ public class ConversionToDateTime { - public static void Main() - { - // Try converting an integer. - int number = 16352; - ConvertToDateTime(number); + public static void Main() + { + // Try converting an integer. + int number = 16352; + ConvertToDateTime(number); - // Convert a null. - object obj = null; - ConvertToDateTime(obj); + // Convert a null. + object obj = null; + ConvertToDateTime(obj); - // Convert a non-date string. - string nonDateString = "monthly"; - ConvertToDateTime(nonDateString); + // Convert a non-date string. + string nonDateString = "monthly"; + ConvertToDateTime(nonDateString); - // Try to convert various date strings. - string dateString; - dateString = "05/01/1996"; - ConvertToDateTime(dateString); - dateString = "Tue Apr 28, 2009"; - ConvertToDateTime(dateString); - dateString = "06 July 2008 7:32:47 AM"; - ConvertToDateTime(dateString); - dateString = "17:32:47.003"; - ConvertToDateTime(dateString); - } + // Try to convert various date strings. + string dateString; + dateString = "05/01/1996"; + ConvertToDateTime(dateString); + dateString = "Tue Apr 28, 2009"; + ConvertToDateTime(dateString); + dateString = "06 July 2008 7:32:47 AM"; + ConvertToDateTime(dateString); + dateString = "17:32:47.003"; + ConvertToDateTime(dateString); + } - private static void ConvertToDateTime(object value) - { - DateTime convertedDate; - try { - convertedDate = Convert.ToDateTime(value); - Console.WriteLine("'{0}' converts to {1}.", value, convertedDate); - } - catch (FormatException) { - Console.WriteLine("'{0}' is not in the proper format.", value); - } - catch (InvalidCastException) { - Console.WriteLine("Conversion of the {0} '{1}' is not supported", - value.GetType().Name, value); - } - } + private static void ConvertToDateTime(object value) + { + DateTime convertedDate; + try + { + convertedDate = Convert.ToDateTime(value); + Console.WriteLine($"'{value}' converts to {convertedDate}."); + } + catch (FormatException) + { + Console.WriteLine($"'{value}' is not in the proper format."); + } + catch (InvalidCastException) + { + Console.WriteLine($"Conversion of the {value.GetType().Name} '{value}' is not supported"); + } + } } // The example displays the following output: // Conversion of the Int32 '16352' is not supported diff --git a/snippets/csharp/System/Convert/ToDateTime/ToDateTime2.cs b/snippets/csharp/System/Convert/ToDateTime/ToDateTime2.cs index 1dc31c220dc..79828753901 100644 --- a/snippets/csharp/System/Convert/ToDateTime/ToDateTime2.cs +++ b/snippets/csharp/System/Convert/ToDateTime/ToDateTime2.cs @@ -5,53 +5,53 @@ public class ConversionToDateTime { - public static void Main() - { - string dateString = null; + public static void Main() + { + string dateString = null; - // Convert a null string. - ConvertToDateTime(dateString); + // Convert a null string. + ConvertToDateTime(dateString); - // Convert an empty string. - dateString = String.Empty; - ConvertToDateTime(dateString); + // Convert an empty string. + dateString = string.Empty; + ConvertToDateTime(dateString); - // Convert a non-date string. - dateString = "not a date"; - ConvertToDateTime(dateString); + // Convert a non-date string. + dateString = "not a date"; + ConvertToDateTime(dateString); - // Try to convert various date strings. - dateString = "05/01/1996"; - ConvertToDateTime(dateString); - dateString = "Tue Apr 28, 2009"; - ConvertToDateTime(dateString); - dateString = "Wed Apr 28, 2009"; - ConvertToDateTime(dateString); - dateString = "06 July 2008 7:32:47 AM"; - ConvertToDateTime(dateString); - dateString = "17:32:47.003"; - ConvertToDateTime(dateString); - // Convert a string returned by DateTime.ToString("R"). - dateString = "Sat, 10 May 2008 14:32:17 GMT"; - ConvertToDateTime(dateString); - // Convert a string returned by DateTime.ToString("o"). - dateString = "2009-05-01T07:54:59.9843750-04:00"; - ConvertToDateTime(dateString); - } + // Try to convert various date strings. + dateString = "05/01/1996"; + ConvertToDateTime(dateString); + dateString = "Tue Apr 28, 2009"; + ConvertToDateTime(dateString); + dateString = "Wed Apr 28, 2009"; + ConvertToDateTime(dateString); + dateString = "06 July 2008 7:32:47 AM"; + ConvertToDateTime(dateString); + dateString = "17:32:47.003"; + ConvertToDateTime(dateString); + // Convert a string returned by DateTime.ToString("R"). + dateString = "Sat, 10 May 2008 14:32:17 GMT"; + ConvertToDateTime(dateString); + // Convert a string returned by DateTime.ToString("o"). + dateString = "2009-05-01T07:54:59.9843750-04:00"; + ConvertToDateTime(dateString); + } - private static void ConvertToDateTime(string value) - { - DateTime convertedDate; - try { - convertedDate = Convert.ToDateTime(value); - Console.WriteLine("'{0}' converts to {1} {2} time.", - value, convertedDate, - convertedDate.Kind.ToString()); - } - catch (FormatException) { - Console.WriteLine("'{0}' is not in the proper format.", value); - } - } + private static void ConvertToDateTime(string value) + { + DateTime convertedDate; + try + { + convertedDate = Convert.ToDateTime(value); + Console.WriteLine($"'{value}' converts to {convertedDate} {convertedDate.Kind.ToString()} time."); + } + catch (FormatException) + { + Console.WriteLine($"'{value}' is not in the proper format."); + } + } } // The example displays the following output: // '' converts to 1/1/0001 12:00:00 AM Unspecified time. diff --git a/snippets/csharp/System/Convert/ToDateTime/ToDateTime3.cs b/snippets/csharp/System/Convert/ToDateTime/ToDateTime3.cs index 4c3aad71490..e9f7302fe22 100644 --- a/snippets/csharp/System/Convert/ToDateTime/ToDateTime3.cs +++ b/snippets/csharp/System/Convert/ToDateTime/ToDateTime3.cs @@ -4,36 +4,36 @@ public class Example { - public static void Main() - { - Console.WriteLine("{0,-18}{1,-12}{2}\n", "Date String", "Culture", "Result"); + public static void Main() + { + Console.WriteLine($"{"Date String",-18}{"Culture",-12}{"Result"}\n"); - string[] cultureNames = { "en-US", "ru-RU","ja-JP" }; - string[] dateStrings = { "01/02/09", "2009/02/03", "01/2009/03", + string[] cultureNames = { "en-US", "ru-RU", "ja-JP" }; + string[] dateStrings = { "01/02/09", "2009/02/03", "01/2009/03", "01/02/2009", "21/02/09", "01/22/09", "01/02/23" }; - // Iterate each culture name in the array. - foreach (string cultureName in cultureNames) - { - CultureInfo culture = new CultureInfo(cultureName); + // Iterate each culture name in the array. + foreach (string cultureName in cultureNames) + { + CultureInfo culture = new(cultureName); - // Parse each date using the designated culture. - foreach (string dateStr in dateStrings) - { - DateTime dateTimeValue; - try { - dateTimeValue = Convert.ToDateTime(dateStr, culture); - // Display the date and time in a fixed format. - Console.WriteLine("{0,-18}{1,-12}{2:yyyy-MMM-dd}", - dateStr, cultureName, dateTimeValue); + // Parse each date using the designated culture. + foreach (string dateStr in dateStrings) + { + DateTime dateTimeValue; + try + { + dateTimeValue = Convert.ToDateTime(dateStr, culture); + // Display the date and time in a fixed format. + Console.WriteLine($"{dateStr,-18}{cultureName,-12}{dateTimeValue:yyyy-MMM-dd}"); + } + catch (FormatException e) + { + Console.WriteLine($"{dateStr,-18}{cultureName,-12}{e.GetType().Name}"); + } } - catch (FormatException e) { - Console.WriteLine("{0,-18}{1,-12}{2}", - dateStr, cultureName, e.GetType().Name); - } - } - Console.WriteLine(); - } - } + Console.WriteLine(); + } + } } // diff --git a/snippets/csharp/System/Convert/ToDateTime/todatetime4.cs b/snippets/csharp/System/Convert/ToDateTime/todatetime4.cs index 9d0dbc735dc..c7acc600170 100644 --- a/snippets/csharp/System/Convert/ToDateTime/todatetime4.cs +++ b/snippets/csharp/System/Convert/ToDateTime/todatetime4.cs @@ -4,57 +4,56 @@ public class Example { - public static void Main() - { - string[] cultureNames = { "en-US", "hu-HU", "pt-PT" }; - object[] objects = { 12, 17.2, false, new DateTime(2010, 1, 1), "today", + public static void Main() + { + string[] cultureNames = { "en-US", "hu-HU", "pt-PT" }; + object[] objects = { 12, 17.2, false, new DateTime(2010, 1, 1), "today", new System.Collections.ArrayList(), 'c', "05/10/2009 6:13:18 PM", "September 8, 1899" }; - foreach (string cultureName in cultureNames) - { - Console.WriteLine("{0} culture:", cultureName); - CustomProvider provider = new CustomProvider(cultureName); - foreach (object obj in objects) - { - try { - DateTime dateValue = Convert.ToDateTime(obj, provider); - Console.WriteLine("{0} --> {1}", obj, - dateValue.ToString(new CultureInfo(cultureName))); + foreach (string cultureName in cultureNames) + { + Console.WriteLine($"{cultureName} culture:"); + CustomProvider provider = new(cultureName); + foreach (object obj in objects) + { + try + { + DateTime dateValue = Convert.ToDateTime(obj, provider); + Console.WriteLine($"{obj} --> {dateValue.ToString(new CultureInfo(cultureName))}"); + } + catch (FormatException) + { + Console.WriteLine($"{obj} --> Bad Format"); + } + catch (InvalidCastException) + { + Console.WriteLine($"{obj} --> Conversion Not Supported"); + } } - catch (FormatException) { - Console.WriteLine("{0} --> Bad Format", obj); - } - catch (InvalidCastException) { - Console.WriteLine("{0} --> Conversion Not Supported", obj); - } - } - Console.WriteLine(); - } - } + Console.WriteLine(); + } + } } public class CustomProvider : IFormatProvider { - private string cultureName; + private string cultureName; - public CustomProvider(string cultureName) - { - this.cultureName = cultureName; - } + public CustomProvider(string cultureName) => this.cultureName = cultureName; - public object GetFormat(Type formatType) - { - if (formatType == typeof(DateTimeFormatInfo)) - { - Console.Write("(CustomProvider retrieved.) "); - return new CultureInfo(cultureName).GetFormat(formatType); - } - else - { - return null; - } - } + public object GetFormat(Type formatType) + { + if (formatType == typeof(DateTimeFormatInfo)) + { + Console.Write("(CustomProvider retrieved.) "); + return new CultureInfo(cultureName).GetFormat(formatType); + } + else + { + return null; + } + } } // The example displays the following output: // en-US culture: diff --git a/snippets/csharp/System/Convert/ToDecimal/ToDecimal1.cs b/snippets/csharp/System/Convert/ToDecimal/ToDecimal1.cs index 504c97987bb..6543141fc19 100644 --- a/snippets/csharp/System/Convert/ToDecimal/ToDecimal1.cs +++ b/snippets/csharp/System/Convert/ToDecimal/ToDecimal1.cs @@ -2,31 +2,31 @@ public class Class1 { - public static void Main() - { - ConvertSingleToDecimal(); - ConvertDoubleToDecimal(); - } + public static void Main() + { + ConvertSingleToDecimal(); + ConvertDoubleToDecimal(); + } - private static void ConvertSingleToDecimal() - { - // - Console.WriteLine(Convert.ToDecimal(1234567500.12F)); // Displays 1234568000 - Console.WriteLine(Convert.ToDecimal(1234568500.12F)); // Displays 1234568000 + private static void ConvertSingleToDecimal() + { + // + Console.WriteLine(Convert.ToDecimal(1234567500.12F)); // Displays 1234568000 + Console.WriteLine(Convert.ToDecimal(1234568500.12F)); // Displays 1234568000 - Console.WriteLine(Convert.ToDecimal(10.980365F)); // Displays 10.98036 - Console.WriteLine(Convert.ToDecimal(10.980355F)); // Displays 10.98036 - // - } + Console.WriteLine(Convert.ToDecimal(10.980365F)); // Displays 10.98036 + Console.WriteLine(Convert.ToDecimal(10.980355F)); // Displays 10.98036 + // + } - private static void ConvertDoubleToDecimal() - { - // - Console.WriteLine(Convert.ToDecimal(123456789012345500.12D)); // Displays 123456789012346000 - Console.WriteLine(Convert.ToDecimal(123456789012346500.12D)); // Displays 123456789012346000 + private static void ConvertDoubleToDecimal() + { + // + Console.WriteLine(Convert.ToDecimal(123456789012345500.12D)); // Displays 123456789012346000 + Console.WriteLine(Convert.ToDecimal(123456789012346500.12D)); // Displays 123456789012346000 - Console.WriteLine(Convert.ToDecimal(10030.12345678905D)); // Displays 10030.123456789 - Console.WriteLine(Convert.ToDecimal(10030.12345678915D)); // Displays 10030.1234567892 - // - } + Console.WriteLine(Convert.ToDecimal(10030.12345678905D)); // Displays 10030.123456789 + Console.WriteLine(Convert.ToDecimal(10030.12345678915D)); // Displays 10030.1234567892 + // + } } diff --git a/snippets/csharp/System/Convert/ToDecimal/todecimal11.cs b/snippets/csharp/System/Convert/ToDecimal/todecimal11.cs index 91d9fde2f9d..3d4403e211b 100644 --- a/snippets/csharp/System/Convert/ToDecimal/todecimal11.cs +++ b/snippets/csharp/System/Convert/ToDecimal/todecimal11.cs @@ -2,233 +2,230 @@ public class Example { - public static void Main() - { - ConvertBoolean(); - Console.WriteLine("-----"); - ConvertInt16(); - Console.WriteLine("-----"); - ConvertInt32(); - Console.WriteLine("-----"); - ConvertObject(); - Console.WriteLine("-----"); - ConvertSByte(); - Console.WriteLine("-----"); - ConvertSingle(); - Console.WriteLine("-----"); - ConvertUInt16(); - Console.WriteLine("-----"); - ConvertUInt32(); - Console.WriteLine("-----"); - ConvertUInt64(); - } - - private static void ConvertBoolean() - { - // - bool[] flags = { true, false }; - decimal result; - - foreach (bool flag in flags) - { - result = Convert.ToDecimal(flag); - Console.WriteLine("Converted {0} to {1}.", flag, result); - } - // The example displays the following output: - // Converted True to 1. - // Converted False to 0. - // - } - - private static void ConvertInt16() - { - // - short[] numbers = { Int16.MinValue, -1000, 0, 1000, Int16.MaxValue }; - decimal result; - - foreach (short number in numbers) - { - result = Convert.ToDecimal(number); - Console.WriteLine("Converted the Int16 value {0} to the Decimal value {1}.", - number, result); - } - // The example displays the following output: - // Converted the Int16 value -32768 to the Decimal value -32768. - // Converted the Int16 value -1000 to the Decimal value -1000. - // Converted the Int16 value 0 to the Decimal value 0. - // Converted the Int16 value 1000 to the Decimal value 1000. - // Converted the Int16 value 32767 to the Decimal value 32767. - // - } - - private static void ConvertInt32() - { - // - int[] numbers = { Int32.MinValue, -1000, 0, 1000, Int32.MaxValue }; - decimal result; - - foreach (int number in numbers) - { - result = Convert.ToDecimal(number); - Console.WriteLine("Converted the Int32 value {0} to the Decimal value {1}.", - number, result); - } - // The example displays the following output: - // Converted the Int32 value -2147483648 to the Decimal value -2147483648. - // Converted the Int32 value -1000 to the Decimal value -1000. - // Converted the Int32 value 0 to the Decimal value 0. - // Converted the Int32 value 1000 to the Decimal value 1000. - // Converted the Int32 value 2147483647 to the Decimal value 2147483647. - // - } - - private static void ConvertObject() - { - // - object[] values = { true, 'a', 123, 1.764e32, "9.78", "1e-02", + public static void Main() + { + ConvertBoolean(); + Console.WriteLine("-----"); + ConvertInt16(); + Console.WriteLine("-----"); + ConvertInt32(); + Console.WriteLine("-----"); + ConvertObject(); + Console.WriteLine("-----"); + ConvertSByte(); + Console.WriteLine("-----"); + ConvertSingle(); + Console.WriteLine("-----"); + ConvertUInt16(); + Console.WriteLine("-----"); + ConvertUInt32(); + Console.WriteLine("-----"); + ConvertUInt64(); + } + + private static void ConvertBoolean() + { + // + bool[] flags = { true, false }; + decimal result; + + foreach (bool flag in flags) + { + result = Convert.ToDecimal(flag); + Console.WriteLine($"Converted {flag} to {result}."); + } + // The example displays the following output: + // Converted True to 1. + // Converted False to 0. + // + } + + private static void ConvertInt16() + { + // + short[] numbers = { short.MinValue, -1000, 0, 1000, short.MaxValue }; + decimal result; + + foreach (short number in numbers) + { + result = Convert.ToDecimal(number); + Console.WriteLine($"Converted the Int16 value {number} to the Decimal value {result}."); + } + // The example displays the following output: + // Converted the Int16 value -32768 to the Decimal value -32768. + // Converted the Int16 value -1000 to the Decimal value -1000. + // Converted the Int16 value 0 to the Decimal value 0. + // Converted the Int16 value 1000 to the Decimal value 1000. + // Converted the Int16 value 32767 to the Decimal value 32767. + // + } + + private static void ConvertInt32() + { + // + int[] numbers = { int.MinValue, -1000, 0, 1000, int.MaxValue }; + decimal result; + + foreach (int number in numbers) + { + result = Convert.ToDecimal(number); + Console.WriteLine($"Converted the Int32 value {number} to the Decimal value {result}."); + } + // The example displays the following output: + // Converted the Int32 value -2147483648 to the Decimal value -2147483648. + // Converted the Int32 value -1000 to the Decimal value -1000. + // Converted the Int32 value 0 to the Decimal value 0. + // Converted the Int32 value 1000 to the Decimal value 1000. + // Converted the Int32 value 2147483647 to the Decimal value 2147483647. + // + } + + private static void ConvertObject() + { + // + object[] values = { true, 'a', 123, 1.764e32, "9.78", "1e-02", 1.67e03, "A100", "1,033.67", DateTime.Now, - Double.MaxValue }; - decimal result; - - foreach (object value in values) - { - try { - result = Convert.ToDecimal(value); - Console.WriteLine("Converted the {0} value {1} to {2}.", - value.GetType().Name, value, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is out of range of the Decimal type.", - value.GetType().Name, value); - } - catch (FormatException) { - Console.WriteLine("The {0} value {1} is not recognized as a valid Decimal value.", - value.GetType().Name, value); - } - catch (InvalidCastException) { - Console.WriteLine("Conversion of the {0} value {1} to a Decimal is not supported.", - value.GetType().Name, value); - } - } - // The example displays the following output: - // Converted the Boolean value True to 1. - // Conversion of the Char value a to a Decimal is not supported. - // Converted the Int32 value 123 to 123. - // The Double value 1.764E+32 is out of range of the Decimal type. - // Converted the String value 9.78 to 9.78. - // The String value 1e-02 is not recognized as a valid Decimal value. - // Converted the Double value 1670 to 1670. - // The String value A100 is not recognized as a valid Decimal value. - // Converted the String value 1,033.67 to 1033.67. - // Conversion of the DateTime value 10/15/2008 05:40:42 PM to a Decimal is not supported. - // The Double value 1.79769313486232E+308 is out of range of the Decimal type. - // - } - - private static void ConvertSByte() - { - // - sbyte[] numbers = { SByte.MinValue, -23, 0, 17, SByte.MaxValue }; - decimal result; - - foreach (sbyte number in numbers) - { - result = Convert.ToDecimal(number); - Console.WriteLine("Converted the SByte value {0} to {1}.", number, result); - } - // Converted the SByte value -128 to -128. - // Converted the SByte value -23 to -23. - // Converted the SByte value 0 to 0. - // Converted the SByte value 17 to 17. - // Converted the SByte value 127 to 127. - // - } - - private static void ConvertSingle() - { - // - float[] numbers = { Single.MinValue, -3e10f, -1093.54f, 0f, 1e-03f, - 1034.23f, Single.MaxValue }; - decimal result; - - foreach (float number in numbers) - { - try { + double.MaxValue }; + decimal result; + + foreach (object value in values) + { + try + { + result = Convert.ToDecimal(value); + Console.WriteLine($"Converted the {value.GetType().Name} value {value} to {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {value.GetType().Name} value {value} is out of range of the Decimal type."); + } + catch (FormatException) + { + Console.WriteLine($"The {value.GetType().Name} value {value} is not recognized as a valid Decimal value."); + } + catch (InvalidCastException) + { + Console.WriteLine($"Conversion of the {value.GetType().Name} value {value} to a Decimal is not supported."); + } + } + // The example displays the following output: + // Converted the Boolean value True to 1. + // Conversion of the Char value a to a Decimal is not supported. + // Converted the Int32 value 123 to 123. + // The Double value 1.764E+32 is out of range of the Decimal type. + // Converted the String value 9.78 to 9.78. + // The String value 1e-02 is not recognized as a valid Decimal value. + // Converted the Double value 1670 to 1670. + // The String value A100 is not recognized as a valid Decimal value. + // Converted the String value 1,033.67 to 1033.67. + // Conversion of the DateTime value 10/15/2008 05:40:42 PM to a Decimal is not supported. + // The Double value 1.79769313486232E+308 is out of range of the Decimal type. + // + } + + private static void ConvertSByte() + { + // + sbyte[] numbers = { sbyte.MinValue, -23, 0, 17, sbyte.MaxValue }; + decimal result; + + foreach (sbyte number in numbers) + { + result = Convert.ToDecimal(number); + Console.WriteLine($"Converted the SByte value {number} to {result}."); + } + // Converted the SByte value -128 to -128. + // Converted the SByte value -23 to -23. + // Converted the SByte value 0 to 0. + // Converted the SByte value 17 to 17. + // Converted the SByte value 127 to 127. + // + } + + private static void ConvertSingle() + { + // + float[] numbers = { float.MinValue, -3e10f, -1093.54f, 0f, 1e-03f, + 1034.23f, float.MaxValue }; + decimal result; + + foreach (float number in numbers) + { + try + { + result = Convert.ToDecimal(number); + Console.WriteLine($"Converted the Single value {number} to {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"{number} is out of range of the Decimal type."); + } + } + // The example displays the following output: + // -3.402823E+38 is out of range of the Decimal type. + // Converted the Single value -3E+10 to -30000000000. + // Converted the Single value -1093.54 to -1093.54. + // Converted the Single value 0 to 0. + // Converted the Single value 0.001 to 0.001. + // Converted the Single value 1034.23 to 1034.23. + // 3.402823E+38 is out of range of the Decimal type. + // + } + + private static void ConvertUInt16() + { + // + ushort[] numbers = { ushort.MinValue, 121, 12345, ushort.MaxValue }; + decimal result; + + foreach (ushort number in numbers) + { + result = Convert.ToDecimal(number); + Console.WriteLine($"Converted the UInt16 value {number} to {result}."); + } + // The example displays the following output: + // Converted the UInt16 value 0 to 0. + // Converted the UInt16 value 121 to 121. + // Converted the UInt16 value 12345 to 12345. + // Converted the UInt16 value 65535 to 65535. + // + } + + private static void ConvertUInt32() + { + // + uint[] numbers = { uint.MinValue, 121, 12345, uint.MaxValue }; + decimal result; + + foreach (uint number in numbers) + { + result = Convert.ToDecimal(number); + Console.WriteLine($"Converted the UInt32 value {number} to {result}."); + } + // The example displays the following output: + // Converted the UInt32 value 0 to 0. + // Converted the UInt32 value 121 to 121. + // Converted the UInt32 value 12345 to 12345. + // Converted the UInt32 value 4294967295 to 4294967295. + // + } + + private static void ConvertUInt64() + { + // + ulong[] numbers = { ulong.MinValue, 121, 12345, ulong.MaxValue }; + decimal result; + + foreach (ulong number in numbers) + { result = Convert.ToDecimal(number); - Console.WriteLine("Converted the Single value {0} to {1}.", number, result); - } - catch (OverflowException) { - Console.WriteLine("{0} is out of range of the Decimal type.", number); - } - } - // The example displays the following output: - // -3.402823E+38 is out of range of the Decimal type. - // Converted the Single value -3E+10 to -30000000000. - // Converted the Single value -1093.54 to -1093.54. - // Converted the Single value 0 to 0. - // Converted the Single value 0.001 to 0.001. - // Converted the Single value 1034.23 to 1034.23. - // 3.402823E+38 is out of range of the Decimal type. - // - } - - private static void ConvertUInt16() - { - // - ushort[] numbers = { UInt16.MinValue, 121, 12345, UInt16.MaxValue }; - decimal result; - - foreach (ushort number in numbers) - { - result = Convert.ToDecimal(number); - Console.WriteLine("Converted the UInt16 value {0} to {1}.", - number, result); - } - // The example displays the following output: - // Converted the UInt16 value 0 to 0. - // Converted the UInt16 value 121 to 121. - // Converted the UInt16 value 12345 to 12345. - // Converted the UInt16 value 65535 to 65535. - // - } - - private static void ConvertUInt32() - { - // - uint[] numbers = { UInt32.MinValue, 121, 12345, UInt32.MaxValue }; - decimal result; - - foreach (uint number in numbers) - { - result = Convert.ToDecimal(number); - Console.WriteLine("Converted the UInt32 value {0} to {1}.", - number, result); - } - // The example displays the following output: - // Converted the UInt32 value 0 to 0. - // Converted the UInt32 value 121 to 121. - // Converted the UInt32 value 12345 to 12345. - // Converted the UInt32 value 4294967295 to 4294967295. - // - } - - private static void ConvertUInt64() - { - // - ulong[] numbers = { UInt64.MinValue, 121, 12345, UInt64.MaxValue }; - decimal result; - - foreach (ulong number in numbers) - { - result = Convert.ToDecimal(number); - Console.WriteLine("Converted the UInt64 value {0} to {1}.", - number, result); - } - // The example displays the following output: - // Converted the UInt64 value 0 to 0. - // Converted the UInt64 value 121 to 121. - // Converted the UInt64 value 12345 to 12345. - // Converted the UInt64 value 18446744073709551615 to 18446744073709551615. - // - } + Console.WriteLine($"Converted the UInt64 value {number} to {result}."); + } + // The example displays the following output: + // Converted the UInt64 value 0 to 0. + // Converted the UInt64 value 121 to 121. + // Converted the UInt64 value 12345 to 12345. + // Converted the UInt64 value 18446744073709551615 to 18446744073709551615. + // + } } diff --git a/snippets/csharp/System/Convert/ToDecimal/todecimal2.cs b/snippets/csharp/System/Convert/ToDecimal/todecimal2.cs index a8426456d9e..10149bf0e5b 100644 --- a/snippets/csharp/System/Convert/ToDecimal/todecimal2.cs +++ b/snippets/csharp/System/Convert/ToDecimal/todecimal2.cs @@ -1,212 +1,167 @@ // using System; -using System.Globalization; + public class Temperature : IConvertible { - private decimal m_Temp; - - public Temperature(decimal temperature) - { - this.m_Temp = temperature; - } - - public decimal Celsius - { - get { return this.m_Temp; } - } - - public decimal Kelvin - { - get { return this.m_Temp + 273.15m; } - } - - public decimal Fahrenheit - { - get { return Math.Round((decimal) (this.m_Temp * 9 / 5 + 32), 2); } - } - - public override string ToString() - { - return m_Temp.ToString("N2") + " °C"; - } - - // IConvertible implementations. - public TypeCode GetTypeCode() - { - return TypeCode.Object; - } - - public bool ToBoolean(IFormatProvider provider) - { - if (m_Temp == 0) - return false; - else - return true; - } - - public byte ToByte(IFormatProvider provider) - { - if (m_Temp < Byte.MinValue || m_Temp > Byte.MaxValue) - throw new OverflowException(String.Format("{0} is out of range of the Byte type.", - this.m_Temp)); - else - return Decimal.ToByte(this.m_Temp); - } - - public char ToChar(IFormatProvider provider) - { - throw new InvalidCastException("Temperature to Char conversion is not supported."); - } - - public DateTime ToDateTime(IFormatProvider provider) - { - throw new InvalidCastException("Temperature to DateTime conversion is not supported."); - } - - public decimal ToDecimal(IFormatProvider provider) - { - return this.m_Temp; - } - - public double ToDouble(IFormatProvider provider) - { - return Decimal.ToDouble(this.m_Temp); - } - - public short ToInt16(IFormatProvider provider) - { - if (this.m_Temp < Int16.MinValue || this.m_Temp > Int16.MaxValue) - throw new OverflowException(String.Format("{0} is out of range of the Int16 type.", - this.m_Temp)); - else - return Decimal.ToInt16(this.m_Temp); - } - - public int ToInt32(IFormatProvider provider) - { - if (this.m_Temp < Int32.MinValue || this.m_Temp > Int32.MaxValue) - throw new OverflowException(String.Format("{0} is out of range of the Int32 type.", - this.m_Temp)); - else - return Decimal.ToInt32(this.m_Temp); - } - - public long ToInt64(IFormatProvider provider) - { - if (this.m_Temp < Int64.MinValue || this.m_Temp > Int64.MaxValue) - throw new OverflowException(String.Format("{0} is out of range of the Int64 type.", - this.m_Temp)); - else - return Decimal.ToInt64(this.m_Temp); - } - - public sbyte ToSByte(IFormatProvider provider) - { - if (this.m_Temp < SByte.MinValue || this.m_Temp > SByte.MaxValue) - throw new OverflowException(String.Format("{0} is out of range of the SByte type.", - this.m_Temp)); - else - return Decimal.ToSByte(this.m_Temp); - } - - public float ToSingle(IFormatProvider provider) - { - return Decimal.ToSingle(this.m_Temp); - } - - public string ToString(IFormatProvider provider) - { - return m_Temp.ToString("N2", provider) + " °C"; - } - - public object ToType(Type conversionType, IFormatProvider provider) - { - switch (Type.GetTypeCode(conversionType)) - { - case TypeCode.Boolean: - return this.ToBoolean(null); - case TypeCode.Byte: - return this.ToByte(null); - case TypeCode.Char: - return this.ToChar(null); - case TypeCode.DateTime: - return this.ToDateTime(null); - case TypeCode.Decimal: - return this.ToDecimal(null); - case TypeCode.Double: - return this.ToDouble(null); - case TypeCode.Int16: - return this.ToInt16(null); - case TypeCode.Int32: - return this.ToInt32(null); - case TypeCode.Int64: - return this.ToInt64(null); - case TypeCode.Object: - if (typeof(Temperature).Equals(conversionType)) - return this; - else - throw new InvalidCastException(String.Format("Conversion to a {0} is not supported.", - conversionType.Name)); - case TypeCode.SByte: - return this.ToSByte(null); - case TypeCode.Single: - return this.ToSingle(null); - case TypeCode.String: - return this.ToString(provider); - case TypeCode.UInt16: - return this.ToUInt16(null); - case TypeCode.UInt32: - return this.ToUInt32(null); - case TypeCode.UInt64: - return this.ToUInt64(null); - default: - throw new InvalidCastException(String.Format("Conversion to {0} is not supported.", conversionType.Name)); - } - } - - public ushort ToUInt16(IFormatProvider provider) - { - if (this.m_Temp < UInt16.MinValue || this.m_Temp > UInt16.MaxValue) - throw new OverflowException(String.Format("{0} is out of range of the UInt16 type.", - this.m_Temp)); - else - return Decimal.ToUInt16(this.m_Temp); - } - - public uint ToUInt32(IFormatProvider provider) - { - if (this.m_Temp < UInt32.MinValue || this.m_Temp > UInt32.MaxValue) - throw new OverflowException(String.Format("{0} is out of range of the UInt32 type.", - this.m_Temp)); - else - return Decimal.ToUInt32(this.m_Temp); - } - - public ulong ToUInt64(IFormatProvider provider) - { - if (this.m_Temp < UInt64.MinValue || this.m_Temp > UInt64.MaxValue) - throw new OverflowException(String.Format("{0} is out of range of the UInt64 type.", - this.m_Temp)); - else - return Decimal.ToUInt64(this.m_Temp); - } + private decimal m_Temp; + + public Temperature(decimal temperature) => this.m_Temp = temperature; + + public decimal Celsius => this.m_Temp; + + public decimal Kelvin => this.m_Temp + 273.15m; + + public decimal Fahrenheit => Math.Round((decimal)(this.m_Temp * 9 / 5 + 32), 2); + + public override string ToString() => m_Temp.ToString("N2") + " °C"; + + // IConvertible implementations. + public TypeCode GetTypeCode() => TypeCode.Object; + + public bool ToBoolean(IFormatProvider provider) + { + if (m_Temp == 0) + return false; + else + return true; + } + + public byte ToByte(IFormatProvider provider) + { + if (m_Temp < byte.MinValue || m_Temp > byte.MaxValue) + throw new OverflowException($"{this.m_Temp} is out of range of the Byte type."); + else + return decimal.ToByte(this.m_Temp); + } + + public char ToChar(IFormatProvider provider) => throw new InvalidCastException("Temperature to Char conversion is not supported."); + + public DateTime ToDateTime(IFormatProvider provider) => throw new InvalidCastException("Temperature to DateTime conversion is not supported."); + + public decimal ToDecimal(IFormatProvider provider) => this.m_Temp; + + public double ToDouble(IFormatProvider provider) => decimal.ToDouble(this.m_Temp); + + public short ToInt16(IFormatProvider provider) + { + if (this.m_Temp < short.MinValue || this.m_Temp > short.MaxValue) + throw new OverflowException($"{this.m_Temp} is out of range of the Int16 type."); + else + return decimal.ToInt16(this.m_Temp); + } + + public int ToInt32(IFormatProvider provider) + { + if (this.m_Temp < int.MinValue || this.m_Temp > int.MaxValue) + throw new OverflowException($"{this.m_Temp} is out of range of the Int32 type."); + else + return decimal.ToInt32(this.m_Temp); + } + + public long ToInt64(IFormatProvider provider) + { + if (this.m_Temp < long.MinValue || this.m_Temp > long.MaxValue) + throw new OverflowException($"{this.m_Temp} is out of range of the Int64 type."); + else + return decimal.ToInt64(this.m_Temp); + } + + public sbyte ToSByte(IFormatProvider provider) + { + if (this.m_Temp < sbyte.MinValue || this.m_Temp > sbyte.MaxValue) + throw new OverflowException($"{this.m_Temp} is out of range of the SByte type."); + else + return decimal.ToSByte(this.m_Temp); + } + + public float ToSingle(IFormatProvider provider) => decimal.ToSingle(this.m_Temp); + + public string ToString(IFormatProvider provider) => m_Temp.ToString("N2", provider) + " °C"; + + public object ToType(Type conversionType, IFormatProvider provider) + { + switch (Type.GetTypeCode(conversionType)) + { + case TypeCode.Boolean: + return this.ToBoolean(null); + case TypeCode.Byte: + return this.ToByte(null); + case TypeCode.Char: + return this.ToChar(null); + case TypeCode.DateTime: + return this.ToDateTime(null); + case TypeCode.Decimal: + return this.ToDecimal(null); + case TypeCode.Double: + return this.ToDouble(null); + case TypeCode.Int16: + return this.ToInt16(null); + case TypeCode.Int32: + return this.ToInt32(null); + case TypeCode.Int64: + return this.ToInt64(null); + case TypeCode.Object: + if (typeof(Temperature).Equals(conversionType)) + return this; + else + throw new InvalidCastException($"Conversion to a {conversionType.Name} is not supported."); + case TypeCode.SByte: + return this.ToSByte(null); + case TypeCode.Single: + return this.ToSingle(null); + case TypeCode.String: + return this.ToString(provider); + case TypeCode.UInt16: + return this.ToUInt16(null); + case TypeCode.UInt32: + return this.ToUInt32(null); + case TypeCode.UInt64: + return this.ToUInt64(null); + default: + throw new InvalidCastException($"Conversion to {conversionType.Name} is not supported."); + } + } + + public ushort ToUInt16(IFormatProvider provider) + { + if (this.m_Temp < ushort.MinValue || this.m_Temp > ushort.MaxValue) + throw new OverflowException($"{this.m_Temp} is out of range of the UInt16 type."); + else + return decimal.ToUInt16(this.m_Temp); + } + + public uint ToUInt32(IFormatProvider provider) + { + if (this.m_Temp < uint.MinValue || this.m_Temp > uint.MaxValue) + throw new OverflowException($"{this.m_Temp} is out of range of the UInt32 type."); + else + return decimal.ToUInt32(this.m_Temp); + } + + public ulong ToUInt64(IFormatProvider provider) + { + if (this.m_Temp < ulong.MinValue || this.m_Temp > ulong.MaxValue) + throw new OverflowException($"{this.m_Temp} is out of range of the UInt64 type."); + else + return decimal.ToUInt64(this.m_Temp); + } } // // public class Example { - public static void Main() - { - Temperature cold = new Temperature(-40); - Temperature freezing = new Temperature(0); - Temperature boiling = new Temperature(100); - - Console.WriteLine(Convert.ToDecimal(cold, null)); - Console.WriteLine(Convert.ToDecimal(freezing, null)); - Console.WriteLine(Convert.ToDecimal(boiling, null)); - } + public static void Main() + { + Temperature cold = new(-40); + Temperature freezing = new(0); + Temperature boiling = new(100); + + Console.WriteLine(Convert.ToDecimal(cold, null)); + Console.WriteLine(Convert.ToDecimal(freezing, null)); + Console.WriteLine(Convert.ToDecimal(boiling, null)); + } } // The example dosplays the following output: // -40 diff --git a/snippets/csharp/System/Convert/ToDecimal/todecimal3.cs b/snippets/csharp/System/Convert/ToDecimal/todecimal3.cs index 9eb0fbe825a..3ad9faed816 100644 --- a/snippets/csharp/System/Convert/ToDecimal/todecimal3.cs +++ b/snippets/csharp/System/Convert/ToDecimal/todecimal3.cs @@ -4,31 +4,32 @@ public class Example { - public static void Main() - { - string[] values = { "123456789", "12345.6789", "12 345,6789", + public static void Main() + { + string[] values = { "123456789", "12345.6789", "12 345,6789", "123,456.789", "123 456,789", "123,456,789.0123", "123 456 789,0123" }; - CultureInfo[] cultures = { new CultureInfo("en-US"), + CultureInfo[] cultures = { new CultureInfo("en-US"), new CultureInfo("fr-FR") }; - foreach (CultureInfo culture in cultures) - { - Console.WriteLine("String -> Decimal Conversion Using the {0} Culture", - culture.Name); - foreach (string value in values) - { - Console.Write("{0,20} -> ", value); - try { - Console.WriteLine(Convert.ToDecimal(value, culture)); + foreach (CultureInfo culture in cultures) + { + Console.WriteLine($"String -> Decimal Conversion Using the {culture.Name} Culture"); + foreach (string value in values) + { + Console.Write($"{value,20} -> "); + try + { + Console.WriteLine(Convert.ToDecimal(value, culture)); + } + catch (FormatException) + { + Console.WriteLine("FormatException"); + } } - catch (FormatException) { - Console.WriteLine("FormatException"); - } - } - Console.WriteLine(); - } - } + Console.WriteLine(); + } + } } // The example displays the following output: // String -> Decimal Conversion Using the en-US Culture diff --git a/snippets/csharp/System/Convert/ToDouble/example8.cs b/snippets/csharp/System/Convert/ToDouble/example8.cs index f50897f7734..51a4422c1d3 100644 --- a/snippets/csharp/System/Convert/ToDouble/example8.cs +++ b/snippets/csharp/System/Convert/ToDouble/example8.cs @@ -3,27 +3,30 @@ public class Example { - public static void Main() - { - string[] values= { "-1,035.77219", "1AFF", "1e-35", + public static void Main() + { + string[] values = { "-1,035.77219", "1AFF", "1e-35", "1,635,592,999,999,999,999,999,999", "-17.455", "190.34001", "1.29e325"}; - double result; + double result; - foreach (string value in values) - { - try { - result = Convert.ToDouble(value); - Console.WriteLine("Converted '{0}' to {1}.", value, result); - } - catch (FormatException) { - Console.WriteLine("Unable to convert '{0}' to a Double.", value); - } - catch (OverflowException) { - Console.WriteLine("'{0}' is outside the range of a Double.", value); - } - } - } + foreach (string value in values) + { + try + { + result = Convert.ToDouble(value); + Console.WriteLine($"Converted '{value}' to {result}."); + } + catch (FormatException) + { + Console.WriteLine($"Unable to convert '{value}' to a Double."); + } + catch (OverflowException) + { + Console.WriteLine($"'{value}' is outside the range of a Double."); + } + } + } } // The example displays the following output: // Converted '-1,035.77219' to -1035.77219. diff --git a/snippets/csharp/System/Convert/ToDouble/todouble.cs b/snippets/csharp/System/Convert/ToDouble/todouble.cs index 448d47dfdb3..7140f0daf0d 100644 --- a/snippets/csharp/System/Convert/ToDouble/todouble.cs +++ b/snippets/csharp/System/Convert/ToDouble/todouble.cs @@ -7,36 +7,41 @@ class Example static void Main() { // Create a NumberFormatInfo object and set some of its properties. - NumberFormatInfo provider = new NumberFormatInfo(); - provider.NumberDecimalSeparator = ","; - provider.NumberGroupSeparator = "."; - provider.NumberGroupSizes = new int[] { 3 }; + NumberFormatInfo provider = new() + { + NumberDecimalSeparator = ",", + NumberGroupSeparator = ".", + NumberGroupSizes = new int[] { 3 } + }; // Define an array of numeric strings to convert. - String[] values = { "123456789", "12345.6789", "12345,6789", + string[] values = { "123456789", "12345.6789", "12345,6789", "123,456.789", "123.456,789", "123,456,789.0123", "123.456.789,0123" }; - Console.WriteLine("Default Culture: {0}\n", - CultureInfo.CurrentCulture.Name); - Console.WriteLine("{0,-22} {1,-20} {2,-20}\n", "String to Convert", - "Default/Exception", "Provider/Exception"); + Console.WriteLine($"Default Culture: {CultureInfo.CurrentCulture.Name}\n"); + Console.WriteLine($"{"String to Convert",-22} {"Default/Exception",-20} {"Provider/Exception",-20}\n"); // Convert each string to a Double with and without the provider. - foreach (var value in values) { - Console.Write("{0,-22} ", value); - try { - Console.Write("{0,-20} ", Convert.ToDouble(value)); - } - catch (FormatException e) { - Console.Write("{0,-20} ", e.GetType().Name); - } - try { - Console.WriteLine("{0,-20} ", Convert.ToDouble(value, provider)); - } - catch (FormatException e) { - Console.WriteLine("{0,-20} ", e.GetType().Name); - } + foreach (string value in values) + { + Console.Write($"{value,-22} "); + try + { + Console.Write($"{Convert.ToDouble(value),-20} "); + } + catch (FormatException e) + { + Console.Write($"{e.GetType().Name,-20} "); + } + try + { + Console.WriteLine($"{Convert.ToDouble(value, provider),-20} "); + } + catch (FormatException e) + { + Console.WriteLine($"{e.GetType().Name,-20} "); + } } } } diff --git a/snippets/csharp/System/Convert/ToDouble/todouble1.cs b/snippets/csharp/System/Convert/ToDouble/todouble1.cs index 845ba447c8b..156de56fad7 100644 --- a/snippets/csharp/System/Convert/ToDouble/todouble1.cs +++ b/snippets/csharp/System/Convert/ToDouble/todouble1.cs @@ -2,213 +2,210 @@ public class Example { - public static void Main() - { - ConvertInt16(); - Console.WriteLine("-----"); - ConvertInt64(); - Console.WriteLine("-----"); - ConvertObject(); - Console.WriteLine("-----"); - ConvertSByte(); - Console.WriteLine("----"); - ConvertUInt16(); - Console.WriteLine("-----"); - ConvertUInt32(); - Console.WriteLine("------"); - ConvertUInt64(); - Console.WriteLine("-----"); - ConvertString(); - } - - private static void ConvertInt16() - { - // - short[] numbers = { Int16.MinValue, -1032, 0, 192, Int16.MaxValue }; - double result; - - foreach (short number in numbers) - { - result = Convert.ToDouble(number); - Console.WriteLine("Converted the UInt16 value {0} to {1}.", - number, result); - } - // Converted the UInt16 value -32768 to -32768. - // Converted the UInt16 value -1032 to -1032. - // Converted the UInt16 value 0 to 0. - // Converted the UInt16 value 192 to 192. - // Converted the UInt16 value 32767 to 32767. - // - } - - private static void ConvertInt64() - { - // - long[] numbers = { Int64.MinValue, -903, 0, 172, Int64.MaxValue}; - double result; - - foreach (long number in numbers) - { - result = Convert.ToDouble(number); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the Int64 value '-9223372036854775808' to the Double value -9.22337203685478E+18. - // Converted the Int64 value '-903' to the Double value -903. - // Converted the Int64 value '0' to the Double value 0. - // Converted the Int64 value '172' to the Double value 172. - // Converted the Int64 value '9223372036854775807' to the Double value 9.22337203685478E+18. - // - } - - private static void ConvertObject() - { - // - object[] values = { true, 'a', 123, 1.764e32f, "9.78", "1e-02", + public static void Main() + { + ConvertInt16(); + Console.WriteLine("-----"); + ConvertInt64(); + Console.WriteLine("-----"); + ConvertObject(); + Console.WriteLine("-----"); + ConvertSByte(); + Console.WriteLine("----"); + ConvertUInt16(); + Console.WriteLine("-----"); + ConvertUInt32(); + Console.WriteLine("------"); + ConvertUInt64(); + Console.WriteLine("-----"); + ConvertString(); + } + + private static void ConvertInt16() + { + // + short[] numbers = { short.MinValue, -1032, 0, 192, short.MaxValue }; + double result; + + foreach (short number in numbers) + { + result = Convert.ToDouble(number); + Console.WriteLine($"Converted the UInt16 value {number} to {result}."); + } + // Converted the UInt16 value -32768 to -32768. + // Converted the UInt16 value -1032 to -1032. + // Converted the UInt16 value 0 to 0. + // Converted the UInt16 value 192 to 192. + // Converted the UInt16 value 32767 to 32767. + // + } + + private static void ConvertInt64() + { + // + long[] numbers = { long.MinValue, -903, 0, 172, long.MaxValue }; + double result; + + foreach (long number in numbers) + { + result = Convert.ToDouble(number); + Console.WriteLine($"Converted the {number.GetType().Name} value '{number}' to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the Int64 value '-9223372036854775808' to the Double value -9.22337203685478E+18. + // Converted the Int64 value '-903' to the Double value -903. + // Converted the Int64 value '0' to the Double value 0. + // Converted the Int64 value '172' to the Double value 172. + // Converted the Int64 value '9223372036854775807' to the Double value 9.22337203685478E+18. + // + } + + private static void ConvertObject() + { + // + object[] values = { true, 'a', 123, 1.764e32f, "9.78", "1e-02", 1.67e03f, "A100", "1,033.67", DateTime.Now, - Decimal.MaxValue }; - double result; - - foreach (object value in values) - { - try { - result = Convert.ToDouble(value); - Console.WriteLine("Converted the {0} value {1} to {2}.", - value.GetType().Name, value, result); - } - catch (FormatException) { - Console.WriteLine("The {0} value {1} is not recognized as a valid Double value.", - value.GetType().Name, value); - } - catch (InvalidCastException) { - Console.WriteLine("Conversion of the {0} value {1} to a Double is not supported.", - value.GetType().Name, value); - } - } - // The example displays the following output: - // Converted the Boolean value True to 1. - // Conversion of the Char value a to a Double is not supported. - // Converted the Int32 value 123 to 123. - // Converted the Single value 1.764E+32 to 1.76399995098587E+32. - // Converted the String value 9.78 to 9.78. - // Converted the String value 1e-02 to 0.01. - // Converted the Single value 1670 to 1670. - // The String value A100 is not recognized as a valid Double value. - // Converted the String value 1,033.67 to 1033.67. - // Conversion of the DateTime value 10/21/2008 07:12:12 AM to a Double is not supported. - // Converted the Decimal value 79228162514264337593543950335 to 7.92281625142643E+28. - // - } - - private static void ConvertSByte() - { - // - sbyte[] numbers = { SByte.MinValue, -23, 0, 17, SByte.MaxValue }; - double result; - - foreach (sbyte number in numbers) - { - result = Convert.ToDouble(number); - Console.WriteLine("Converted the SByte value {0} to {1}.", number, result); - } - // Converted the SByte value -128 to -128. - // Converted the SByte value -23 to -23. - // Converted the SByte value 0 to 0. - // Converted the SByte value 17 to 17. - // Converted the SByte value 127 to 127. - // - } - - private static void ConvertUInt16() - { - // - ushort[] numbers = { UInt16.MinValue, 121, 12345, UInt16.MaxValue }; - double result; - - foreach (ushort number in numbers) - { - result = Convert.ToDouble(number); - Console.WriteLine("Converted the UInt16 value {0} to {1}.", - number, result); - } - // The example displays the following output: - // Converted the UInt16 value 0 to 0. - // Converted the UInt16 value 121 to 121. - // Converted the UInt16 value 12345 to 12345. - // Converted the UInt16 value 65535 to 65535. - // - } - - private static void ConvertUInt32() - { - // - uint[] numbers = { UInt32.MinValue, 121, 12345, UInt32.MaxValue }; - double result; - - foreach (uint number in numbers) - { - result = Convert.ToDouble(number); - Console.WriteLine("Converted the UInt32 value {0} to {1}.", - number, result); - } - // The example displays the following output: - // Converted the UInt32 value 0 to 0. - // Converted the UInt32 value 121 to 121. - // Converted the UInt32 value 12345 to 12345. - // Converted the UInt32 value 4294967295 to 4294967295. - // - } - - private static void ConvertUInt64() - { - // - ulong[] numbers = { UInt64.MinValue, 121, 12345, UInt64.MaxValue }; - double result; - - foreach (ulong number in numbers) - { - result = Convert.ToDouble(number); - Console.WriteLine("Converted the UInt64 value {0} to {1}.", - number, result); - } - // The example displays the following output: - // Converted the UInt64 value 0 to 0. - // Converted the UInt64 value 121 to 121. - // Converted the UInt64 value 12345 to 12345. - // Converted the UInt64 value 18446744073709551615 to 1.84467440737096E+19. - // - } - - // unused - private static void ConvertString() - { - string[] values= { "-1,035.77219", "1AFF", "1e-35", + decimal.MaxValue }; + double result; + + foreach (object value in values) + { + try + { + result = Convert.ToDouble(value); + Console.WriteLine($"Converted the {value.GetType().Name} value {value} to {result}."); + } + catch (FormatException) + { + Console.WriteLine($"The {value.GetType().Name} value {value} is not recognized as a valid Double value."); + } + catch (InvalidCastException) + { + Console.WriteLine($"Conversion of the {value.GetType().Name} value {value} to a Double is not supported."); + } + } + // The example displays the following output: + // Converted the Boolean value True to 1. + // Conversion of the Char value a to a Double is not supported. + // Converted the Int32 value 123 to 123. + // Converted the Single value 1.764E+32 to 1.76399995098587E+32. + // Converted the String value 9.78 to 9.78. + // Converted the String value 1e-02 to 0.01. + // Converted the Single value 1670 to 1670. + // The String value A100 is not recognized as a valid Double value. + // Converted the String value 1,033.67 to 1033.67. + // Conversion of the DateTime value 10/21/2008 07:12:12 AM to a Double is not supported. + // Converted the Decimal value 79228162514264337593543950335 to 7.92281625142643E+28. + // + } + + private static void ConvertSByte() + { + // + sbyte[] numbers = { sbyte.MinValue, -23, 0, 17, sbyte.MaxValue }; + double result; + + foreach (sbyte number in numbers) + { + result = Convert.ToDouble(number); + Console.WriteLine($"Converted the SByte value {number} to {result}."); + } + // Converted the SByte value -128 to -128. + // Converted the SByte value -23 to -23. + // Converted the SByte value 0 to 0. + // Converted the SByte value 17 to 17. + // Converted the SByte value 127 to 127. + // + } + + private static void ConvertUInt16() + { + // + ushort[] numbers = { ushort.MinValue, 121, 12345, ushort.MaxValue }; + double result; + + foreach (ushort number in numbers) + { + result = Convert.ToDouble(number); + Console.WriteLine($"Converted the UInt16 value {number} to {result}."); + } + // The example displays the following output: + // Converted the UInt16 value 0 to 0. + // Converted the UInt16 value 121 to 121. + // Converted the UInt16 value 12345 to 12345. + // Converted the UInt16 value 65535 to 65535. + // + } + + private static void ConvertUInt32() + { + // + uint[] numbers = { uint.MinValue, 121, 12345, uint.MaxValue }; + double result; + + foreach (uint number in numbers) + { + result = Convert.ToDouble(number); + Console.WriteLine($"Converted the UInt32 value {number} to {result}."); + } + // The example displays the following output: + // Converted the UInt32 value 0 to 0. + // Converted the UInt32 value 121 to 121. + // Converted the UInt32 value 12345 to 12345. + // Converted the UInt32 value 4294967295 to 4294967295. + // + } + + private static void ConvertUInt64() + { + // + ulong[] numbers = { ulong.MinValue, 121, 12345, ulong.MaxValue }; + double result; + + foreach (ulong number in numbers) + { + result = Convert.ToDouble(number); + Console.WriteLine($"Converted the UInt64 value {number} to {result}."); + } + // The example displays the following output: + // Converted the UInt64 value 0 to 0. + // Converted the UInt64 value 121 to 121. + // Converted the UInt64 value 12345 to 12345. + // Converted the UInt64 value 18446744073709551615 to 1.84467440737096E+19. + // + } + + // unused + private static void ConvertString() + { + string[] values = { "-1,035.77219", "1AFF", "1e-35", "1,635,592,999,999,999,999,999,999", "-17.455", "190.34001", "1.29e325"}; - double result; - - foreach (string value in values) - { - try { - result = Convert.ToDouble(value); - Console.WriteLine("Converted '{0}' to {1}.", value, result); - } - catch (FormatException) { - Console.WriteLine("Unable to convert '{0}' to a Double.", value); - } - catch (OverflowException) { - Console.WriteLine("'{0}' is outside the range of a Double.", value); - } - } - // The example displays the following output: - // Converted '-1,035.77219' to -1035.77219. - // Unable to convert '1AFF' to a Double. - // Converted '1e-35' to 1E-35. - // Converted '1,635,592,999,999,999,999,999,999' to 1.635593E+24. - // Converted '-17.455' to -17.455. - // Converted '190.34001' to 190.34001. - // '1.29e325' is outside the range of a Double. - } + double result; + + foreach (string value in values) + { + try + { + result = Convert.ToDouble(value); + Console.WriteLine($"Converted '{value}' to {result}."); + } + catch (FormatException) + { + Console.WriteLine($"Unable to convert '{value}' to a Double."); + } + catch (OverflowException) + { + Console.WriteLine($"'{value}' is outside the range of a Double."); + } + } + // The example displays the following output: + // Converted '-1,035.77219' to -1035.77219. + // Unable to convert '1AFF' to a Double. + // Converted '1e-35' to 1E-35. + // Converted '1,635,592,999,999,999,999,999,999' to 1.635593E+24. + // Converted '-17.455' to -17.455. + // Converted '190.34001' to 190.34001. + // '1.29e325' is outside the range of a Double. + } } diff --git a/snippets/csharp/System/Convert/ToInt16/toint16.cs b/snippets/csharp/System/Convert/ToInt16/toint16.cs index 7c6a147850e..0c4fc116313 100644 --- a/snippets/csharp/System/Convert/ToInt16/toint16.cs +++ b/snippets/csharp/System/Convert/ToInt16/toint16.cs @@ -8,16 +8,16 @@ class ToInt16ProviderDemo { static string format = "{0,-20}{1,-20}{2}"; - // Get the exception type name; remove the namespace prefix. - static string GetExceptionType( Exception ex ) + // Get the exception type name; remove the namespace prefix. + static string GetExceptionType(Exception ex) { - string exceptionType = ex.GetType( ).ToString( ); + string exceptionType = ex.GetType().ToString(); return exceptionType.Substring( - exceptionType.LastIndexOf( '.' ) + 1 ); + exceptionType.LastIndexOf('.') + 1); } - static void ConvertToInt16( string numericStr, - IFormatProvider provider ) + static void ConvertToInt16(string numericStr, + IFormatProvider provider) { object defaultValue; object providerValue; @@ -25,32 +25,32 @@ static void ConvertToInt16( string numericStr, // Convert numericStr to Int16 without a format provider. try { - defaultValue = Convert.ToInt16( numericStr ); + defaultValue = Convert.ToInt16(numericStr); } - catch( Exception ex ) + catch (Exception ex) { - defaultValue = GetExceptionType( ex ); + defaultValue = GetExceptionType(ex); } // Convert numericStr to Int16 with a format provider. try { - providerValue = Convert.ToInt16( numericStr, provider ); + providerValue = Convert.ToInt16(numericStr, provider); } - catch( Exception ex ) + catch (Exception ex) { - providerValue = GetExceptionType( ex ); + providerValue = GetExceptionType(ex); } - Console.WriteLine( format, numericStr, - defaultValue, providerValue ); + Console.WriteLine(format, numericStr, + defaultValue, providerValue); } - public static void Main( ) + public static void Main() { // Create a NumberFormatInfo object and set several of its // properties that apply to numbers. - NumberFormatInfo provider = new NumberFormatInfo(); + NumberFormatInfo provider = new(); // These properties affect the conversion. provider.NegativeSign = "neg "; @@ -60,7 +60,7 @@ public static void Main( ) // The input string cannot have decimal and group separators. provider.NumberDecimalSeparator = "."; provider.NumberGroupSeparator = ","; - provider.NumberGroupSizes = new int[ ] { 3 }; + provider.NumberGroupSizes = new int[] { 3 }; provider.NumberNegativePattern = 0; Console.WriteLine("This example of\n" + @@ -68,23 +68,23 @@ public static void Main( ) " Convert.ToInt16( string, IFormatProvider ) " + "\ngenerates the following output. It converts " + "several strings to \nshort values, using " + - "default formatting or a NumberFormatInfo object.\n" ); - Console.WriteLine( format, "String to convert", - "Default/exception", "Provider/exception" ); - Console.WriteLine( format, "-----------------", - "-----------------", "------------------" ); + "default formatting or a NumberFormatInfo object.\n"); + Console.WriteLine(format, "String to convert", + "Default/exception", "Provider/exception"); + Console.WriteLine(format, "-----------------", + "-----------------", "------------------"); // Convert strings, with and without an IFormatProvider. - ConvertToInt16( "12345", provider ); - ConvertToInt16( "+12345", provider ); - ConvertToInt16( "pos 12345", provider ); - ConvertToInt16( "-12345", provider ); - ConvertToInt16( "neg 12345", provider ); - ConvertToInt16( "12345.", provider ); - ConvertToInt16( "12,345", provider ); - ConvertToInt16( "(12345)", provider ); - ConvertToInt16( "32768", provider ); - ConvertToInt16( "-32769", provider ); + ConvertToInt16("12345", provider); + ConvertToInt16("+12345", provider); + ConvertToInt16("pos 12345", provider); + ConvertToInt16("-12345", provider); + ConvertToInt16("neg 12345", provider); + ConvertToInt16("12345.", provider); + ConvertToInt16("12,345", provider); + ConvertToInt16("(12345)", provider); + ConvertToInt16("32768", provider); + ConvertToInt16("-32769", provider); } } diff --git a/snippets/csharp/System/Convert/ToInt16/toint16_1.cs b/snippets/csharp/System/Convert/ToInt16/toint16_1.cs index 0dfa629a77b..313eabd7f7b 100644 --- a/snippets/csharp/System/Convert/ToInt16/toint16_1.cs +++ b/snippets/csharp/System/Convert/ToInt16/toint16_1.cs @@ -2,391 +2,383 @@ public class Class1 { - public static void Main() - { - ConvertBoolean(); - Console.WriteLine("-----"); - ConvertByte(); - Console.WriteLine("-----"); - ConvertChar(); - Console.WriteLine("-----"); - ConvertDecimal(); - Console.WriteLine("-----"); - ConvertDouble(); - Console.WriteLine("-----"); - ConvertInt32(); - Console.WriteLine("-----"); - ConvertInt64(); - Console.WriteLine("-----"); - ConvertObject(); - Console.WriteLine("-----"); - ConvertSByte(); - Console.WriteLine("-----"); - ConvertSingle(); - Console.WriteLine("----"); - ConvertUInt16(); - Console.WriteLine("-----"); - ConvertUInt32(); - Console.WriteLine("-----"); - ConvertUInt64(); - } + public static void Main() + { + ConvertBoolean(); + Console.WriteLine("-----"); + ConvertByte(); + Console.WriteLine("-----"); + ConvertChar(); + Console.WriteLine("-----"); + ConvertDecimal(); + Console.WriteLine("-----"); + ConvertDouble(); + Console.WriteLine("-----"); + ConvertInt32(); + Console.WriteLine("-----"); + ConvertInt64(); + Console.WriteLine("-----"); + ConvertObject(); + Console.WriteLine("-----"); + ConvertSByte(); + Console.WriteLine("-----"); + ConvertSingle(); + Console.WriteLine("----"); + ConvertUInt16(); + Console.WriteLine("-----"); + ConvertUInt32(); + Console.WriteLine("-----"); + ConvertUInt64(); + } - private static void ConvertBoolean() - { - // - bool falseFlag = false; - bool trueFlag = true; + private static void ConvertBoolean() + { + // + bool falseFlag = false; + bool trueFlag = true; - Console.WriteLine("{0} converts to {1}.", falseFlag, - Convert.ToInt16(falseFlag)); - Console.WriteLine("{0} converts to {1}.", trueFlag, - Convert.ToInt16(trueFlag)); - // The example displays the following output: - // False converts to 0. - // True converts to 1. - // - } + Console.WriteLine($"{falseFlag} converts to {Convert.ToInt16(falseFlag)}."); + Console.WriteLine($"{trueFlag} converts to {Convert.ToInt16(trueFlag)}."); + // The example displays the following output: + // False converts to 0. + // True converts to 1. + // + } - private static void ConvertByte() - { - // - byte[] bytes = { Byte.MinValue, 14, 122, Byte.MaxValue}; - short result; + private static void ConvertByte() + { + // + byte[] bytes = { byte.MinValue, 14, 122, byte.MaxValue }; + short result; - foreach (byte byteValue in bytes) - { - result = Convert.ToInt16(byteValue); - Console.WriteLine("The Byte value {0} converts to {1}.", - byteValue, result); - } - // The example displays the following output: - // The Byte value 0 converts to 0. - // The Byte value 14 converts to 14. - // The Byte value 122 converts to 122. - // The Byte value 255 converts to 255. - // - } + foreach (byte byteValue in bytes) + { + result = Convert.ToInt16(byteValue); + Console.WriteLine($"The Byte value {byteValue} converts to {result}."); + } + // The example displays the following output: + // The Byte value 0 converts to 0. + // The Byte value 14 converts to 14. + // The Byte value 122 converts to 122. + // The Byte value 255 converts to 255. + // + } - private static void ConvertChar() - { - // - char[] chars = { 'a', 'z', '\x0007', '\x03FF', + private static void ConvertChar() + { + // + char[] chars = { 'a', 'z', '\x0007', '\x03FF', '\x7FFF', '\xFFFE' }; - short result; + short result; - foreach (char ch in chars) - { - try { - result = Convert.ToInt16(ch); - Console.WriteLine("'{0}' converts to {1}.", ch, result); - } - catch (OverflowException) { - Console.WriteLine("Unable to convert u+{0} to an Int16.", - ((int)ch).ToString("X4")); - } - } - // The example displays the following output: - // 'a' converts to 97. - // 'z' converts to 122. - // '' converts to 7. - // 'Ͽ' converts to 1023. - // '翿' converts to 32767. - // Unable to convert u+FFFE to an Int16. - // - } + foreach (char ch in chars) + { + try + { + result = Convert.ToInt16(ch); + Console.WriteLine($"'{ch}' converts to {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"Unable to convert u+{((int)ch).ToString("X4")} to an Int16."); + } + } + // The example displays the following output: + // 'a' converts to 97. + // 'z' converts to 122. + // '' converts to 7. + // 'Ͽ' converts to 1023. + // '翿' converts to 32767. + // Unable to convert u+FFFE to an Int16. + // + } - private static void ConvertDecimal() - { - // - decimal[] values = { Decimal.MinValue, -1034.23m, -12m, 0m, 147m, - 9214.16m, Decimal.MaxValue }; - short result; + private static void ConvertDecimal() + { + // + decimal[] values = { decimal.MinValue, -1034.23m, -12m, 0m, 147m, + 9214.16m, decimal.MaxValue }; + short result; - foreach (decimal value in values) - { - try { - result = Convert.ToInt16(value); - Console.WriteLine("Converted {0} to {1}.", value, result); - } - catch (OverflowException) - { - Console.WriteLine("{0} is outside the range of the Int16 type.", - value); - } - } - // The example displays the following output: - // -79228162514264337593543950335 is outside the range of the Int16 type. - // Converted -1034.23 to -1034. - // Converted -12 to -12. - // Converted 0 to 0. - // Converted 147 to 147. - // Converted 9214.16 to 9214. - // 79228162514264337593543950335 is outside the range of the Int16 type. - // - } + foreach (decimal value in values) + { + try + { + result = Convert.ToInt16(value); + Console.WriteLine($"Converted {value} to {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"{value} is outside the range of the Int16 type."); + } + } + // The example displays the following output: + // -79228162514264337593543950335 is outside the range of the Int16 type. + // Converted -1034.23 to -1034. + // Converted -12 to -12. + // Converted 0 to 0. + // Converted 147 to 147. + // Converted 9214.16 to 9214. + // 79228162514264337593543950335 is outside the range of the Int16 type. + // + } - private static void ConvertDouble() - { - // - double[] values = { Double.MinValue, -1.38e10, -1023.299, -12.98, - 0, 9.113e-16, 103.919, 17834.191, Double.MaxValue }; - short result; + private static void ConvertDouble() + { + // + double[] values = { double.MinValue, -1.38e10, -1023.299, -12.98, + 0, 9.113e-16, 103.919, 17834.191, double.MaxValue }; + short result; - foreach (double value in values) - { - try { - result = Convert.ToInt16(value); - Console.WriteLine("Converted {0} to {1}.", value, result); - } - catch (OverflowException) - { - Console.WriteLine("{0} is outside the range of the Int16 type.", value); - } - } - // -1.79769313486232E+308 is outside the range of the Int16 type. - // -13800000000 is outside the range of the Int16 type. - // Converted -1023.299 to -1023. - // Converted -12.98 to -13. - // Converted 0 to 0. - // Converted 9.113E-16 to 0. - // Converted 103.919 to 104. - // Converted 17834.191 to 17834. - // 1.79769313486232E+308 is outside the range of the Int16 type. - // - } + foreach (double value in values) + { + try + { + result = Convert.ToInt16(value); + Console.WriteLine($"Converted {value} to {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"{value} is outside the range of the Int16 type."); + } + } + // -1.79769313486232E+308 is outside the range of the Int16 type. + // -13800000000 is outside the range of the Int16 type. + // Converted -1023.299 to -1023. + // Converted -12.98 to -13. + // Converted 0 to 0. + // Converted 9.113E-16 to 0. + // Converted 103.919 to 104. + // Converted 17834.191 to 17834. + // 1.79769313486232E+308 is outside the range of the Int16 type. + // + } - private static void ConvertInt32() - { - // - int[] numbers = { Int32.MinValue, -1, 0, 121, 340, Int32.MaxValue }; - short result; + private static void ConvertInt32() + { + // + int[] numbers = { int.MinValue, -1, 0, 121, 340, int.MaxValue }; + short result; - foreach (int number in numbers) - { - try { - result = Convert.ToInt16(number); - Console.WriteLine("Converted the {0} value {1} to a {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Int16 type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // The Int32 value -2147483648 is outside the range of the Int16 type. - // Converted the Int32 value -1 to a Int16 value -1. - // Converted the Int32 value 0 to a Int16 value 0. - // Converted the Int32 value 121 to a Int16 value 121. - // Converted the Int32 value 340 to a Int16 value 340. - // The Int32 value 2147483647 is outside the range of the Int16 type. - // - } + foreach (int number in numbers) + { + try + { + result = Convert.ToInt16(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to a {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the Int16 type."); + } + } + // The example displays the following output: + // The Int32 value -2147483648 is outside the range of the Int16 type. + // Converted the Int32 value -1 to a Int16 value -1. + // Converted the Int32 value 0 to a Int16 value 0. + // Converted the Int32 value 121 to a Int16 value 121. + // Converted the Int32 value 340 to a Int16 value 340. + // The Int32 value 2147483647 is outside the range of the Int16 type. + // + } - private static void ConvertInt64() - { - // - long[] numbers = { Int64.MinValue, -1, 0, 121, 340, Int64.MaxValue }; - short result; + private static void ConvertInt64() + { + // + long[] numbers = { long.MinValue, -1, 0, 121, 340, long.MaxValue }; + short result; - foreach (long number in numbers) - { - try { - result = Convert.ToInt16(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Int16 type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // The Int64 value -9223372036854775808 is outside the range of the Int16 type. - // Converted the Int64 value -1 to the Int16 value -1. - // Converted the Int64 value 0 to the Int16 value 0. - // Converted the Int64 value 121 to the Int16 value 121. - // Converted the Int64 value 340 to the Int16 value 340. - // The Int64 value 9223372036854775807 is outside the range of the Int16 type. - // - } + foreach (long number in numbers) + { + try + { + result = Convert.ToInt16(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the Int16 type."); + } + } + // The example displays the following output: + // The Int64 value -9223372036854775808 is outside the range of the Int16 type. + // Converted the Int64 value -1 to the Int16 value -1. + // Converted the Int64 value 0 to the Int16 value 0. + // Converted the Int64 value 121 to the Int16 value 121. + // Converted the Int64 value 340 to the Int16 value 340. + // The Int64 value 9223372036854775807 is outside the range of the Int16 type. + // + } - private static void ConvertObject() - { - // - object[] values= { true, -12, 163, 935, 'x', new DateTime(2009, 5, 12), + private static void ConvertObject() + { + // + object[] values = { true, -12, 163, 935, 'x', new DateTime(2009, 5, 12), "104", "103.0", "-1", "1.00e2", "One", 1.00e2}; - short result; + short result; - foreach (object value in values) - { - try { - result = Convert.ToInt16(value); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - value.GetType().Name, value, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Int16 type.", - value.GetType().Name, value); - } - catch (FormatException) { - Console.WriteLine("The {0} value {1} is not in a recognizable format.", - value.GetType().Name, value); - } - catch (InvalidCastException) { - Console.WriteLine("No conversion to an Int16 exists for the {0} value {1}.", - value.GetType().Name, value); - } - } - // The example displays the following output: - // Converted the Boolean value True to the Int16 value 1. - // Converted the Int32 value -12 to the Int16 value -12. - // Converted the Int32 value 163 to the Int16 value 163. - // Converted the Int32 value 935 to the Int16 value 935. - // Converted the Char value x to the Int16 value 120. - // No conversion to an Int16 exists for the DateTime value 5/12/2009 12:00:00 AM. - // Converted the String value 104 to the Int16 value 104. - // The String value 103.0 is not in a recognizable format. - // Converted the String value -1 to the Int16 value -1. - // The String value 1.00e2 is not in a recognizable format. - // The String value One is not in a recognizable format. - // Converted the Double value 100 to the Int16 value 100. - // - } + foreach (object value in values) + { + try + { + result = Convert.ToInt16(value); + Console.WriteLine($"Converted the {value.GetType().Name} value {value} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {value.GetType().Name} value {value} is outside the range of the Int16 type."); + } + catch (FormatException) + { + Console.WriteLine($"The {value.GetType().Name} value {value} is not in a recognizable format."); + } + catch (InvalidCastException) + { + Console.WriteLine($"No conversion to an Int16 exists for the {value.GetType().Name} value {value}."); + } + } + // The example displays the following output: + // Converted the Boolean value True to the Int16 value 1. + // Converted the Int32 value -12 to the Int16 value -12. + // Converted the Int32 value 163 to the Int16 value 163. + // Converted the Int32 value 935 to the Int16 value 935. + // Converted the Char value x to the Int16 value 120. + // No conversion to an Int16 exists for the DateTime value 5/12/2009 12:00:00 AM. + // Converted the String value 104 to the Int16 value 104. + // The String value 103.0 is not in a recognizable format. + // Converted the String value -1 to the Int16 value -1. + // The String value 1.00e2 is not in a recognizable format. + // The String value One is not in a recognizable format. + // Converted the Double value 100 to the Int16 value 100. + // + } - private static void ConvertSByte() - { - // - sbyte[] numbers = { SByte.MinValue, -1, 0, 10, SByte.MaxValue }; - short result; + private static void ConvertSByte() + { + // + sbyte[] numbers = { sbyte.MinValue, -1, 0, 10, sbyte.MaxValue }; + short result; - foreach (sbyte number in numbers) - { - result = Convert.ToInt16(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the SByte value -128 to the Int16 value -128. - // Converted the SByte value -1 to the Int16 value -1. - // Converted the SByte value 0 to the Int16 value 0. - // Converted the SByte value 10 to the Int16 value 10. - // Converted the SByte value 127 to the Int16 value 127. - // - } + foreach (sbyte number in numbers) + { + result = Convert.ToInt16(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the SByte value -128 to the Int16 value -128. + // Converted the SByte value -1 to the Int16 value -1. + // Converted the SByte value 0 to the Int16 value 0. + // Converted the SByte value 10 to the Int16 value 10. + // Converted the SByte value 127 to the Int16 value 127. + // + } - private static void ConvertSingle() - { - // - float[] values = { Single.MinValue, -1.38e10f, -1023.299f, -12.98f, - 0f, 9.113e-16f, 103.919f, 17834.191f, Single.MaxValue }; - short result; + private static void ConvertSingle() + { + // + float[] values = { float.MinValue, -1.38e10f, -1023.299f, -12.98f, + 0f, 9.113e-16f, 103.919f, 17834.191f, float.MaxValue }; + short result; - foreach (float value in values) - { - try { - result = Convert.ToInt16(value); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - value.GetType().Name, value, result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the Int16 type.", value); - } - } - // The example displays the following output: - // -3.4028235E+38 is outside the range of the Int16 type. - // -1.38E+10 is outside the range of the Int16 type. - // Converted the Single value -1023.299 to the Int16 value -1023. - // Converted the Single value -12.98 to the Int16 value -13. - // Converted the Single value 0 to the Int16 value 0. - // Converted the Single value 9.113E-16 to the Int16 value 0. - // Converted the Single value 103.919 to the Int16 value 104. - // Converted the Single value 17834.191 to the Int16 value 17834. - // 3.4028235E+38 is outside the range of the Int16 type. - // - } + foreach (float value in values) + { + try + { + result = Convert.ToInt16(value); + Console.WriteLine($"Converted the {value.GetType().Name} value {value} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"{value} is outside the range of the Int16 type."); + } + } + // The example displays the following output: + // -3.4028235E+38 is outside the range of the Int16 type. + // -1.38E+10 is outside the range of the Int16 type. + // Converted the Single value -1023.299 to the Int16 value -1023. + // Converted the Single value -12.98 to the Int16 value -13. + // Converted the Single value 0 to the Int16 value 0. + // Converted the Single value 9.113E-16 to the Int16 value 0. + // Converted the Single value 103.919 to the Int16 value 104. + // Converted the Single value 17834.191 to the Int16 value 17834. + // 3.4028235E+38 is outside the range of the Int16 type. + // + } - private static void ConvertUInt16() - { - // - ushort[] numbers = { UInt16.MinValue, 121, 340, UInt16.MaxValue }; - short result; - foreach (ushort number in numbers) - { - try { - result = Convert.ToInt16(number); - Console.WriteLine("Converted the {0} value {1} to a {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Int16 type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // Converted the UInt16 value 0 to a Int16 value 0. - // Converted the UInt16 value 121 to a Int16 value 121. - // Converted the UInt16 value 340 to a Int16 value 340. - // The UInt16 value 65535 is outside the range of the Int16 type. - // - } + private static void ConvertUInt16() + { + // + ushort[] numbers = { ushort.MinValue, 121, 340, ushort.MaxValue }; + short result; + foreach (ushort number in numbers) + { + try + { + result = Convert.ToInt16(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to a {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the Int16 type."); + } + } + // The example displays the following output: + // Converted the UInt16 value 0 to a Int16 value 0. + // Converted the UInt16 value 121 to a Int16 value 121. + // Converted the UInt16 value 340 to a Int16 value 340. + // The UInt16 value 65535 is outside the range of the Int16 type. + // + } - private static void ConvertUInt32() - { - // - uint[] numbers = { UInt32.MinValue, 121, 340, UInt32.MaxValue }; - short result; + private static void ConvertUInt32() + { + // + uint[] numbers = { uint.MinValue, 121, 340, uint.MaxValue }; + short result; - foreach (uint number in numbers) - { - try { - result = Convert.ToInt16(number); - Console.WriteLine("Converted the {0} value {1} to a {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Int16 type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // Converted the UInt32 value 0 to a Int16 value 0. - // Converted the UInt32 value 121 to a Int16 value 121. - // Converted the UInt32 value 340 to a Int16 value 340. - // The UInt32 value 4294967295 is outside the range of the Int16 type. - // - } + foreach (uint number in numbers) + { + try + { + result = Convert.ToInt16(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to a {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the Int16 type."); + } + } + // The example displays the following output: + // Converted the UInt32 value 0 to a Int16 value 0. + // Converted the UInt32 value 121 to a Int16 value 121. + // Converted the UInt32 value 340 to a Int16 value 340. + // The UInt32 value 4294967295 is outside the range of the Int16 type. + // + } - private static void ConvertUInt64() - { - // - ulong[] numbers = { UInt64.MinValue, 121, 340, UInt64.MaxValue }; - short result; + private static void ConvertUInt64() + { + // + ulong[] numbers = { ulong.MinValue, 121, 340, ulong.MaxValue }; + short result; - foreach (ulong number in numbers) - { - try { - result = Convert.ToInt16(number); - Console.WriteLine("Converted the {0} value {1} to a {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Int16 type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // Converted the UInt64 value 0 to a Int16 value 0. - // Converted the UInt64 value 121 to a Int16 value 121. - // Converted the UInt64 value 340 to a Int16 value 340. - // The UInt64 value 18446744073709551615 is outside the range of the Int16 type. - // - } + foreach (ulong number in numbers) + { + try + { + result = Convert.ToInt16(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to a {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the Int16 type."); + } + } + // The example displays the following output: + // Converted the UInt64 value 0 to a Int16 value 0. + // Converted the UInt64 value 121 to a Int16 value 121. + // Converted the UInt64 value 340 to a Int16 value 340. + // The UInt64 value 18446744073709551615 is outside the range of the Int16 type. + // + } } diff --git a/snippets/csharp/System/Convert/ToInt16/toint16_2.cs b/snippets/csharp/System/Convert/ToInt16/toint16_2.cs index 99a36e911a2..a4e057808a3 100644 --- a/snippets/csharp/System/Convert/ToInt16/toint16_2.cs +++ b/snippets/csharp/System/Convert/ToInt16/toint16_2.cs @@ -3,28 +3,31 @@ public class Example { - public static void Main() - { - string[] hexStrings = { "8000", "0FFF", "f000", "00A30", "D", "-13", + public static void Main() + { + string[] hexStrings = { "8000", "0FFF", "f000", "00A30", "D", "-13", "9AC61", "GAD" }; - foreach (string hexString in hexStrings) - { - try { - short number = Convert.ToInt16(hexString, 16); - Console.WriteLine("Converted '{0}' to {1:N0}.", hexString, number); - } - catch (FormatException) { - Console.WriteLine("'{0}' is not in the correct format for a hexadecimal number.", - hexString); - } - catch (OverflowException) { - Console.WriteLine("'{0}' is outside the range of an Int16.", hexString); - } - catch (ArgumentException) { - Console.WriteLine("'{0}' is invalid in base 16.", hexString); - } - } - } + foreach (string hexString in hexStrings) + { + try + { + short number = Convert.ToInt16(hexString, 16); + Console.WriteLine($"Converted '{hexString}' to {number:N0}."); + } + catch (FormatException) + { + Console.WriteLine($"'{hexString}' is not in the correct format for a hexadecimal number."); + } + catch (OverflowException) + { + Console.WriteLine($"'{hexString}' is outside the range of an Int16."); + } + catch (ArgumentException) + { + Console.WriteLine($"'{hexString}' is invalid in base 16."); + } + } + } } // The example displays the following output: // Converted '8000' to -32,768. diff --git a/snippets/csharp/System/Convert/ToInt16/toint32.cs b/snippets/csharp/System/Convert/ToInt16/toint32.cs index 12c17d3a630..fed42ed046c 100644 --- a/snippets/csharp/System/Convert/ToInt16/toint32.cs +++ b/snippets/csharp/System/Convert/ToInt16/toint32.cs @@ -8,16 +8,16 @@ class ToInt32ProviderDemo { static string format = "{0,-20}{1,-20}{2}"; - // Get the exception type name; remove the namespace prefix. - static string GetExceptionType( Exception ex ) + // Get the exception type name; remove the namespace prefix. + static string GetExceptionType(Exception ex) { - string exceptionType = ex.GetType( ).ToString( ); + string exceptionType = ex.GetType().ToString(); return exceptionType.Substring( - exceptionType.LastIndexOf( '.' ) + 1 ); + exceptionType.LastIndexOf('.') + 1); } - static void ConvertToInt32( string numericStr, - IFormatProvider provider ) + static void ConvertToInt32(string numericStr, + IFormatProvider provider) { object defaultValue; object providerValue; @@ -25,32 +25,32 @@ static void ConvertToInt32( string numericStr, // Convert numericStr to Int32 without a format provider. try { - defaultValue = Convert.ToInt32( numericStr ); + defaultValue = Convert.ToInt32(numericStr); } - catch( Exception ex ) + catch (Exception ex) { - defaultValue = GetExceptionType( ex ); + defaultValue = GetExceptionType(ex); } // Convert numericStr to Int32 with a format provider. try { - providerValue = Convert.ToInt32( numericStr, provider ); + providerValue = Convert.ToInt32(numericStr, provider); } - catch( Exception ex ) + catch (Exception ex) { - providerValue = GetExceptionType( ex ); + providerValue = GetExceptionType(ex); } - Console.WriteLine( format, numericStr, - defaultValue, providerValue ); + Console.WriteLine(format, numericStr, + defaultValue, providerValue); } - public static void Main( ) + public static void Main() { // Create a NumberFormatInfo object and set several of its // properties that apply to numbers. - NumberFormatInfo provider = new NumberFormatInfo(); + NumberFormatInfo provider = new(); // These properties affect the conversion. provider.NegativeSign = "neg "; @@ -60,7 +60,7 @@ public static void Main( ) // The input string cannot have decimal and group separators. provider.NumberDecimalSeparator = "."; provider.NumberGroupSeparator = ","; - provider.NumberGroupSizes = new int[ ] { 3 }; + provider.NumberGroupSizes = new int[] { 3 }; provider.NumberNegativePattern = 0; Console.WriteLine("This example of\n" + @@ -68,23 +68,23 @@ public static void Main( ) " Convert.ToInt32( string, IFormatProvider ) " + "\ngenerates the following output. It converts " + "several strings to \nint values, using " + - "default formatting or a NumberFormatInfo object.\n" ); - Console.WriteLine( format, "String to convert", - "Default/exception", "Provider/exception" ); - Console.WriteLine( format, "-----------------", - "-----------------", "------------------" ); + "default formatting or a NumberFormatInfo object.\n"); + Console.WriteLine(format, "String to convert", + "Default/exception", "Provider/exception"); + Console.WriteLine(format, "-----------------", + "-----------------", "------------------"); // Convert strings, with and without an IFormatProvider. - ConvertToInt32( "123456789", provider ); - ConvertToInt32( "+123456789", provider ); - ConvertToInt32( "pos 123456789", provider ); - ConvertToInt32( "-123456789", provider ); - ConvertToInt32( "neg 123456789", provider ); - ConvertToInt32( "123456789.", provider ); - ConvertToInt32( "123,456,789", provider ); - ConvertToInt32( "(123456789)", provider ); - ConvertToInt32( "2147483648", provider ); - ConvertToInt32( "-2147483649", provider ); + ConvertToInt32("123456789", provider); + ConvertToInt32("+123456789", provider); + ConvertToInt32("pos 123456789", provider); + ConvertToInt32("-123456789", provider); + ConvertToInt32("neg 123456789", provider); + ConvertToInt32("123456789.", provider); + ConvertToInt32("123,456,789", provider); + ConvertToInt32("(123456789)", provider); + ConvertToInt32("2147483648", provider); + ConvertToInt32("-2147483649", provider); } } diff --git a/snippets/csharp/System/Convert/ToInt16/toint64.cs b/snippets/csharp/System/Convert/ToInt16/toint64.cs index 4b7098939fd..13a280f653d 100644 --- a/snippets/csharp/System/Convert/ToInt16/toint64.cs +++ b/snippets/csharp/System/Convert/ToInt16/toint64.cs @@ -8,16 +8,16 @@ class ToInt64ProviderDemo { static string format = "{0,-22}{1,-20}{2}"; - // Get the exception type name; remove the namespace prefix. - static string GetExceptionType( Exception ex ) + // Get the exception type name; remove the namespace prefix. + static string GetExceptionType(Exception ex) { - string exceptionType = ex.GetType( ).ToString( ); + string exceptionType = ex.GetType().ToString(); return exceptionType.Substring( - exceptionType.LastIndexOf( '.' ) + 1 ); + exceptionType.LastIndexOf('.') + 1); } - static void ConvertToInt64( string numericStr, - IFormatProvider provider ) + static void ConvertToInt64(string numericStr, + IFormatProvider provider) { object defaultValue; object providerValue; @@ -25,32 +25,32 @@ static void ConvertToInt64( string numericStr, // Convert numericStr to Int64 without a format provider. try { - defaultValue = Convert.ToInt64( numericStr ); + defaultValue = Convert.ToInt64(numericStr); } - catch( Exception ex ) + catch (Exception ex) { - defaultValue = GetExceptionType( ex ); + defaultValue = GetExceptionType(ex); } // Convert numericStr to Int64 with a format provider. try { - providerValue = Convert.ToInt64( numericStr, provider ); + providerValue = Convert.ToInt64(numericStr, provider); } - catch( Exception ex ) + catch (Exception ex) { - providerValue = GetExceptionType( ex ); + providerValue = GetExceptionType(ex); } - Console.WriteLine( format, numericStr, - defaultValue, providerValue ); + Console.WriteLine(format, numericStr, + defaultValue, providerValue); } - public static void Main( ) + public static void Main() { // Create a NumberFormatInfo object and set several of its // properties that apply to numbers. - NumberFormatInfo provider = new NumberFormatInfo(); + NumberFormatInfo provider = new(); // These properties affect the conversion. provider.NegativeSign = "neg "; @@ -60,7 +60,7 @@ public static void Main( ) // The input string cannot have decimal and group separators. provider.NumberDecimalSeparator = "."; provider.NumberGroupSeparator = ","; - provider.NumberGroupSizes = new int[ ] { 3 }; + provider.NumberGroupSizes = new int[] { 3 }; provider.NumberNegativePattern = 0; Console.WriteLine("This example of\n" + @@ -68,23 +68,23 @@ public static void Main( ) " Convert.ToInt64( string, IFormatProvider ) " + "\ngenerates the following output. It converts " + "several strings to \nlong values, using " + - "default formatting or a NumberFormatInfo object.\n" ); - Console.WriteLine( format, "String to convert", - "Default/exception", "Provider/exception" ); - Console.WriteLine( format, "-----------------", - "-----------------", "------------------" ); + "default formatting or a NumberFormatInfo object.\n"); + Console.WriteLine(format, "String to convert", + "Default/exception", "Provider/exception"); + Console.WriteLine(format, "-----------------", + "-----------------", "------------------"); // Convert strings, with and without an IFormatProvider. - ConvertToInt64( "123456789", provider ); - ConvertToInt64( "+123456789", provider ); - ConvertToInt64( "pos 123456789", provider ); - ConvertToInt64( "-123456789", provider ); - ConvertToInt64( "neg 123456789", provider ); - ConvertToInt64( "123456789.", provider ); - ConvertToInt64( "123,456,789", provider ); - ConvertToInt64( "(123456789)", provider ); - ConvertToInt64( "9223372036854775808", provider ); - ConvertToInt64( "-9223372036854775809", provider ); + ConvertToInt64("123456789", provider); + ConvertToInt64("+123456789", provider); + ConvertToInt64("pos 123456789", provider); + ConvertToInt64("-123456789", provider); + ConvertToInt64("neg 123456789", provider); + ConvertToInt64("123456789.", provider); + ConvertToInt64("123,456,789", provider); + ConvertToInt64("(123456789)", provider); + ConvertToInt64("9223372036854775808", provider); + ConvertToInt64("-9223372036854775809", provider); } } diff --git a/snippets/csharp/System/Convert/ToInt16/tosbyte.cs b/snippets/csharp/System/Convert/ToInt16/tosbyte.cs index 47cf25cf45f..6287dc77e11 100644 --- a/snippets/csharp/System/Convert/ToInt16/tosbyte.cs +++ b/snippets/csharp/System/Convert/ToInt16/tosbyte.cs @@ -8,16 +8,16 @@ class ToSByteProviderDemo { static string format = "{0,-20}{1,-20}{2}"; - // Get the exception type name; remove the namespace prefix. - static string GetExceptionType( Exception ex ) + // Get the exception type name; remove the namespace prefix. + static string GetExceptionType(Exception ex) { - string exceptionType = ex.GetType( ).ToString( ); + string exceptionType = ex.GetType().ToString(); return exceptionType.Substring( - exceptionType.LastIndexOf( '.' ) + 1 ); + exceptionType.LastIndexOf('.') + 1); } - static void ConvertToSByte( string numericStr, - IFormatProvider provider ) + static void ConvertToSByte(string numericStr, + IFormatProvider provider) { object defaultValue; object providerValue; @@ -25,32 +25,32 @@ static void ConvertToSByte( string numericStr, // Convert numericStr to SByte without a format provider. try { - defaultValue = Convert.ToSByte( numericStr ); + defaultValue = Convert.ToSByte(numericStr); } - catch( Exception ex ) + catch (Exception ex) { - defaultValue = GetExceptionType( ex ); + defaultValue = GetExceptionType(ex); } // Convert numericStr to SByte with a format provider. try { - providerValue = Convert.ToSByte( numericStr, provider ); + providerValue = Convert.ToSByte(numericStr, provider); } - catch( Exception ex ) + catch (Exception ex) { - providerValue = GetExceptionType( ex ); + providerValue = GetExceptionType(ex); } - Console.WriteLine( format, numericStr, - defaultValue, providerValue ); + Console.WriteLine(format, numericStr, + defaultValue, providerValue); } - public static void Main( ) + public static void Main() { // Create a NumberFormatInfo object and set several of its // properties that apply to numbers. - NumberFormatInfo provider = new NumberFormatInfo(); + NumberFormatInfo provider = new(); // These properties affect the conversion. provider.NegativeSign = "neg "; @@ -66,22 +66,22 @@ public static void Main( ) " Convert.ToSByte( string, IFormatProvider ) " + "\ngenerates the following output. It converts " + "several strings to \nSByte values, using " + - "default formatting or a NumberFormatInfo object.\n" ); - Console.WriteLine( format, "String to convert", - "Default/exception", "Provider/exception" ); - Console.WriteLine( format, "-----------------", - "-----------------", "------------------" ); + "default formatting or a NumberFormatInfo object.\n"); + Console.WriteLine(format, "String to convert", + "Default/exception", "Provider/exception"); + Console.WriteLine(format, "-----------------", + "-----------------", "------------------"); // Convert strings, with and without an IFormatProvider. - ConvertToSByte( "123", provider ); - ConvertToSByte( "+123", provider ); - ConvertToSByte( "pos 123", provider ); - ConvertToSByte( "-123", provider ); - ConvertToSByte( "neg 123", provider ); - ConvertToSByte( "123.", provider ); - ConvertToSByte( "(123)", provider ); - ConvertToSByte( "128", provider ); - ConvertToSByte( "-129", provider ); + ConvertToSByte("123", provider); + ConvertToSByte("+123", provider); + ConvertToSByte("pos 123", provider); + ConvertToSByte("-123", provider); + ConvertToSByte("neg 123", provider); + ConvertToSByte("123.", provider); + ConvertToSByte("(123)", provider); + ConvertToSByte("128", provider); + ConvertToSByte("-129", provider); } } diff --git a/snippets/csharp/System/Convert/ToInt32/toint32_1.cs b/snippets/csharp/System/Convert/ToInt32/toint32_1.cs index 1f54fdec6e4..c6f653aa05e 100644 --- a/snippets/csharp/System/Convert/ToInt32/toint32_1.cs +++ b/snippets/csharp/System/Convert/ToInt32/toint32_1.cs @@ -2,427 +2,414 @@ public class Example { - public static void Main() - { - ConvertBoolean(); - Console.WriteLine("-----"); - ConvertByte(); - Console.WriteLine("-----"); - ConvertChar(); - Console.WriteLine("-----"); - ConvertDecimal(); - Console.WriteLine("-----"); - ConvertDouble(); - Console.WriteLine("-----"); - ConvertInt16(); - Console.WriteLine("-----"); - ConvertInt64(); - Console.WriteLine("-----"); - ConvertObject(); - Console.WriteLine("-----"); - ConvertSByte(); - Console.WriteLine("-----"); - ConvertSingle(); - Console.WriteLine("----"); - ConvertString(); - Console.WriteLine("-----"); - ConvertUInt16(); - Console.WriteLine("-----"); - ConvertUInt32(); - Console.WriteLine("-----"); - ConvertUInt64(); + public static void Main() + { + ConvertBoolean(); + Console.WriteLine("-----"); + ConvertByte(); + Console.WriteLine("-----"); + ConvertChar(); + Console.WriteLine("-----"); + ConvertDecimal(); + Console.WriteLine("-----"); + ConvertDouble(); + Console.WriteLine("-----"); + ConvertInt16(); + Console.WriteLine("-----"); + ConvertInt64(); + Console.WriteLine("-----"); + ConvertObject(); + Console.WriteLine("-----"); + ConvertSByte(); + Console.WriteLine("-----"); + ConvertSingle(); + Console.WriteLine("----"); + ConvertString(); + Console.WriteLine("-----"); + ConvertUInt16(); + Console.WriteLine("-----"); + ConvertUInt32(); + Console.WriteLine("-----"); + ConvertUInt64(); } - private static void ConvertBoolean() - { - // - bool falseFlag = false; - bool trueFlag = true; + private static void ConvertBoolean() + { + // + bool falseFlag = false; + bool trueFlag = true; - Console.WriteLine("{0} converts to {1}.", falseFlag, - Convert.ToInt32(falseFlag)); - Console.WriteLine("{0} converts to {1}.", trueFlag, - Convert.ToInt32(trueFlag)); - // The example displays the following output: - // False converts to 0. - // True converts to 1. - // - } + Console.WriteLine($"{falseFlag} converts to {Convert.ToInt32(falseFlag)}."); + Console.WriteLine($"{trueFlag} converts to {Convert.ToInt32(trueFlag)}."); + // The example displays the following output: + // False converts to 0. + // True converts to 1. + // + } - private static void ConvertByte() - { - // - byte[] bytes = { Byte.MinValue, 14, 122, Byte.MaxValue}; - int result; + private static void ConvertByte() + { + // + byte[] bytes = { byte.MinValue, 14, 122, byte.MaxValue }; + int result; - foreach (byte byteValue in bytes) - { - result = Convert.ToInt32(byteValue); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - byteValue.GetType().Name, byteValue, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the Byte value 0 to the Int32 value 0. - // Converted the Byte value 14 to the Int32 value 14. - // Converted the Byte value 122 to the Int32 value 122. - // Converted the Byte value 255 to the Int32 value 255. - // - } + foreach (byte byteValue in bytes) + { + result = Convert.ToInt32(byteValue); + Console.WriteLine($"Converted the {byteValue.GetType().Name} value {byteValue} to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the Byte value 0 to the Int32 value 0. + // Converted the Byte value 14 to the Int32 value 14. + // Converted the Byte value 122 to the Int32 value 122. + // Converted the Byte value 255 to the Int32 value 255. + // + } - private static void ConvertChar() - { - // - char[] chars = { 'a', 'z', '\u0007', '\u03FF', + private static void ConvertChar() + { + // + char[] chars = { 'a', 'z', '\u0007', '\u03FF', '\u7FFF', '\uFFFE' }; - int result; - - foreach (char ch in chars) - { - try { - result = Convert.ToInt32(ch); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - ch.GetType().Name, ch, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("Unable to convert u+{0} to an Int32.", - ((int)ch).ToString("X4")); - } - } - // The example displays the following output: - // Converted the Char value 'a' to the Int32 value 97. - // Converted the Char value 'z' to the Int32 value 122. - // Converted the Char value '' to the Int32 value 7. - // Converted the Char value 'Ͽ' to the Int32 value 1023. - // Converted the Char value '翿' to the Int32 value 32767. - // Converted the Char value '￾' to the Int32 value 65534. - // - } + int result; - private static void ConvertDecimal() - { - // - decimal[] values= { Decimal.MinValue, -1034.23m, -12m, 0m, 147m, - 199.55m, 9214.16m, Decimal.MaxValue }; - int result; + foreach (char ch in chars) + { + try + { + result = Convert.ToInt32(ch); + Console.WriteLine($"Converted the {ch.GetType().Name} value '{ch}' to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"Unable to convert u+{((int)ch).ToString("X4")} to an Int32."); + } + } + // The example displays the following output: + // Converted the Char value 'a' to the Int32 value 97. + // Converted the Char value 'z' to the Int32 value 122. + // Converted the Char value '' to the Int32 value 7. + // Converted the Char value 'Ͽ' to the Int32 value 1023. + // Converted the Char value '翿' to the Int32 value 32767. + // Converted the Char value '￾' to the Int32 value 65534. + // + } - foreach (decimal value in values) - { - try { - result = Convert.ToInt32(value); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - value.GetType().Name, value, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the Int32 type.", - value); - } - } - // The example displays the following output: - // -79228162514264337593543950335 is outside the range of the Int32 type. - // Converted the Decimal value '-1034.23' to the Int32 value -1034. - // Converted the Decimal value '-12' to the Int32 value -12. - // Converted the Decimal value '0' to the Int32 value 0. - // Converted the Decimal value '147' to the Int32 value 147. - // Converted the Decimal value '199.55' to the Int32 value 200. - // Converted the Decimal value '9214.16' to the Int32 value 9214. - // 79228162514264337593543950335 is outside the range of the Int32 type. - // - } + private static void ConvertDecimal() + { + // + decimal[] values = { decimal.MinValue, -1034.23m, -12m, 0m, 147m, + 199.55m, 9214.16m, decimal.MaxValue }; + int result; - private static void ConvertDouble() - { - // - double[] values= { Double.MinValue, -1.38e10, -1023.299, -12.98, - 0, 9.113e-16, 103.919, 17834.191, Double.MaxValue }; - int result; + foreach (decimal value in values) + { + try + { + result = Convert.ToInt32(value); + Console.WriteLine($"Converted the {value.GetType().Name} value '{value}' to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"{value} is outside the range of the Int32 type."); + } + } + // The example displays the following output: + // -79228162514264337593543950335 is outside the range of the Int32 type. + // Converted the Decimal value '-1034.23' to the Int32 value -1034. + // Converted the Decimal value '-12' to the Int32 value -12. + // Converted the Decimal value '0' to the Int32 value 0. + // Converted the Decimal value '147' to the Int32 value 147. + // Converted the Decimal value '199.55' to the Int32 value 200. + // Converted the Decimal value '9214.16' to the Int32 value 9214. + // 79228162514264337593543950335 is outside the range of the Int32 type. + // + } - foreach (double value in values) - { - try { - result = Convert.ToInt32(value); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - value.GetType().Name, value, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the Int32 type.", value); - } - } - // -1.79769313486232E+308 is outside the range of the Int32 type. - // -13800000000 is outside the range of the Int32 type. - // Converted the Double value '-1023.299' to the Int32 value -1023. - // Converted the Double value '-12.98' to the Int32 value -13. - // Converted the Double value '0' to the Int32 value 0. - // Converted the Double value '9.113E-16' to the Int32 value 0. - // Converted the Double value '103.919' to the Int32 value 104. - // Converted the Double value '17834.191' to the Int32 value 17834. - // 1.79769313486232E+308 is outside the range of the Int32 type. - // - } + private static void ConvertDouble() + { + // + double[] values = { double.MinValue, -1.38e10, -1023.299, -12.98, + 0, 9.113e-16, 103.919, 17834.191, double.MaxValue }; + int result; - private static void ConvertInt16() - { - // - short[] numbers= { Int16.MinValue, -1, 0, 121, 340, Int16.MaxValue }; - int result; + foreach (double value in values) + { + try + { + result = Convert.ToInt32(value); + Console.WriteLine($"Converted the {value.GetType().Name} value '{value}' to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"{value} is outside the range of the Int32 type."); + } + } + // -1.79769313486232E+308 is outside the range of the Int32 type. + // -13800000000 is outside the range of the Int32 type. + // Converted the Double value '-1023.299' to the Int32 value -1023. + // Converted the Double value '-12.98' to the Int32 value -13. + // Converted the Double value '0' to the Int32 value 0. + // Converted the Double value '9.113E-16' to the Int32 value 0. + // Converted the Double value '103.919' to the Int32 value 104. + // Converted the Double value '17834.191' to the Int32 value 17834. + // 1.79769313486232E+308 is outside the range of the Int32 type. + // + } - foreach (short number in numbers) - { - result = Convert.ToInt32(number); - Console.WriteLine("Converted the {0} value {1} to a {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the Int16 value -32768 to a Int32 value -32768. - // Converted the Int16 value -1 to a Int32 value -1. - // Converted the Int16 value 0 to a Int32 value 0. - // Converted the Int16 value 121 to a Int32 value 121. - // Converted the Int16 value 340 to a Int32 value 340. - // Converted the Int16 value 32767 to a Int32 value 32767. - // - } + private static void ConvertInt16() + { + // + short[] numbers = { short.MinValue, -1, 0, 121, 340, short.MaxValue }; + int result; - private static void ConvertInt64() - { - // - long[] numbers = { Int64.MinValue, -1, 0, 121, 340, Int64.MaxValue }; - int result; - foreach (long number in numbers) - { - try { + foreach (short number in numbers) + { result = Convert.ToInt32(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Int32 type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // The Int64 value -9223372036854775808 is outside the range of the Int32 type. - // Converted the Int64 value -1 to the Int32 value -1. - // Converted the Int64 value 0 to the Int32 value 0. - // Converted the Int64 value 121 to the Int32 value 121. - // Converted the Int64 value 340 to the Int32 value 340. - // The Int64 value 9223372036854775807 is outside the range of the Int32 type. - // - } + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to a {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the Int16 value -32768 to a Int32 value -32768. + // Converted the Int16 value -1 to a Int32 value -1. + // Converted the Int16 value 0 to a Int32 value 0. + // Converted the Int16 value 121 to a Int32 value 121. + // Converted the Int16 value 340 to a Int32 value 340. + // Converted the Int16 value 32767 to a Int32 value 32767. + // + } - private static void ConvertObject() - { - // - object[] values = { true, -12, 163, 935, 'x', new DateTime(2009, 5, 12), + private static void ConvertInt64() + { + // + long[] numbers = { long.MinValue, -1, 0, 121, 340, long.MaxValue }; + int result; + foreach (long number in numbers) + { + try + { + result = Convert.ToInt32(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the Int32 type."); + } + } + // The example displays the following output: + // The Int64 value -9223372036854775808 is outside the range of the Int32 type. + // Converted the Int64 value -1 to the Int32 value -1. + // Converted the Int64 value 0 to the Int32 value 0. + // Converted the Int64 value 121 to the Int32 value 121. + // Converted the Int64 value 340 to the Int32 value 340. + // The Int64 value 9223372036854775807 is outside the range of the Int32 type. + // + } + + private static void ConvertObject() + { + // + object[] values = { true, -12, 163, 935, 'x', new DateTime(2009, 5, 12), "104", "103.0", "-1", "1.00e2", "One", 1.00e2, 16.3e42}; - int result; + int result; - foreach (object value in values) - { - try { - result = Convert.ToInt32(value); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - value.GetType().Name, value, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Int32 type.", - value.GetType().Name, value); - } - catch (FormatException) { - Console.WriteLine("The {0} value {1} is not in a recognizable format.", - value.GetType().Name, value); - } - catch (InvalidCastException) { - Console.WriteLine("No conversion to an Int32 exists for the {0} value {1}.", - value.GetType().Name, value); - } - } - // The example displays the following output: - // Converted the Boolean value True to the Int32 value 1. - // Converted the Int32 value -12 to the Int32 value -12. - // Converted the Int32 value 163 to the Int32 value 163. - // Converted the Int32 value 935 to the Int32 value 935. - // Converted the Char value x to the Int32 value 120. - // No conversion to an Int32 exists for the DateTime value 5/12/2009 12:00:00 AM. - // Converted the String value 104 to the Int32 value 104. - // The String value 103.0 is not in a recognizable format. - // Converted the String value -1 to the Int32 value -1. - // The String value 1.00e2 is not in a recognizable format. - // The String value One is not in a recognizable format. - // Converted the Double value 100 to the Int32 value 100. - // The Double value 1.63E+43 is outside the range of the Int32 type. - // - } + foreach (object value in values) + { + try + { + result = Convert.ToInt32(value); + Console.WriteLine($"Converted the {value.GetType().Name} value {value} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {value.GetType().Name} value {value} is outside the range of the Int32 type."); + } + catch (FormatException) + { + Console.WriteLine($"The {value.GetType().Name} value {value} is not in a recognizable format."); + } + catch (InvalidCastException) + { + Console.WriteLine($"No conversion to an Int32 exists for the {value.GetType().Name} value {value}."); + } + } + // The example displays the following output: + // Converted the Boolean value True to the Int32 value 1. + // Converted the Int32 value -12 to the Int32 value -12. + // Converted the Int32 value 163 to the Int32 value 163. + // Converted the Int32 value 935 to the Int32 value 935. + // Converted the Char value x to the Int32 value 120. + // No conversion to an Int32 exists for the DateTime value 5/12/2009 12:00:00 AM. + // Converted the String value 104 to the Int32 value 104. + // The String value 103.0 is not in a recognizable format. + // Converted the String value -1 to the Int32 value -1. + // The String value 1.00e2 is not in a recognizable format. + // The String value One is not in a recognizable format. + // Converted the Double value 100 to the Int32 value 100. + // The Double value 1.63E+43 is outside the range of the Int32 type. + // + } - private static void ConvertSByte() - { - // - sbyte[] numbers = { SByte.MinValue, -1, 0, 10, SByte.MaxValue }; - int result; + private static void ConvertSByte() + { + // + sbyte[] numbers = { sbyte.MinValue, -1, 0, 10, sbyte.MaxValue }; + int result; - foreach (sbyte number in numbers) - { - result = Convert.ToInt32(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the SByte value -128 to the Int32 value -128. - // Converted the SByte value -1 to the Int32 value -1. - // Converted the SByte value 0 to the Int32 value 0. - // Converted the SByte value 10 to the Int32 value 10. - // Converted the SByte value 127 to the Int32 value 127. - // - } + foreach (sbyte number in numbers) + { + result = Convert.ToInt32(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the SByte value -128 to the Int32 value -128. + // Converted the SByte value -1 to the Int32 value -1. + // Converted the SByte value 0 to the Int32 value 0. + // Converted the SByte value 10 to the Int32 value 10. + // Converted the SByte value 127 to the Int32 value 127. + // + } - private static void ConvertSingle() - { - // - float[] values= { Single.MinValue, -1.38e10f, -1023.299f, -12.98f, - 0f, 9.113e-16f, 103.919f, 17834.191f, Single.MaxValue }; - int result; + private static void ConvertSingle() + { + // + float[] values = { float.MinValue, -1.38e10f, -1023.299f, -12.98f, + 0f, 9.113e-16f, 103.919f, 17834.191f, float.MaxValue }; + int result; - foreach (float value in values) - { - try { - result = Convert.ToInt32(value); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - value.GetType().Name, value, result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the Int32 type.", value); - } - } - // The example displays the following output: - // -3.40282346638529E+38 is outside the range of the Int32 type. - // -13799999488 is outside the range of the Int32 type. - // Converted the Double value -1023.29901123047 to the Int32 value -1023. - // Converted the Double value -12.9799995422363 to the Int32 value -13. - // Converted the Double value 0 to the Int32 value 0. - // Converted the Double value 9.11299983940444E-16 to the Int32 value 0. - // Converted the Double value 103.918998718262 to the Int32 value 104. - // Converted the Double value 17834.19140625 to the Int32 value 17834. - // 3.40282346638529E+38 is outside the range of the Int32 type. - // - } + foreach (float value in values) + { + try + { + result = Convert.ToInt32(value); + Console.WriteLine($"Converted the {value.GetType().Name} value {value} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"{value} is outside the range of the Int32 type."); + } + } + // The example displays the following output: + // -3.40282346638529E+38 is outside the range of the Int32 type. + // -13799999488 is outside the range of the Int32 type. + // Converted the Double value -1023.29901123047 to the Int32 value -1023. + // Converted the Double value -12.9799995422363 to the Int32 value -13. + // Converted the Double value 0 to the Int32 value 0. + // Converted the Double value 9.11299983940444E-16 to the Int32 value 0. + // Converted the Double value 103.918998718262 to the Int32 value 104. + // Converted the Double value 17834.19140625 to the Int32 value 17834. + // 3.40282346638529E+38 is outside the range of the Int32 type. + // + } - private static void ConvertString() - { - // - string[] values = { "One", "1.34e28", "-26.87", "-18", "-6.00", - " 0", "137", "1601.9", Int32.MaxValue.ToString() }; - int result; + private static void ConvertString() + { + // + string[] values = { "One", "1.34e28", "-26.87", "-18", "-6.00", + " 0", "137", "1601.9", int.MaxValue.ToString() }; + int result; - foreach (string value in values) - { - try { - result = Convert.ToInt32(value); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - value.GetType().Name, value, result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the Int32 type.", value); - } - catch (FormatException) { - Console.WriteLine("The {0} value '{1}' is not in a recognizable format.", - value.GetType().Name, value); - } - } - // The example displays the following output: - // The String value 'One' is not in a recognizable format. - // The String value '1.34e28' is not in a recognizable format. - // The String value '-26.87' is not in a recognizable format. - // Converted the String value '-18' to the Int32 value -18. - // The String value '-6.00' is not in a recognizable format. - // Converted the String value ' 0' to the Int32 value 0. - // Converted the String value '137' to the Int32 value 137. - // The String value '1601.9' is not in a recognizable format. - // Converted the String value '2147483647' to the Int32 value 2147483647. - // - } + foreach (string value in values) + { + try + { + result = Convert.ToInt32(value); + Console.WriteLine($"Converted the {value.GetType().Name} value '{value}' to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"{value} is outside the range of the Int32 type."); + } + catch (FormatException) + { + Console.WriteLine($"The {value.GetType().Name} value '{value}' is not in a recognizable format."); + } + } + // The example displays the following output: + // The String value 'One' is not in a recognizable format. + // The String value '1.34e28' is not in a recognizable format. + // The String value '-26.87' is not in a recognizable format. + // Converted the String value '-18' to the Int32 value -18. + // The String value '-6.00' is not in a recognizable format. + // Converted the String value ' 0' to the Int32 value 0. + // Converted the String value '137' to the Int32 value 137. + // The String value '1601.9' is not in a recognizable format. + // Converted the String value '2147483647' to the Int32 value 2147483647. + // + } - private static void ConvertUInt16() - { - // - ushort[] numbers = { UInt16.MinValue, 121, 340, UInt16.MaxValue }; - int result; - foreach (ushort number in numbers) - { - try { - result = Convert.ToInt32(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Int32 type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // Converted the UInt16 value 0 to the Int32 value 0. - // Converted the UInt16 value 121 to the Int32 value 121. - // Converted the UInt16 value 340 to the Int32 value 340. - // Converted the UInt16 value 65535 to the Int32 value 65535. - // - } + private static void ConvertUInt16() + { + // + ushort[] numbers = { ushort.MinValue, 121, 340, ushort.MaxValue }; + int result; + foreach (ushort number in numbers) + { + try + { + result = Convert.ToInt32(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the Int32 type."); + } + } + // The example displays the following output: + // Converted the UInt16 value 0 to the Int32 value 0. + // Converted the UInt16 value 121 to the Int32 value 121. + // Converted the UInt16 value 340 to the Int32 value 340. + // Converted the UInt16 value 65535 to the Int32 value 65535. + // + } - private static void ConvertUInt32() - { - // - uint[] numbers = { UInt32.MinValue, 121, 340, UInt32.MaxValue }; - int result; - foreach (uint number in numbers) - { - try { - result = Convert.ToInt32(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Int32 type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // Converted the UInt32 value 0 to the Int32 value 0. - // Converted the UInt32 value 121 to the Int32 value 121. - // Converted the UInt32 value 340 to the Int32 value 340. - // The UInt32 value 4294967295 is outside the range of the Int32 type. - // - } + private static void ConvertUInt32() + { + // + uint[] numbers = { uint.MinValue, 121, 340, uint.MaxValue }; + int result; + foreach (uint number in numbers) + { + try + { + result = Convert.ToInt32(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the Int32 type."); + } + } + // The example displays the following output: + // Converted the UInt32 value 0 to the Int32 value 0. + // Converted the UInt32 value 121 to the Int32 value 121. + // Converted the UInt32 value 340 to the Int32 value 340. + // The UInt32 value 4294967295 is outside the range of the Int32 type. + // + } - private static void ConvertUInt64() - { - // - ulong[] numbers = { UInt64.MinValue, 121, 340, UInt64.MaxValue }; - int result; - foreach (ulong number in numbers) - { - try { - result = Convert.ToInt32(number); - Console.WriteLine("Converted the {0} value {1} to a {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Int32 type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // Converted the UInt64 value 0 to a Int32 value 0. - // Converted the UInt64 value 121 to a Int32 value 121. - // Converted the UInt64 value 340 to a Int32 value 340. - // The UInt64 value 18446744073709551615 is outside the range of the Int32 type. - // - } + private static void ConvertUInt64() + { + // + ulong[] numbers = { ulong.MinValue, 121, 340, ulong.MaxValue }; + int result; + foreach (ulong number in numbers) + { + try + { + result = Convert.ToInt32(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to a {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the Int32 type."); + } + } + // The example displays the following output: + // Converted the UInt64 value 0 to a Int32 value 0. + // Converted the UInt64 value 121 to a Int32 value 121. + // Converted the UInt64 value 340 to a Int32 value 340. + // The UInt64 value 18446744073709551615 is outside the range of the Int32 type. + // + } } diff --git a/snippets/csharp/System/Convert/ToInt32/toint32_2.cs b/snippets/csharp/System/Convert/ToInt32/toint32_2.cs index 26b56bbf493..e16a21d18ab 100644 --- a/snippets/csharp/System/Convert/ToInt32/toint32_2.cs +++ b/snippets/csharp/System/Convert/ToInt32/toint32_2.cs @@ -4,44 +4,49 @@ public class Example { - public static void Main() - { - // Create a custom NumberFormatInfo object and set its two properties - // used by default in parsing numeric strings. - NumberFormatInfo customProvider = new NumberFormatInfo(); - customProvider.NegativeSign = "neg "; - customProvider.PositiveSign = "pos "; + public static void Main() + { + // Create a custom NumberFormatInfo object and set its two properties + // used by default in parsing numeric strings. + NumberFormatInfo customProvider = new() + { + NegativeSign = "neg ", + PositiveSign = "pos " + }; - // Add custom and invariant provider to an array of providers. - NumberFormatInfo[] providers = { customProvider, NumberFormatInfo.InvariantInfo }; + // Add custom and invariant provider to an array of providers. + NumberFormatInfo[] providers = { customProvider, NumberFormatInfo.InvariantInfo }; - // Define an array of strings to convert. - string[] numericStrings = { "123456789", "+123456789", "pos 123456789", + // Define an array of strings to convert. + string[] numericStrings = { "123456789", "+123456789", "pos 123456789", "-123456789", "neg 123456789", "123456789.", "123,456,789", "(123456789)", "2147483648", "-2147483649" }; - // Use each provider to parse all the numeric strings. - for (int ctr = 0; ctr <= 1; ctr++) - { - IFormatProvider provider = providers[ctr]; - Console.WriteLine(ctr == 0 ? "Custom Provider:" : "Invariant Provider:"); - foreach (string numericString in numericStrings) - { - Console.Write("{0,15} --> ", numericString); - try { - Console.WriteLine("{0,20}", Convert.ToInt32(numericString, provider)); + // Use each provider to parse all the numeric strings. + for (int ctr = 0; ctr <= 1; ctr++) + { + IFormatProvider provider = providers[ctr]; + Console.WriteLine(ctr == 0 ? "Custom Provider:" : "Invariant Provider:"); + foreach (string numericString in numericStrings) + { + Console.Write($"{numericString,15} --> "); + try + { + Console.WriteLine($"{Convert.ToInt32(numericString, provider),20}"); + } + catch (FormatException) + { + Console.WriteLine($"{"FormatException",20}"); + } + catch (OverflowException) + { + Console.WriteLine($"{"OverflowException",20}"); + } } - catch (FormatException) { - Console.WriteLine("{0,20}", "FormatException"); - } - catch (OverflowException) { - Console.WriteLine("{0,20}", "OverflowException"); - } - } - Console.WriteLine(); - } - } + Console.WriteLine(); + } + } } // The example displays the following output: // Custom Provider: diff --git a/snippets/csharp/System/Convert/ToInt64/toint64_1.cs b/snippets/csharp/System/Convert/ToInt64/toint64_1.cs index 4a8d98221e4..b3cfece830f 100644 --- a/snippets/csharp/System/Convert/ToInt64/toint64_1.cs +++ b/snippets/csharp/System/Convert/ToInt64/toint64_1.cs @@ -2,409 +2,393 @@ public class Example { - public static void Main() - { - ConvertBoolean(); - Console.WriteLine("-----"); - ConvertByte(); - Console.WriteLine("-----"); - ConvertChar(); - Console.WriteLine("-----"); - ConvertDecimal(); - Console.WriteLine("-----"); - ConvertDouble(); - Console.WriteLine("-----"); - ConvertInt16(); - Console.WriteLine("-----"); - ConvertInt32(); - Console.WriteLine("-----"); - ConvertObject(); - Console.WriteLine("-----"); - ConvertSByte(); - Console.WriteLine("-----"); - ConvertSingle(); - Console.WriteLine("----"); - ConvertString(); - Console.WriteLine("-----"); - ConvertUInt16(); - Console.WriteLine("-----"); - ConvertUInt32(); - Console.WriteLine("-----"); - ConvertUInt64(); + public static void Main() + { + ConvertBoolean(); + Console.WriteLine("-----"); + ConvertByte(); + Console.WriteLine("-----"); + ConvertChar(); + Console.WriteLine("-----"); + ConvertDecimal(); + Console.WriteLine("-----"); + ConvertDouble(); + Console.WriteLine("-----"); + ConvertInt16(); + Console.WriteLine("-----"); + ConvertInt32(); + Console.WriteLine("-----"); + ConvertObject(); + Console.WriteLine("-----"); + ConvertSByte(); + Console.WriteLine("-----"); + ConvertSingle(); + Console.WriteLine("----"); + ConvertString(); + Console.WriteLine("-----"); + ConvertUInt16(); + Console.WriteLine("-----"); + ConvertUInt32(); + Console.WriteLine("-----"); + ConvertUInt64(); } - private static void ConvertBoolean() - { - // - bool falseFlag = false; - bool trueFlag = true; + private static void ConvertBoolean() + { + // + bool falseFlag = false; + bool trueFlag = true; - Console.WriteLine("{0} converts to {1}.", falseFlag, - Convert.ToInt64(falseFlag)); - Console.WriteLine("{0} converts to {1}.", trueFlag, - Convert.ToInt64(trueFlag)); - // The example displays the following output: - // False converts to 0. - // True converts to 1. - // - } + Console.WriteLine($"{falseFlag} converts to {Convert.ToInt64(falseFlag)}."); + Console.WriteLine($"{trueFlag} converts to {Convert.ToInt64(trueFlag)}."); + // The example displays the following output: + // False converts to 0. + // True converts to 1. + // + } - private static void ConvertByte() - { - // - byte[] bytes = { Byte.MinValue, 14, 122, Byte.MaxValue}; - long result; + private static void ConvertByte() + { + // + byte[] bytes = { byte.MinValue, 14, 122, byte.MaxValue }; + long result; - foreach (byte byteValue in bytes) - { - result = Convert.ToInt64(byteValue); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - byteValue.GetType().Name, byteValue, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the Byte value 0 to the Int64 value 0. - // Converted the Byte value 14 to the Int64 value 14. - // Converted the Byte value 122 to the Int64 value 122. - // Converted the Byte value 255 to the Int64 value 255. - // - } + foreach (byte byteValue in bytes) + { + result = Convert.ToInt64(byteValue); + Console.WriteLine($"Converted the {byteValue.GetType().Name} value {byteValue} to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the Byte value 0 to the Int64 value 0. + // Converted the Byte value 14 to the Int64 value 14. + // Converted the Byte value 122 to the Int64 value 122. + // Converted the Byte value 255 to the Int64 value 255. + // + } - private static void ConvertChar() - { - // - char[] chars = { 'a', 'z', '\u0007', '\u03FF', + private static void ConvertChar() + { + // + char[] chars = { 'a', 'z', '\u0007', '\u03FF', '\u7FFF', '\uFFFE' }; - long result; + long result; - foreach (char ch in chars) - { - result = Convert.ToInt64(ch); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - ch.GetType().Name, ch, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the Char value 'a' to the Int64 value 97. - // Converted the Char value 'z' to the Int64 value 122. - // Converted the Char value '' to the Int64 value 7. - // Converted the Char value 'Ͽ' to the Int64 value 1023. - // Converted the Char value '翿' to the Int64 value 32767. - // Converted the Char value '￾' to the Int64 value 65534. - // - } + foreach (char ch in chars) + { + result = Convert.ToInt64(ch); + Console.WriteLine($"Converted the {ch.GetType().Name} value '{ch}' to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the Char value 'a' to the Int64 value 97. + // Converted the Char value 'z' to the Int64 value 122. + // Converted the Char value '' to the Int64 value 7. + // Converted the Char value 'Ͽ' to the Int64 value 1023. + // Converted the Char value '翿' to the Int64 value 32767. + // Converted the Char value '￾' to the Int64 value 65534. + // + } - private static void ConvertDecimal() - { - // - decimal[] values= { Decimal.MinValue, -1034.23m, -12m, 0m, 147m, - 199.55m, 9214.16m, Decimal.MaxValue }; - long result; + private static void ConvertDecimal() + { + // + decimal[] values = { decimal.MinValue, -1034.23m, -12m, 0m, 147m, + 199.55m, 9214.16m, decimal.MaxValue }; + long result; - foreach (decimal value in values) - { - try { - result = Convert.ToInt64(value); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - value.GetType().Name, value, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the Int64 type.", - value); - } - } - // The example displays the following output: - // -79228162514264337593543950335 is outside the range of the Int64 type. - // Converted the Decimal value '-1034.23' to the Int64 value -1034. - // Converted the Decimal value '-12' to the Int64 value -12. - // Converted the Decimal value '0' to the Int64 value 0. - // Converted the Decimal value '147' to the Int64 value 147. - // Converted the Decimal value '199.55' to the Int64 value 200. - // Converted the Decimal value '9214.16' to the Int64 value 9214. - // 79228162514264337593543950335 is outside the range of the Int64 type. - // - } + foreach (decimal value in values) + { + try + { + result = Convert.ToInt64(value); + Console.WriteLine($"Converted the {value.GetType().Name} value '{value}' to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"{value} is outside the range of the Int64 type."); + } + } + // The example displays the following output: + // -79228162514264337593543950335 is outside the range of the Int64 type. + // Converted the Decimal value '-1034.23' to the Int64 value -1034. + // Converted the Decimal value '-12' to the Int64 value -12. + // Converted the Decimal value '0' to the Int64 value 0. + // Converted the Decimal value '147' to the Int64 value 147. + // Converted the Decimal value '199.55' to the Int64 value 200. + // Converted the Decimal value '9214.16' to the Int64 value 9214. + // 79228162514264337593543950335 is outside the range of the Int64 type. + // + } - private static void ConvertDouble() - { - // - double[] values= { Double.MinValue, -1.38e10, -1023.299, -12.98, - 0, 9.113e-16, 103.919, 17834.191, Double.MaxValue }; - long result; + private static void ConvertDouble() + { + // + double[] values = { double.MinValue, -1.38e10, -1023.299, -12.98, + 0, 9.113e-16, 103.919, 17834.191, double.MaxValue }; + long result; - foreach (double value in values) - { - try { - result = Convert.ToInt64(value); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - value.GetType().Name, value, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the Int64 type.", value); - } - } - // -1.7976931348623157E+308 is outside the range of the Int64 type. - // Converted the Double value '-13800000000' to the Int64 value -13800000000. - // Converted the Double value '-1023.299' to the Int64 value -1023. - // Converted the Double value '-12.98' to the Int64 value -13. - // Converted the Double value '0' to the Int64 value 0. - // Converted the Double value '9.113E-16' to the Int64 value 0. - // Converted the Double value '103.919' to the Int64 value 104. - // Converted the Double value '17834.191' to the Int64 value 17834. - // 1.7976931348623157E+308 is outside the range of the Int64 type. - // - } + foreach (double value in values) + { + try + { + result = Convert.ToInt64(value); + Console.WriteLine($"Converted the {value.GetType().Name} value '{value}' to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"{value} is outside the range of the Int64 type."); + } + } + // -1.7976931348623157E+308 is outside the range of the Int64 type. + // Converted the Double value '-13800000000' to the Int64 value -13800000000. + // Converted the Double value '-1023.299' to the Int64 value -1023. + // Converted the Double value '-12.98' to the Int64 value -13. + // Converted the Double value '0' to the Int64 value 0. + // Converted the Double value '9.113E-16' to the Int64 value 0. + // Converted the Double value '103.919' to the Int64 value 104. + // Converted the Double value '17834.191' to the Int64 value 17834. + // 1.7976931348623157E+308 is outside the range of the Int64 type. + // + } - private static void ConvertInt16() - { - // - short[] numbers= { Int16.MinValue, -1, 0, 121, 340, Int16.MaxValue }; - long result; + private static void ConvertInt16() + { + // + short[] numbers = { short.MinValue, -1, 0, 121, 340, short.MaxValue }; + long result; - foreach (short number in numbers) - { - result = Convert.ToInt64(number); - Console.WriteLine("Converted the {0} value {1} to a {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the Int16 value -32768 to a Int32 value -32768. - // Converted the Int16 value -1 to a Int32 value -1. - // Converted the Int16 value 0 to a Int32 value 0. - // Converted the Int16 value 121 to a Int32 value 121. - // Converted the Int16 value 340 to a Int32 value 340. - // Converted the Int16 value 32767 to a Int32 value 32767. - // - } + foreach (short number in numbers) + { + result = Convert.ToInt64(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to a {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the Int16 value -32768 to a Int32 value -32768. + // Converted the Int16 value -1 to a Int32 value -1. + // Converted the Int16 value 0 to a Int32 value 0. + // Converted the Int16 value 121 to a Int32 value 121. + // Converted the Int16 value 340 to a Int32 value 340. + // Converted the Int16 value 32767 to a Int32 value 32767. + // + } - private static void ConvertInt32() - { - // - int[] numbers = { Int32.MinValue, -1, 0, 121, 340, Int32.MaxValue }; - long result; - foreach (int number in numbers) - { - result = Convert.ToInt64(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the Int32 value -2147483648 to the Int64 value -2147483648. - // Converted the Int32 value -1 to the Int64 value -1. - // Converted the Int32 value 0 to the Int64 value 0. - // Converted the Int32 value 121 to the Int64 value 121. - // Converted the Int32 value 340 to the Int64 value 340. - // Converted the Int32 value 2147483647 to the Int64 value 2147483647. - // - } + private static void ConvertInt32() + { + // + int[] numbers = { int.MinValue, -1, 0, 121, 340, int.MaxValue }; + long result; + foreach (int number in numbers) + { + result = Convert.ToInt64(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the Int32 value -2147483648 to the Int64 value -2147483648. + // Converted the Int32 value -1 to the Int64 value -1. + // Converted the Int32 value 0 to the Int64 value 0. + // Converted the Int32 value 121 to the Int64 value 121. + // Converted the Int32 value 340 to the Int64 value 340. + // Converted the Int32 value 2147483647 to the Int64 value 2147483647. + // + } - private static void ConvertObject() - { - // - object[] values = { true, -12, 163, 935, 'x', new DateTime(2009, 5, 12), + private static void ConvertObject() + { + // + object[] values = { true, -12, 163, 935, 'x', new DateTime(2009, 5, 12), "104", "103.0", "-1", "1.00e2", "One", 1.00e2, 16.3e42}; - long result; - - foreach (object value in values) - { - try { - result = Convert.ToInt64(value); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - value.GetType().Name, value, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Int64 type.", - value.GetType().Name, value); - } - catch (FormatException) { - Console.WriteLine("The {0} value {1} is not in a recognizable format.", - value.GetType().Name, value); - } - catch (InvalidCastException) { - Console.WriteLine("No conversion to an Int64 exists for the {0} value {1}.", - value.GetType().Name, value); - } - } - // The example displays the following output: - // Converted the Boolean value True to the Int64 value 1. - // Converted the Int32 value -12 to the Int64 value -12. - // Converted the Int32 value 163 to the Int64 value 163. - // Converted the Int32 value 935 to the Int64 value 935. - // Converted the Char value x to the Int64 value 120. - // No conversion to an Int64 exists for the DateTime value 5/12/2009 12:00:00 AM. - // Converted the String value 104 to the Int64 value 104. - // The String value 103.0 is not in a recognizable format. - // Converted the String value -1 to the Int64 value -1. - // The String value 1.00e2 is not in a recognizable format. - // The String value One is not in a recognizable format. - // Converted the Double value 100 to the Int64 value 100. - // The Double value 1.63E+43 is outside the range of the Int64 type. - // - } + long result; - private static void ConvertSByte() - { - // - sbyte[] numbers = { SByte.MinValue, -1, 0, 10, SByte.MaxValue }; - long result; + foreach (object value in values) + { + try + { + result = Convert.ToInt64(value); + Console.WriteLine($"Converted the {value.GetType().Name} value {value} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {value.GetType().Name} value {value} is outside the range of the Int64 type."); + } + catch (FormatException) + { + Console.WriteLine($"The {value.GetType().Name} value {value} is not in a recognizable format."); + } + catch (InvalidCastException) + { + Console.WriteLine($"No conversion to an Int64 exists for the {value.GetType().Name} value {value}."); + } + } + // The example displays the following output: + // Converted the Boolean value True to the Int64 value 1. + // Converted the Int32 value -12 to the Int64 value -12. + // Converted the Int32 value 163 to the Int64 value 163. + // Converted the Int32 value 935 to the Int64 value 935. + // Converted the Char value x to the Int64 value 120. + // No conversion to an Int64 exists for the DateTime value 5/12/2009 12:00:00 AM. + // Converted the String value 104 to the Int64 value 104. + // The String value 103.0 is not in a recognizable format. + // Converted the String value -1 to the Int64 value -1. + // The String value 1.00e2 is not in a recognizable format. + // The String value One is not in a recognizable format. + // Converted the Double value 100 to the Int64 value 100. + // The Double value 1.63E+43 is outside the range of the Int64 type. + // + } - foreach (sbyte number in numbers) - { - result = Convert.ToInt64(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the SByte value -128 to the Int64 value -128. - // Converted the SByte value -1 to the Int64 value -1. - // Converted the SByte value 0 to the Int64 value 0. - // Converted the SByte value 10 to the Int64 value 10. - // Converted the SByte value 127 to the Int64 value 127. - // - } + private static void ConvertSByte() + { + // + sbyte[] numbers = { sbyte.MinValue, -1, 0, 10, sbyte.MaxValue }; + long result; - private static void ConvertSingle() - { - // - float[] values= { Single.MinValue, -1.38e10f, -1023.299f, -12.98f, - 0f, 9.113e-16f, 103.919f, 17834.191f, Single.MaxValue }; - long result; + foreach (sbyte number in numbers) + { + result = Convert.ToInt64(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the SByte value -128 to the Int64 value -128. + // Converted the SByte value -1 to the Int64 value -1. + // Converted the SByte value 0 to the Int64 value 0. + // Converted the SByte value 10 to the Int64 value 10. + // Converted the SByte value 127 to the Int64 value 127. + // + } - foreach (float value in values) - { - try { - result = Convert.ToInt64(value); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - value.GetType().Name, value, result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the Int64 type.", value); - } - } - // The example displays the following output: - // -3.4028235E+38 is outside the range of the Int64 type. - // Converted the Single value -1.38E+10 to the Int64 value -13799999488. - // Converted the Single value -1023.299 to the Int64 value -1023. - // Converted the Single value -12.98 to the Int64 value -13. - // Converted the Single value 0 to the Int64 value 0. - // Converted the Single value 9.113E-16 to the Int64 value 0. - // Converted the Single value 103.919 to the Int64 value 104. - // Converted the Single value 17834.191 to the Int64 value 17834. - // 3.4028235E+38 is outside the range of the Int64 type. - // - } + private static void ConvertSingle() + { + // + float[] values = { float.MinValue, -1.38e10f, -1023.299f, -12.98f, + 0f, 9.113e-16f, 103.919f, 17834.191f, float.MaxValue }; + long result; - private static void ConvertString() - { - // - string[] values = { "One", "1.34e28", "-26.87", "-18", "-6.00", - " 0", "137", "1601.9", Int32.MaxValue.ToString() }; - long result; + foreach (float value in values) + { + try + { + result = Convert.ToInt64(value); + Console.WriteLine($"Converted the {value.GetType().Name} value {value} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"{value} is outside the range of the Int64 type."); + } + } + // The example displays the following output: + // -3.4028235E+38 is outside the range of the Int64 type. + // Converted the Single value -1.38E+10 to the Int64 value -13799999488. + // Converted the Single value -1023.299 to the Int64 value -1023. + // Converted the Single value -12.98 to the Int64 value -13. + // Converted the Single value 0 to the Int64 value 0. + // Converted the Single value 9.113E-16 to the Int64 value 0. + // Converted the Single value 103.919 to the Int64 value 104. + // Converted the Single value 17834.191 to the Int64 value 17834. + // 3.4028235E+38 is outside the range of the Int64 type. + // + } - foreach (string value in values) - { - try { - result = Convert.ToInt64(value); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - value.GetType().Name, value, result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the Int64 type.", value); - } - catch (FormatException) { - Console.WriteLine("The {0} value '{1}' is not in a recognizable format.", - value.GetType().Name, value); - } - } - // The example displays the following output: - // The String value 'One' is not in a recognizable format. - // The String value '1.34e28' is not in a recognizable format. - // The String value '-26.87' is not in a recognizable format. - // Converted the String value '-18' to the Int64 value -18. - // The String value '-6.00' is not in a recognizable format. - // Converted the String value ' 0' to the Int64 value 0. - // Converted the String value '137' to the Int64 value 137. - // The String value '1601.9' is not in a recognizable format. - // Converted the String value '2147483647' to the Int64 value 2147483647. - // - } + private static void ConvertString() + { + // + string[] values = { "One", "1.34e28", "-26.87", "-18", "-6.00", + " 0", "137", "1601.9", int.MaxValue.ToString() }; + long result; - private static void ConvertUInt16() - { - // - ushort[] numbers = { UInt16.MinValue, 121, 340, UInt16.MaxValue }; - long result; - foreach (ushort number in numbers) - { - try { - result = Convert.ToInt64(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Int64 type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // Converted the UInt16 value 0 to the Int64 value 0. - // Converted the UInt16 value 121 to the Int64 value 121. - // Converted the UInt16 value 340 to the Int64 value 340. - // Converted the UInt16 value 65535 to the Int64 value 65535. - // - } + foreach (string value in values) + { + try + { + result = Convert.ToInt64(value); + Console.WriteLine($"Converted the {value.GetType().Name} value '{value}' to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"{value} is outside the range of the Int64 type."); + } + catch (FormatException) + { + Console.WriteLine($"The {value.GetType().Name} value '{value}' is not in a recognizable format."); + } + } + // The example displays the following output: + // The String value 'One' is not in a recognizable format. + // The String value '1.34e28' is not in a recognizable format. + // The String value '-26.87' is not in a recognizable format. + // Converted the String value '-18' to the Int64 value -18. + // The String value '-6.00' is not in a recognizable format. + // Converted the String value ' 0' to the Int64 value 0. + // Converted the String value '137' to the Int64 value 137. + // The String value '1601.9' is not in a recognizable format. + // Converted the String value '2147483647' to the Int64 value 2147483647. + // + } - private static void ConvertUInt32() - { - // - uint[] numbers = { UInt32.MinValue, 121, 340, UInt32.MaxValue }; - long result; - foreach (uint number in numbers) - { - result = Convert.ToInt64(number); - Console.WriteLine("Converted the {0} value {1:N0} to the {2} value {3:N0}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the UInt32 value 0 to the Int64 value 0. - // Converted the UInt32 value 121 to the Int64 value 121. - // Converted the UInt32 value 340 to the Int64 value 340. - // Converted the UInt32 value 4,294,967,295 to the Int64 value 4,294,967,295. - // - } + private static void ConvertUInt16() + { + // + ushort[] numbers = { ushort.MinValue, 121, 340, ushort.MaxValue }; + long result; + foreach (ushort number in numbers) + { + try + { + result = Convert.ToInt64(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the Int64 type."); + } + } + // The example displays the following output: + // Converted the UInt16 value 0 to the Int64 value 0. + // Converted the UInt16 value 121 to the Int64 value 121. + // Converted the UInt16 value 340 to the Int64 value 340. + // Converted the UInt16 value 65535 to the Int64 value 65535. + // + } - private static void ConvertUInt64() - { - // - ulong[] numbers = { UInt64.MinValue, 121, 340, UInt64.MaxValue }; - long result; - foreach (ulong number in numbers) - { - try { + private static void ConvertUInt32() + { + // + uint[] numbers = { uint.MinValue, 121, 340, uint.MaxValue }; + long result; + foreach (uint number in numbers) + { result = Convert.ToInt64(number); - Console.WriteLine("Converted the {0} value {1} to a {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Int64 type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // Converted the UInt64 value 0 to a Int32 value 0. - // Converted the UInt64 value 121 to a Int32 value 121. - // Converted the UInt64 value 340 to a Int32 value 340. - // The UInt64 value 18446744073709551615 is outside the range of the Int64 type. - // - } + Console.WriteLine($"Converted the {number.GetType().Name} value {number:N0} to the {result.GetType().Name} value {result:N0}."); + } + // The example displays the following output: + // Converted the UInt32 value 0 to the Int64 value 0. + // Converted the UInt32 value 121 to the Int64 value 121. + // Converted the UInt32 value 340 to the Int64 value 340. + // Converted the UInt32 value 4,294,967,295 to the Int64 value 4,294,967,295. + // + } + + private static void ConvertUInt64() + { + // + ulong[] numbers = { ulong.MinValue, 121, 340, ulong.MaxValue }; + long result; + foreach (ulong number in numbers) + { + try + { + result = Convert.ToInt64(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to a {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the Int64 type."); + } + } + // The example displays the following output: + // Converted the UInt64 value 0 to a Int32 value 0. + // Converted the UInt64 value 121 to a Int32 value 121. + // Converted the UInt64 value 340 to a Int32 value 340. + // The UInt64 value 18446744073709551615 is outside the range of the Int64 type. + // + } } diff --git a/snippets/csharp/System/Convert/ToInt64/toint64_2.cs b/snippets/csharp/System/Convert/ToInt64/toint64_2.cs index daa7dc3fe09..5f03ea31e79 100644 --- a/snippets/csharp/System/Convert/ToInt64/toint64_2.cs +++ b/snippets/csharp/System/Convert/ToInt64/toint64_2.cs @@ -3,28 +3,31 @@ public class Example { - public static void Main() - { - string[] hexStrings = { "8000000000000000", "0FFFFFFFFFFFFFFF", + public static void Main() + { + string[] hexStrings = { "8000000000000000", "0FFFFFFFFFFFFFFF", "f0000000000001000", "00A30", "D", "-13", "GAD" }; - foreach (string hexString in hexStrings) - { - try { - long number = Convert.ToInt64(hexString, 16); - Console.WriteLine("Converted '{0}' to {1:N0}.", hexString, number); - } - catch (FormatException) { - Console.WriteLine("'{0}' is not in the correct format for a hexadecimal number.", - hexString); - } - catch (OverflowException) { - Console.WriteLine("'{0}' is outside the range of an Int64.", hexString); - } - catch (ArgumentException) { - Console.WriteLine("'{0}' is invalid in base 16.", hexString); - } - } - } + foreach (string hexString in hexStrings) + { + try + { + long number = Convert.ToInt64(hexString, 16); + Console.WriteLine($"Converted '{hexString}' to {number:N0}."); + } + catch (FormatException) + { + Console.WriteLine($"'{hexString}' is not in the correct format for a hexadecimal number."); + } + catch (OverflowException) + { + Console.WriteLine($"'{hexString}' is outside the range of an Int64."); + } + catch (ArgumentException) + { + Console.WriteLine($"'{hexString}' is invalid in base 16."); + } + } + } } // The example displays the following output: // Converted '8000000000000000' to -9,223,372,036,854,775,808. diff --git a/snippets/csharp/System/Convert/ToInt64/toint64_3.cs b/snippets/csharp/System/Convert/ToInt64/toint64_3.cs index 51b8e9ca000..6ab723f32ef 100644 --- a/snippets/csharp/System/Convert/ToInt64/toint64_3.cs +++ b/snippets/csharp/System/Convert/ToInt64/toint64_3.cs @@ -4,45 +4,50 @@ public class Example { - public static void Main() - { - // Create a NumberFormatInfo object and set the properties that - // affect conversions using Convert.ToInt64(String, IFormatProvider). - NumberFormatInfo customProvider = new NumberFormatInfo(); - customProvider.NegativeSign = "neg "; - customProvider.PositiveSign = "pos "; + public static void Main() + { + // Create a NumberFormatInfo object and set the properties that + // affect conversions using Convert.ToInt64(String, IFormatProvider). + NumberFormatInfo customProvider = new() + { + NegativeSign = "neg ", + PositiveSign = "pos " + }; - // Create an array of providers with the custom provider and the - // NumberFormatInfo object for the invariant culture. - NumberFormatInfo[] providers = { customProvider, + // Create an array of providers with the custom provider and the + // NumberFormatInfo object for the invariant culture. + NumberFormatInfo[] providers = { customProvider, NumberFormatInfo.InvariantInfo }; - // Define an array of strings to parse. - string[] numericStrings = { "123456789", "+123456789", "pos 123456789", + // Define an array of strings to parse. + string[] numericStrings = { "123456789", "+123456789", "pos 123456789", "-123456789", "neg 123456789", "123456789.", "123,456,789", "(123456789)", "9223372036854775808", "-9223372036854775809" }; - for (int ctr = 0; ctr < 2; ctr++) - { - IFormatProvider provider = providers[ctr]; - Console.WriteLine(ctr == 0 ? "Custom Provider:" : "Invariant Culture:"); - foreach (string numericString in numericStrings) - { - Console.Write(" {0,-22} --> ", numericString); - try { - Console.WriteLine("{0,22}", Convert.ToInt32(numericString, provider)); + for (int ctr = 0; ctr < 2; ctr++) + { + IFormatProvider provider = providers[ctr]; + Console.WriteLine(ctr == 0 ? "Custom Provider:" : "Invariant Culture:"); + foreach (string numericString in numericStrings) + { + Console.Write($" {numericString,-22} --> "); + try + { + Console.WriteLine($"{Convert.ToInt32(numericString, provider),22}"); + } + catch (FormatException) + { + Console.WriteLine($"{"Unrecognized Format",22}"); + } + catch (OverflowException) + { + Console.WriteLine($"{"Overflow",22}"); + } } - catch (FormatException) { - Console.WriteLine("{0,22}", "Unrecognized Format"); - } - catch (OverflowException) { - Console.WriteLine("{0,22}", "Overflow"); - } - } - Console.WriteLine(); - } - } + Console.WriteLine(); + } + } } // The example displays the following output: // Custom Provider: diff --git a/snippets/csharp/System/Convert/ToSByte/tosbyte1.cs b/snippets/csharp/System/Convert/ToSByte/tosbyte1.cs index b205ea814c9..300e8716c1f 100644 --- a/snippets/csharp/System/Convert/ToSByte/tosbyte1.cs +++ b/snippets/csharp/System/Convert/ToSByte/tosbyte1.cs @@ -2,406 +2,394 @@ public class Class1 { - public static void Main() - { - ConvertBoolean(); - Console.WriteLine("-----"); - ConvertByte(); - Console.WriteLine("-----"); - ConvertChar(); - Console.WriteLine("-----"); - ConvertDecimal(); - Console.WriteLine("-----"); - ConvertDouble(); - Console.WriteLine("-----"); - ConvertInt16(); - Console.WriteLine("-----"); - ConvertInt32(); - Console.WriteLine("-----"); - ConvertInt64(); - Console.WriteLine("-----"); - ConvertObject(); - Console.WriteLine("-----"); - ConvertSingle(); - Console.WriteLine("-----"); - ConvertUInt16(); - Console.WriteLine("-----"); - ConvertUInt32(); - Console.WriteLine("-----"); - ConvertUInt64(); - } + public static void Main() + { + ConvertBoolean(); + Console.WriteLine("-----"); + ConvertByte(); + Console.WriteLine("-----"); + ConvertChar(); + Console.WriteLine("-----"); + ConvertDecimal(); + Console.WriteLine("-----"); + ConvertDouble(); + Console.WriteLine("-----"); + ConvertInt16(); + Console.WriteLine("-----"); + ConvertInt32(); + Console.WriteLine("-----"); + ConvertInt64(); + Console.WriteLine("-----"); + ConvertObject(); + Console.WriteLine("-----"); + ConvertSingle(); + Console.WriteLine("-----"); + ConvertUInt16(); + Console.WriteLine("-----"); + ConvertUInt32(); + Console.WriteLine("-----"); + ConvertUInt64(); + } - private static void ConvertBoolean() - { - // - bool falseFlag = false; - bool trueFlag = true; + private static void ConvertBoolean() + { + // + bool falseFlag = false; + bool trueFlag = true; - Console.WriteLine("{0} converts to {1}.", falseFlag, - Convert.ToSByte(falseFlag)); - Console.WriteLine("{0} converts to {1}.", trueFlag, - Convert.ToSByte(trueFlag)); - // The example displays the following output: - // false converts to 0. - // true converts to 1. - // - } + Console.WriteLine($"{falseFlag} converts to {Convert.ToSByte(falseFlag)}."); + Console.WriteLine($"{trueFlag} converts to {Convert.ToSByte(trueFlag)}."); + // The example displays the following output: + // false converts to 0. + // true converts to 1. + // + } - private static void ConvertByte() - { - // - byte[] numbers = { Byte.MinValue, 10, 100, Byte.MaxValue }; - sbyte result; + private static void ConvertByte() + { + // + byte[] numbers = { byte.MinValue, 10, 100, byte.MaxValue }; + sbyte result; - foreach (byte number in numbers) - { - try { - result = Convert.ToSByte(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the SByte type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // Converted the Byte value 0 to the SByte value 0. - // Converted the Byte value 10 to the SByte value 10. - // Converted the Byte value 100 to the SByte value 100. - // The Byte value 255 is outside the range of the SByte type. - // - } + foreach (byte number in numbers) + { + try + { + result = Convert.ToSByte(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the SByte type."); + } + } + // The example displays the following output: + // Converted the Byte value 0 to the SByte value 0. + // Converted the Byte value 10 to the SByte value 10. + // Converted the Byte value 100 to the SByte value 100. + // The Byte value 255 is outside the range of the SByte type. + // + } - private static void ConvertChar() - { - // - char[] chars = { 'a', 'z', '\u0007', '\u0200', '\u1023' }; - foreach (char ch in chars) - { - try { - sbyte result = Convert.ToSByte(ch); - Console.WriteLine("{0} is converted to {1}.", ch, result); - } - catch (OverflowException) { - Console.WriteLine("Unable to convert u+{0} to a byte.", - Convert.ToInt16(ch).ToString("X4")); - } - } - // The example displays the following output: - // a is converted to 97. - // z is converted to 122. - // is converted to 7. - // Unable to convert u+00C8 to a byte. - // Unable to convert u+03FF to a byte. - // - } + private static void ConvertChar() + { + // + char[] chars = { 'a', 'z', '\u0007', '\u0200', '\u1023' }; + foreach (char ch in chars) + { + try + { + sbyte result = Convert.ToSByte(ch); + Console.WriteLine($"{ch} is converted to {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"Unable to convert u+{Convert.ToInt16(ch).ToString("X4")} to a byte."); + } + } + // The example displays the following output: + // a is converted to 97. + // z is converted to 122. + // is converted to 7. + // Unable to convert u+00C8 to a byte. + // Unable to convert u+03FF to a byte. + // + } - private static void ConvertDecimal() - { - // - decimal[] numbers = { Decimal.MinValue, -129.5m, -12.7m, 0m, 16m, - 103.6m, 255.0m, Decimal.MaxValue }; - sbyte result; + private static void ConvertDecimal() + { + // + decimal[] numbers = { decimal.MinValue, -129.5m, -12.7m, 0m, 16m, + 103.6m, 255.0m, decimal.MaxValue }; + sbyte result; - foreach (decimal number in numbers) - { - try { - result = Convert.ToSByte(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the SByte type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // The Decimal value -79228162514264337593543950335 is outside the range of the SByte type. - // The Decimal value -129.5 is outside the range of the SByte type. - // Converted the Decimal value -12.7 to the SByte value -13. - // Converted the Decimal value 0 to the SByte value 0. - // Converted the Decimal value 16 to the SByte value 16. - // Converted the Decimal value 103.6 to the SByte value 104. - // The Decimal value 255 is outside the range of the SByte type. - // The Decimal value 79228162514264337593543950335 is outside the range of the SByte type. - // - } + foreach (decimal number in numbers) + { + try + { + result = Convert.ToSByte(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the SByte type."); + } + } + // The example displays the following output: + // The Decimal value -79228162514264337593543950335 is outside the range of the SByte type. + // The Decimal value -129.5 is outside the range of the SByte type. + // Converted the Decimal value -12.7 to the SByte value -13. + // Converted the Decimal value 0 to the SByte value 0. + // Converted the Decimal value 16 to the SByte value 16. + // Converted the Decimal value 103.6 to the SByte value 104. + // The Decimal value 255 is outside the range of the SByte type. + // The Decimal value 79228162514264337593543950335 is outside the range of the SByte type. + // + } - private static void ConvertDouble() - { - // - double[] numbers = { Double.MinValue, -129.5, -12.7, 0, 16, - 103.6, 255.0, 1.63509e17, Double.MaxValue}; - sbyte result; + private static void ConvertDouble() + { + // + double[] numbers = { double.MinValue, -129.5, -12.7, 0, 16, + 103.6, 255.0, 1.63509e17, double.MaxValue}; + sbyte result; - foreach (double number in numbers) - { - try { - result = Convert.ToSByte(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the SByte type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // The Double value -1.79769313486232E+308 is outside the range of the SByte type. - // The Double value -129.5 is outside the range of the SByte type. - // Converted the Double value -12.7 to the SByte value -13. - // Converted the Double value 0 to the SByte value 0. - // Converted the Double value 16 to the SByte value 16. - // Converted the Double value 103.6 to the SByte value 104. - // The Double value 255 is outside the range of the SByte type. - // The Double value 1.63509E+17 is outside the range of the SByte type. - // The Double value 1.79769313486232E+308 is outside the range of the SByte type. - // - } + foreach (double number in numbers) + { + try + { + result = Convert.ToSByte(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the SByte type."); + } + } + // The example displays the following output: + // The Double value -1.79769313486232E+308 is outside the range of the SByte type. + // The Double value -129.5 is outside the range of the SByte type. + // Converted the Double value -12.7 to the SByte value -13. + // Converted the Double value 0 to the SByte value 0. + // Converted the Double value 16 to the SByte value 16. + // Converted the Double value 103.6 to the SByte value 104. + // The Double value 255 is outside the range of the SByte type. + // The Double value 1.63509E+17 is outside the range of the SByte type. + // The Double value 1.79769313486232E+308 is outside the range of the SByte type. + // + } - private static void ConvertInt16() - { - // - short[] numbers = { Int16.MinValue, -1, 0, 121, 340, Int16.MaxValue }; - sbyte result; - foreach (short number in numbers) - { - try { - result = Convert.ToSByte(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the SByte type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // The Int16 value -32768 is outside the range of the SByte type. - // Converted the Int16 value -1 to the SByte value -1. - // Converted the Int16 value 0 to the SByte value 0. - // Converted the Int16 value 121 to the SByte value 121. - // The Int16 value 340 is outside the range of the SByte type. - // The Int16 value 32767 is outside the range of the SByte type. - // - } + private static void ConvertInt16() + { + // + short[] numbers = { short.MinValue, -1, 0, 121, 340, short.MaxValue }; + sbyte result; + foreach (short number in numbers) + { + try + { + result = Convert.ToSByte(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the SByte type."); + } + } + // The example displays the following output: + // The Int16 value -32768 is outside the range of the SByte type. + // Converted the Int16 value -1 to the SByte value -1. + // Converted the Int16 value 0 to the SByte value 0. + // Converted the Int16 value 121 to the SByte value 121. + // The Int16 value 340 is outside the range of the SByte type. + // The Int16 value 32767 is outside the range of the SByte type. + // + } - private static void ConvertInt32() - { - // - int[] numbers = { Int32.MinValue, -1, 0, 121, 340, Int32.MaxValue }; - sbyte result; + private static void ConvertInt32() + { + // + int[] numbers = { int.MinValue, -1, 0, 121, 340, int.MaxValue }; + sbyte result; - foreach (int number in numbers) - { - try { - result = Convert.ToSByte(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the SByte type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // The Int32 value -2147483648 is outside the range of the SByte type. - // Converted the Int32 value -1 to the SByte value -1. - // Converted the Int32 value 0 to the SByte value 0. - // Converted the Int32 value 121 to the SByte value 121. - // The Int32 value 340 is outside the range of the SByte type. - // The Int32 value 2147483647 is outside the range of the SByte type. - // - } + foreach (int number in numbers) + { + try + { + result = Convert.ToSByte(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the SByte type."); + } + } + // The example displays the following output: + // The Int32 value -2147483648 is outside the range of the SByte type. + // Converted the Int32 value -1 to the SByte value -1. + // Converted the Int32 value 0 to the SByte value 0. + // Converted the Int32 value 121 to the SByte value 121. + // The Int32 value 340 is outside the range of the SByte type. + // The Int32 value 2147483647 is outside the range of the SByte type. + // + } - private static void ConvertInt64() - { - // - long[] numbers = { Int64.MinValue, -1, 0, 121, 340, Int64.MaxValue }; - sbyte result; - foreach (long number in numbers) - { - try { - result = Convert.ToSByte(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the SByte type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // The Int64 value -9223372036854775808 is outside the range of the SByte type. - // Converted the Int64 value -1 to the SByte value -1. - // Converted the Int64 value 0 to the SByte value 0. - // Converted the Int64 value 121 to the SByte value 121. - // The Int64 value 340 is outside the range of the SByte type. - // The Int64 value 9223372036854775807 is outside the range of the SByte type. - // - } + private static void ConvertInt64() + { + // + long[] numbers = { long.MinValue, -1, 0, 121, 340, long.MaxValue }; + sbyte result; + foreach (long number in numbers) + { + try + { + result = Convert.ToSByte(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the SByte type."); + } + } + // The example displays the following output: + // The Int64 value -9223372036854775808 is outside the range of the SByte type. + // Converted the Int64 value -1 to the SByte value -1. + // Converted the Int64 value 0 to the SByte value 0. + // Converted the Int64 value 121 to the SByte value 121. + // The Int64 value 340 is outside the range of the SByte type. + // The Int64 value 9223372036854775807 is outside the range of the SByte type. + // + } - private static void ConvertObject() - { - // - object[] values = { true, -12, 163, 935, 'x', "104", "103.0", "-1", + private static void ConvertObject() + { + // + object[] values = { true, -12, 163, 935, 'x', "104", "103.0", "-1", "1.00e2", "One", 1.00e2}; - sbyte result; + sbyte result; - foreach (object value in values) - { - try { - result = Convert.ToSByte(value); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - value.GetType().Name, value, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the SByte type.", - value.GetType().Name, value); - } - catch (FormatException) { - Console.WriteLine("The {0} value {1} is not in a recognizable format.", - value.GetType().Name, value); - } - catch (InvalidCastException) { - Console.WriteLine("No conversion to a Byte exists for the {0} value {1}.", - value.GetType().Name, value); - } - } - // The example displays the following output: - // Converted the Boolean value true to the SByte value 1. - // Converted the Int32 value -12 to the SByte value -12. - // The Int32 value 163 is outside the range of the SByte type. - // The Int32 value 935 is outside the range of the SByte type. - // Converted the Char value x to the SByte value 120. - // Converted the String value 104 to the SByte value 104. - // The String value 103.0 is not in a recognizable format. - // Converted the String value -1 to the SByte value -1. - // The String value 1.00e2 is not in a recognizable format. - // The String value One is not in a recognizable format. - // Converted the Double value 100 to the SByte value 100. - // - } + foreach (object value in values) + { + try + { + result = Convert.ToSByte(value); + Console.WriteLine($"Converted the {value.GetType().Name} value {value} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {value.GetType().Name} value {value} is outside the range of the SByte type."); + } + catch (FormatException) + { + Console.WriteLine($"The {value.GetType().Name} value {value} is not in a recognizable format."); + } + catch (InvalidCastException) + { + Console.WriteLine($"No conversion to a Byte exists for the {value.GetType().Name} value {value}."); + } + } + // The example displays the following output: + // Converted the Boolean value true to the SByte value 1. + // Converted the Int32 value -12 to the SByte value -12. + // The Int32 value 163 is outside the range of the SByte type. + // The Int32 value 935 is outside the range of the SByte type. + // Converted the Char value x to the SByte value 120. + // Converted the String value 104 to the SByte value 104. + // The String value 103.0 is not in a recognizable format. + // Converted the String value -1 to the SByte value -1. + // The String value 1.00e2 is not in a recognizable format. + // The String value One is not in a recognizable format. + // Converted the Double value 100 to the SByte value 100. + // + } - private static void ConvertSingle() - { - // - float[] numbers = { Single.MinValue, -129.5f, -12.7f, 0f, 16f, - 103.6f, 255.0f, 1.63509e17f, Single.MaxValue }; - sbyte result; + private static void ConvertSingle() + { + // + float[] numbers = { float.MinValue, -129.5f, -12.7f, 0f, 16f, + 103.6f, 255.0f, 1.63509e17f, float.MaxValue }; + sbyte result; - foreach (float number in numbers) - { - try { - result = Convert.ToSByte(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the SByte type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // The Single value -3.402823E+38 is outside the range of the SByte type. - // The Single value -129.5 is outside the range of the SByte type. - // Converted the Single value -12.7 to the SByte value -13. - // Converted the Single value 0 to the SByte value 0. - // Converted the Single value 16 to the SByte value 16. - // Converted the Single value 103.6 to the SByte value 104. - // The Single value 255 is outside the range of the SByte type. - // The Single value 1.63509E+17 is outside the range of the SByte type. - // The Single value 3.402823E+38 is outside the range of the SByte type. - // - } + foreach (float number in numbers) + { + try + { + result = Convert.ToSByte(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the SByte type."); + } + } + // The example displays the following output: + // The Single value -3.402823E+38 is outside the range of the SByte type. + // The Single value -129.5 is outside the range of the SByte type. + // Converted the Single value -12.7 to the SByte value -13. + // Converted the Single value 0 to the SByte value 0. + // Converted the Single value 16 to the SByte value 16. + // Converted the Single value 103.6 to the SByte value 104. + // The Single value 255 is outside the range of the SByte type. + // The Single value 1.63509E+17 is outside the range of the SByte type. + // The Single value 3.402823E+38 is outside the range of the SByte type. + // + } - private static void ConvertUInt16() - { - // - ushort[] numbers = { UInt16.MinValue, 121, 340, UInt16.MaxValue }; - sbyte result; + private static void ConvertUInt16() + { + // + ushort[] numbers = { ushort.MinValue, 121, 340, ushort.MaxValue }; + sbyte result; - foreach (ushort number in numbers) - { - try { - result = Convert.ToSByte(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the SByte type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // Converted the UInt16 value 0 to the SByte value 0. - // Converted the UInt16 value 121 to the SByte value 121. - // The UInt16 value 340 is outside the range of the SByte type. - // The UInt16 value 65535 is outside the range of the SByte type. - // - } + foreach (ushort number in numbers) + { + try + { + result = Convert.ToSByte(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the SByte type."); + } + } + // The example displays the following output: + // Converted the UInt16 value 0 to the SByte value 0. + // Converted the UInt16 value 121 to the SByte value 121. + // The UInt16 value 340 is outside the range of the SByte type. + // The UInt16 value 65535 is outside the range of the SByte type. + // + } - private static void ConvertUInt32() - { - // - uint[] numbers = { UInt32.MinValue, 121, 340, UInt32.MaxValue }; - sbyte result; + private static void ConvertUInt32() + { + // + uint[] numbers = { uint.MinValue, 121, 340, uint.MaxValue }; + sbyte result; - foreach (uint number in numbers) - { - try { - result = Convert.ToSByte(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the SByte type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // Converted the UInt32 value 0 to the SByte value 0. - // Converted the UInt32 value 121 to the SByte value 121. - // The UInt32 value 340 is outside the range of the SByte type. - // The UInt32 value 4294967295 is outside the range of the SByte type. - // - } + foreach (uint number in numbers) + { + try + { + result = Convert.ToSByte(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the SByte type."); + } + } + // The example displays the following output: + // Converted the UInt32 value 0 to the SByte value 0. + // Converted the UInt32 value 121 to the SByte value 121. + // The UInt32 value 340 is outside the range of the SByte type. + // The UInt32 value 4294967295 is outside the range of the SByte type. + // + } - private static void ConvertUInt64() - { - // - ulong[] numbers = { UInt64.MinValue, 121, 340, UInt64.MaxValue }; - sbyte result; + private static void ConvertUInt64() + { + // + ulong[] numbers = { ulong.MinValue, 121, 340, ulong.MaxValue }; + sbyte result; - foreach (ulong number in numbers) - { - try { - result = Convert.ToSByte(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the SByte type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // Converted the UInt64 value 0 to the SByte value 0. - // Converted the UInt64 value 121 to the SByte value 121. - // The UInt64 value 340 is outside the range of the SByte type. - // The UInt64 value 18446744073709551615 is outside the range of the SByte type. - // - } + foreach (ulong number in numbers) + { + try + { + result = Convert.ToSByte(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the SByte type."); + } + } + // The example displays the following output: + // Converted the UInt64 value 0 to the SByte value 0. + // Converted the UInt64 value 121 to the SByte value 121. + // The UInt64 value 340 is outside the range of the SByte type. + // The UInt64 value 18446744073709551615 is outside the range of the SByte type. + // + } } diff --git a/snippets/csharp/System/Convert/ToSByte/tosbyte2.cs b/snippets/csharp/System/Convert/ToSByte/tosbyte2.cs index 1680f9af42f..ce2dbf3a2c4 100644 --- a/snippets/csharp/System/Convert/ToSByte/tosbyte2.cs +++ b/snippets/csharp/System/Convert/ToSByte/tosbyte2.cs @@ -2,240 +2,241 @@ using System; using System.Globalization; -public enum SignBit { Negative=-1, Zero=0, Positive=1 }; +public enum SignBit { Negative = -1, Zero = 0, Positive = 1 }; public struct ByteString : IConvertible { - private SignBit signBit; - private string byteString; + private SignBit signBit; + private string byteString; - public SignBit Sign + public SignBit Sign { - set { signBit = value; } - get { return signBit; } + set => signBit = value; + get => signBit; } - public string Value - { - set { - if (value.Trim().Length > 2) - throw new ArgumentException("The string representation of a byte cannot have more than two characters."); - else - byteString = value; - } - get { return byteString; } - } - - // IConvertible implementations. - public TypeCode GetTypeCode() { - return TypeCode.Object; - } - - public bool ToBoolean(IFormatProvider provider) - { - if (signBit == SignBit.Zero) - return false; - else - return true; - } - - public byte ToByte(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - throw new OverflowException(String.Format("{0} is out of range of the Byte type.", Convert.ToSByte(byteString, 16))); - else - return Byte.Parse(byteString, NumberStyles.HexNumber); - } - - public char ToChar(IFormatProvider provider) - { - if (signBit == SignBit.Negative) { - throw new OverflowException(String.Format("{0} is out of range of the Char type.", Convert.ToSByte(byteString, 16))); - } - else { - byte byteValue = Byte.Parse(this.byteString, NumberStyles.HexNumber); - return Convert.ToChar(byteValue); - } - } - - public DateTime ToDateTime(IFormatProvider provider) - { - throw new InvalidCastException("ByteString to DateTime conversion is not supported."); - } - - public decimal ToDecimal(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - { - sbyte byteValue = SByte.Parse(byteString, NumberStyles.HexNumber); - return Convert.ToDecimal(byteValue); - } - else - { - byte byteValue = Byte.Parse(byteString, NumberStyles.HexNumber); - return Convert.ToDecimal(byteValue); - } - } - - public double ToDouble(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - return Convert.ToDouble(SByte.Parse(byteString, NumberStyles.HexNumber)); - else - return Convert.ToDouble(Byte.Parse(byteString, NumberStyles.HexNumber)); - } - - public short ToInt16(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - return Convert.ToInt16(SByte.Parse(byteString, NumberStyles.HexNumber)); - else - return Convert.ToInt16(Byte.Parse(byteString, NumberStyles.HexNumber)); - } - - public int ToInt32(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - return Convert.ToInt32(SByte.Parse(byteString, NumberStyles.HexNumber)); - else - return Convert.ToInt32(Byte.Parse(byteString, NumberStyles.HexNumber)); - } - - public long ToInt64(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - return Convert.ToInt64(SByte.Parse(byteString, NumberStyles.HexNumber)); - else - return Convert.ToInt64(Byte.Parse(byteString, NumberStyles.HexNumber)); - } - - public sbyte ToSByte(IFormatProvider provider) - { - try { - return SByte.Parse(byteString, NumberStyles.HexNumber); - } - catch (OverflowException e) { - throw new OverflowException(String.Format("{0} is outside the range of the SByte type.", - Byte.Parse(byteString, NumberStyles.HexNumber)), e); - } - } - - public float ToSingle(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - return Convert.ToSingle(SByte.Parse(byteString, NumberStyles.HexNumber)); - else - return Convert.ToSingle(Byte.Parse(byteString, NumberStyles.HexNumber)); - } - - public string ToString(IFormatProvider provider) - { - return "0x" + this.byteString; - } - - public object ToType(Type conversionType, IFormatProvider provider) - { - switch (Type.GetTypeCode(conversionType)) - { - case TypeCode.Boolean: - return this.ToBoolean(null); - case TypeCode.Byte: - return this.ToByte(null); - case TypeCode.Char: - return this.ToChar(null); - case TypeCode.DateTime: - return this.ToDateTime(null); - case TypeCode.Decimal: - return this.ToDecimal(null); - case TypeCode.Double: - return this.ToDouble(null); - case TypeCode.Int16: - return this.ToInt16(null); - case TypeCode.Int32: - return this.ToInt32(null); - case TypeCode.Int64: - return this.ToInt64(null); - case TypeCode.Object: - if (typeof(ByteString).Equals(conversionType)) - return this; + public string Value + { + set + { + if (value.Trim().Length > 2) + throw new ArgumentException("The string representation of a byte cannot have more than two characters."); else - throw new InvalidCastException(String.Format("Conversion to a {0} is not supported.", conversionType.Name)); - case TypeCode.SByte: - return this.ToSByte(null); - case TypeCode.Single: - return this.ToSingle(null); - case TypeCode.String: - return this.ToString(null); - case TypeCode.UInt16: - return this.ToUInt16(null); - case TypeCode.UInt32: - return this.ToUInt32(null); - case TypeCode.UInt64: - return this.ToUInt64(null); - default: - throw new InvalidCastException(String.Format("Conversion to {0} is not supported.", conversionType.Name)); - } - } - - public UInt16 ToUInt16(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - throw new OverflowException(String.Format("{0} is outside the range of the UInt16 type.", - SByte.Parse(byteString, NumberStyles.HexNumber))); - else - return Convert.ToUInt16(Byte.Parse(byteString, NumberStyles.HexNumber)); - } - - public UInt32 ToUInt32(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - throw new OverflowException(String.Format("{0} is outside the range of the UInt32 type.", - SByte.Parse(byteString, NumberStyles.HexNumber))); - else - return Convert.ToUInt32(Byte.Parse(byteString, NumberStyles.HexNumber)); - } - - public UInt64 ToUInt64(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - throw new OverflowException(String.Format("{0} is outside the range of the UInt64 type.", - SByte.Parse(byteString, NumberStyles.HexNumber))); - else - return Convert.ToUInt64(Byte.Parse(byteString, NumberStyles.HexNumber)); - } + byteString = value; + } + get => byteString; + } + + // IConvertible implementations. + public TypeCode GetTypeCode() => TypeCode.Object; + + public bool ToBoolean(IFormatProvider provider) + { + if (signBit == SignBit.Zero) + return false; + else + return true; + } + + public byte ToByte(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + throw new OverflowException($"{Convert.ToSByte(byteString, 16)} is out of range of the Byte type."); + else + return byte.Parse(byteString, NumberStyles.HexNumber); + } + + public char ToChar(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + { + throw new OverflowException($"{Convert.ToSByte(byteString, 16)} is out of range of the Char type."); + } + else + { + byte byteValue = byte.Parse(this.byteString, NumberStyles.HexNumber); + return Convert.ToChar(byteValue); + } + } + + public DateTime ToDateTime(IFormatProvider provider) => throw new InvalidCastException("ByteString to DateTime conversion is not supported."); + + public decimal ToDecimal(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + { + sbyte byteValue = sbyte.Parse(byteString, NumberStyles.HexNumber); + return Convert.ToDecimal(byteValue); + } + else + { + byte byteValue = byte.Parse(byteString, NumberStyles.HexNumber); + return Convert.ToDecimal(byteValue); + } + } + + public double ToDouble(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + return Convert.ToDouble(sbyte.Parse(byteString, NumberStyles.HexNumber)); + else + return Convert.ToDouble(byte.Parse(byteString, NumberStyles.HexNumber)); + } + + public short ToInt16(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + return Convert.ToInt16(sbyte.Parse(byteString, NumberStyles.HexNumber)); + else + return Convert.ToInt16(byte.Parse(byteString, NumberStyles.HexNumber)); + } + + public int ToInt32(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + return Convert.ToInt32(sbyte.Parse(byteString, NumberStyles.HexNumber)); + else + return Convert.ToInt32(byte.Parse(byteString, NumberStyles.HexNumber)); + } + + public long ToInt64(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + return Convert.ToInt64(sbyte.Parse(byteString, NumberStyles.HexNumber)); + else + return Convert.ToInt64(byte.Parse(byteString, NumberStyles.HexNumber)); + } + + public sbyte ToSByte(IFormatProvider provider) + { + try + { + return sbyte.Parse(byteString, NumberStyles.HexNumber); + } + catch (OverflowException e) + { + throw new OverflowException($"{byte.Parse(byteString, NumberStyles.HexNumber)} is outside the range of the SByte type.", e); + } + } + + public float ToSingle(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + return Convert.ToSingle(sbyte.Parse(byteString, NumberStyles.HexNumber)); + else + return Convert.ToSingle(byte.Parse(byteString, NumberStyles.HexNumber)); + } + + public string ToString(IFormatProvider provider) => "0x" + this.byteString; + + public object ToType(Type conversionType, IFormatProvider provider) + { + switch (Type.GetTypeCode(conversionType)) + { + case TypeCode.Boolean: + return this.ToBoolean(null); + case TypeCode.Byte: + return this.ToByte(null); + case TypeCode.Char: + return this.ToChar(null); + case TypeCode.DateTime: + return this.ToDateTime(null); + case TypeCode.Decimal: + return this.ToDecimal(null); + case TypeCode.Double: + return this.ToDouble(null); + case TypeCode.Int16: + return this.ToInt16(null); + case TypeCode.Int32: + return this.ToInt32(null); + case TypeCode.Int64: + return this.ToInt64(null); + case TypeCode.Object: + if (typeof(ByteString).Equals(conversionType)) + return this; + else + throw new InvalidCastException($"Conversion to a {conversionType.Name} is not supported."); + case TypeCode.SByte: + return this.ToSByte(null); + case TypeCode.Single: + return this.ToSingle(null); + case TypeCode.String: + return this.ToString(null); + case TypeCode.UInt16: + return this.ToUInt16(null); + case TypeCode.UInt32: + return this.ToUInt32(null); + case TypeCode.UInt64: + return this.ToUInt64(null); + default: + throw new InvalidCastException($"Conversion to {conversionType.Name} is not supported."); + } + } + + public ushort ToUInt16(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + throw new OverflowException($"{sbyte.Parse(byteString, NumberStyles.HexNumber)} is outside the range of the UInt16 type."); + else + return Convert.ToUInt16(byte.Parse(byteString, NumberStyles.HexNumber)); + } + + public uint ToUInt32(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + throw new OverflowException($"{sbyte.Parse(byteString, NumberStyles.HexNumber)} is outside the range of the UInt32 type."); + else + return Convert.ToUInt32(byte.Parse(byteString, NumberStyles.HexNumber)); + } + + public ulong ToUInt64(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + throw new OverflowException($"{sbyte.Parse(byteString, NumberStyles.HexNumber)} is outside the range of the UInt64 type."); + else + return Convert.ToUInt64(byte.Parse(byteString, NumberStyles.HexNumber)); + } } // // public class Class1 { - public static void Main() - { - sbyte positiveByte = 120; - sbyte negativeByte = -101; - - ByteString positiveString = new ByteString(); - positiveString.Sign = (SignBit) Math.Sign(positiveByte); - positiveString.Value = positiveByte.ToString("X2"); - - ByteString negativeString = new ByteString(); - negativeString.Sign = (SignBit) Math.Sign(negativeByte); - negativeString.Value = negativeByte.ToString("X2"); - - try { - Console.WriteLine("'{0}' converts to {1}.", positiveString.Value, Convert.ToSByte(positiveString)); - } - catch (OverflowException) { - Console.WriteLine("0x{0} is outside the range of the Byte type.", positiveString.Value); - } - - try { - Console.WriteLine("'{0}' converts to {1}.", negativeString.Value, Convert.ToSByte(negativeString)); - } - catch (OverflowException) { - Console.WriteLine("0x{0} is outside the range of the Byte type.", negativeString.Value); - } - } + public static void Main() + { + sbyte positiveByte = 120; + sbyte negativeByte = -101; + + ByteString positiveString = new() + { + Sign = (SignBit)Math.Sign(positiveByte), + Value = positiveByte.ToString("X2") + }; + + ByteString negativeString = new() + { + Sign = (SignBit)Math.Sign(negativeByte), + Value = negativeByte.ToString("X2") + }; + + try + { + Console.WriteLine($"'{positiveString.Value}' converts to {Convert.ToSByte(positiveString)}."); + } + catch (OverflowException) + { + Console.WriteLine($"0x{positiveString.Value} is outside the range of the Byte type."); + } + + try + { + Console.WriteLine($"'{negativeString.Value}' converts to {Convert.ToSByte(negativeString)}."); + } + catch (OverflowException) + { + Console.WriteLine($"0x{negativeString.Value} is outside the range of the Byte type."); + } + } } // The example displays the following output: // '78' converts to 120. diff --git a/snippets/csharp/System/Convert/ToSByte/tosbyte3.cs b/snippets/csharp/System/Convert/ToSByte/tosbyte3.cs index 4c8af6a6215..d7872a4aed5 100644 --- a/snippets/csharp/System/Convert/ToSByte/tosbyte3.cs +++ b/snippets/csharp/System/Convert/ToSByte/tosbyte3.cs @@ -3,32 +3,35 @@ public class Example { - public static void Main() - { - int[] baseValues = { 2, 8, 16}; - string[] values = { "FF", "81", "03", "11", "8F", "01", "1C", "111", + public static void Main() + { + int[] baseValues = { 2, 8, 16 }; + string[] values = { "FF", "81", "03", "11", "8F", "01", "1C", "111", "123", "18A" }; - // Convert to each supported base. - foreach (int baseValue in baseValues) - { - Console.WriteLine("Converting strings in base {0}:", baseValue); - foreach (string value in values) - { - Console.Write(" '{0,-5} --> ", value + "'"); - try { - Console.WriteLine(Convert.ToSByte(value, baseValue)); + // Convert to each supported base. + foreach (int baseValue in baseValues) + { + Console.WriteLine($"Converting strings in base {baseValue}:"); + foreach (string value in values) + { + Console.Write($" '{value + "'",-5} --> "); + try + { + Console.WriteLine(Convert.ToSByte(value, baseValue)); + } + catch (FormatException) + { + Console.WriteLine("Bad Format"); + } + catch (OverflowException) + { + Console.WriteLine("Out of Range"); + } } - catch (FormatException) { - Console.WriteLine("Bad Format"); - } - catch (OverflowException) { - Console.WriteLine("Out of Range"); - } - } - Console.WriteLine(); - } - } + Console.WriteLine(); + } + } } // The example displays the following output: // Converting strings in base 2: diff --git a/snippets/csharp/System/Convert/ToSingle/tosingle1.cs b/snippets/csharp/System/Convert/ToSingle/tosingle1.cs index fe4324c76dd..16cb4c6189c 100644 --- a/snippets/csharp/System/Convert/ToSingle/tosingle1.cs +++ b/snippets/csharp/System/Convert/ToSingle/tosingle1.cs @@ -2,353 +2,333 @@ public class Example { - public static void Main() - { - ConvertBoolean(); - Console.WriteLine("-----"); - ConvertByte(); - Console.WriteLine("-----"); - ConvertDecimal(); - Console.WriteLine("-----"); - ConvertDouble(); - Console.WriteLine("-----"); - ConvertInt16(); - Console.WriteLine("-----"); - ConvertInt32(); - Console.WriteLine("-----"); - ConvertInt64(); - Console.WriteLine("-----"); - ConvertObject(); - Console.WriteLine("-----"); - ConvertSByte(); - Console.WriteLine("-----"); - ConvertString(); - Console.WriteLine("----"); - ConvertUInt16(); - Console.WriteLine("-----"); - ConvertUInt32(); - Console.WriteLine("------"); - ConvertUInt64(); - } + public static void Main() + { + ConvertBoolean(); + Console.WriteLine("-----"); + ConvertByte(); + Console.WriteLine("-----"); + ConvertDecimal(); + Console.WriteLine("-----"); + ConvertDouble(); + Console.WriteLine("-----"); + ConvertInt16(); + Console.WriteLine("-----"); + ConvertInt32(); + Console.WriteLine("-----"); + ConvertInt64(); + Console.WriteLine("-----"); + ConvertObject(); + Console.WriteLine("-----"); + ConvertSByte(); + Console.WriteLine("-----"); + ConvertString(); + Console.WriteLine("----"); + ConvertUInt16(); + Console.WriteLine("-----"); + ConvertUInt32(); + Console.WriteLine("------"); + ConvertUInt64(); + } - private static void ConvertBoolean() - { - // - bool[] flags = { true, false }; - float result; + private static void ConvertBoolean() + { + // + bool[] flags = { true, false }; + float result; - foreach (bool flag in flags) - { - result = Convert.ToSingle(flag); - Console.WriteLine("Converted {0} to {1}.", flag, result); - } - // The example displays the following output: - // Converted True to 1. - // Converted False to 0. - // - } + foreach (bool flag in flags) + { + result = Convert.ToSingle(flag); + Console.WriteLine($"Converted {flag} to {result}."); + } + // The example displays the following output: + // Converted True to 1. + // Converted False to 0. + // + } - private static void ConvertByte() - { - // - byte[] numbers = { Byte.MinValue, 10, 100, Byte.MaxValue }; - float result; + private static void ConvertByte() + { + // + byte[] numbers = { byte.MinValue, 10, 100, byte.MaxValue }; + float result; - foreach (byte number in numbers) - { - result = Convert.ToSingle(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the Byte value 0 to the Single value 0. - // Converted the Byte value 10 to the Single value 10. - // Converted the Byte value 100 to the Single value 100. - // Converted the Byte value 255 to the Single value 255. - // - } + foreach (byte number in numbers) + { + result = Convert.ToSingle(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the Byte value 0 to the Single value 0. + // Converted the Byte value 10 to the Single value 10. + // Converted the Byte value 100 to the Single value 100. + // Converted the Byte value 255 to the Single value 255. + // + } - private static void ConvertDecimal() - { - // - decimal[] values = { Decimal.MinValue, -1034.23m, -12m, 0m, 147m, - 199.55m, 9214.16m, Decimal.MaxValue }; - float result; + private static void ConvertDecimal() + { + // + decimal[] values = { decimal.MinValue, -1034.23m, -12m, 0m, 147m, + 199.55m, 9214.16m, decimal.MaxValue }; + float result; - foreach (var value in values) - { - result = Convert.ToSingle(value); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - value.GetType().Name, value, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the Decimal value '-79228162514264337593543950335' to the Single value -7.9228163E+28. - // Converted the Decimal value '-1034.23' to the Single value -1034.23. - // Converted the Decimal value '-12' to the Single value -12. - // Converted the Decimal value '0' to the Single value 0. - // Converted the Decimal value '147' to the Single value 147. - // Converted the Decimal value '199.55' to the Single value 199.55. - // Converted the Decimal value '9214.16' to the Single value 9214.16. - // Converted the Decimal value '79228162514264337593543950335' to the Single value 7.9228163E+28. - // - } + foreach (decimal value in values) + { + result = Convert.ToSingle(value); + Console.WriteLine($"Converted the {value.GetType().Name} value '{value}' to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the Decimal value '-79228162514264337593543950335' to the Single value -7.9228163E+28. + // Converted the Decimal value '-1034.23' to the Single value -1034.23. + // Converted the Decimal value '-12' to the Single value -12. + // Converted the Decimal value '0' to the Single value 0. + // Converted the Decimal value '147' to the Single value 147. + // Converted the Decimal value '199.55' to the Single value 199.55. + // Converted the Decimal value '9214.16' to the Single value 9214.16. + // Converted the Decimal value '79228162514264337593543950335' to the Single value 7.9228163E+28. + // + } - private static void ConvertDouble() - { - // - double[] values = { Double.MinValue, -1.38e10, -1023.299, -12.98, - 0, 9.113e-16, 103.919, 17834.191, Double.MaxValue }; - float result; + private static void ConvertDouble() + { + // + double[] values = { double.MinValue, -1.38e10, -1023.299, -12.98, + 0, 9.113e-16, 103.919, 17834.191, double.MaxValue }; + float result; - foreach (double value in values) - { - result = Convert.ToSingle(value); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - value.GetType().Name, value, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the Double value '-1.79769313486232E+308' to the Single value -Infinity. - // Converted the Double value '-13800000000' to the Single value -1.38E+10. - // Converted the Double value '-1023.299' to the Single value -1023.299. - // Converted the Double value '-12.98' to the Single value -12.98. - // Converted the Double value '0' to the Single value 0. - // Converted the Double value '9.113E-16' to the Single value 9.113E-16. - // Converted the Double value '103.919' to the Single value 103.919. - // Converted the Double value '17834.191' to the Single value 17834.19. - // Converted the Double value '1.79769313486232E+308' to the Single value Infinity. - // - } + foreach (double value in values) + { + result = Convert.ToSingle(value); + Console.WriteLine($"Converted the {value.GetType().Name} value '{value}' to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the Double value '-1.79769313486232E+308' to the Single value -Infinity. + // Converted the Double value '-13800000000' to the Single value -1.38E+10. + // Converted the Double value '-1023.299' to the Single value -1023.299. + // Converted the Double value '-12.98' to the Single value -12.98. + // Converted the Double value '0' to the Single value 0. + // Converted the Double value '9.113E-16' to the Single value 9.113E-16. + // Converted the Double value '103.919' to the Single value 103.919. + // Converted the Double value '17834.191' to the Single value 17834.19. + // Converted the Double value '1.79769313486232E+308' to the Single value Infinity. + // + } - private static void ConvertInt16() - { - // - short[] numbers = { Int16.MinValue, -1032, 0, 192, Int16.MaxValue }; - float result; + private static void ConvertInt16() + { + // + short[] numbers = { short.MinValue, -1032, 0, 192, short.MaxValue }; + float result; - foreach (short number in numbers) - { - result = Convert.ToSingle(number); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the Int16 value '-32768' to the Single value -32768. - // Converted the Int16 value '-1032' to the Single value -1032. - // Converted the Int16 value '0' to the Single value 0. - // Converted the Int16 value '192' to the Single value 192. - // Converted the Int16 value '32767' to the Single value 32767. - // - } + foreach (short number in numbers) + { + result = Convert.ToSingle(number); + Console.WriteLine($"Converted the {number.GetType().Name} value '{number}' to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the Int16 value '-32768' to the Single value -32768. + // Converted the Int16 value '-1032' to the Single value -1032. + // Converted the Int16 value '0' to the Single value 0. + // Converted the Int16 value '192' to the Single value 192. + // Converted the Int16 value '32767' to the Single value 32767. + // + } - private static void ConvertInt32() - { - // - int[] numbers = { Int32.MinValue, -1000, 0, 1000, Int32.MaxValue }; - float result; + private static void ConvertInt32() + { + // + int[] numbers = { int.MinValue, -1000, 0, 1000, int.MaxValue }; + float result; - foreach (int number in numbers) - { - result = Convert.ToSingle(number); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the Int32 value '-2147483648' to the Single value -2.147484E+09. - // Converted the Int32 value '-1000' to the Single value -1000. - // Converted the Int32 value '0' to the Single value 0. - // Converted the Int32 value '1000' to the Single value 1000. - // Converted the Int32 value '2147483647' to the Single value 2.147484E+09. - // - } + foreach (int number in numbers) + { + result = Convert.ToSingle(number); + Console.WriteLine($"Converted the {number.GetType().Name} value '{number}' to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the Int32 value '-2147483648' to the Single value -2.147484E+09. + // Converted the Int32 value '-1000' to the Single value -1000. + // Converted the Int32 value '0' to the Single value 0. + // Converted the Int32 value '1000' to the Single value 1000. + // Converted the Int32 value '2147483647' to the Single value 2.147484E+09. + // + } - private static void ConvertInt64() - { - // - long[] numbers = { Int64.MinValue, -903, 0, 172, Int64.MaxValue}; - float result; + private static void ConvertInt64() + { + // + long[] numbers = { long.MinValue, -903, 0, 172, long.MaxValue }; + float result; - foreach (long number in numbers) - { - result = Convert.ToSingle(number); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the Int64 value '-9223372036854775808' to the Single value -9.223372E+18. - // Converted the Int64 value '-903' to the Single value -903. - // Converted the Int64 value '0' to the Single value 0. - // Converted the Int64 value '172' to the Single value 172. - // Converted the Int64 value '9223372036854775807' to the Single value 9.223372E+18. - // - } + foreach (long number in numbers) + { + result = Convert.ToSingle(number); + Console.WriteLine($"Converted the {number.GetType().Name} value '{number}' to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the Int64 value '-9223372036854775808' to the Single value -9.223372E+18. + // Converted the Int64 value '-903' to the Single value -903. + // Converted the Int64 value '0' to the Single value 0. + // Converted the Int64 value '172' to the Single value 172. + // Converted the Int64 value '9223372036854775807' to the Single value 9.223372E+18. + // + } - private static void ConvertObject() - { - // - object[] values = { true, 'a', 123, 1.764e32, "9.78", "1e-02", + private static void ConvertObject() + { + // + object[] values = { true, 'a', 123, 1.764e32, "9.78", "1e-02", 1.67e03, "A100", "1,033.67", DateTime.Now, - Decimal.MaxValue }; - float result; + decimal.MaxValue }; + float result; - foreach (object value in values) - { - try { - result = Convert.ToSingle(value); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - value.GetType().Name, value, - result.GetType().Name, result); - } - catch (FormatException) { - Console.WriteLine("The {0} value {1} is not recognized as a valid Single value.", - value.GetType().Name, value); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the Single type.", - value.GetType().Name, value); - } - catch (InvalidCastException) { - Console.WriteLine("Conversion of the {0} value {1} to a Single is not supported.", - value.GetType().Name, value); - } - } - // The example displays the following output: - // Converted the Boolean value 'True' to the Single value 1. - // Conversion of the Char value a to a Single is not supported. - // Converted the Int32 value '123' to the Single value 123. - // Converted the Double value '1.764E+32' to the Single value 1.764E+32. - // Converted the String value '9.78' to the Single value 9.78. - // Converted the String value '1e-02' to the Single value 0.01. - // Converted the Double value '1670' to the Single value 1670. - // The String value A100 is not recognized as a valid Single value. - // Converted the String value '1,033.67' to the Single value 1033.67. - // Conversion of the DateTime value 11/7/2008 08:02:35 AM to a Single is not supported. - // Converted the Decimal value '79228162514264337593543950335' to the Single value 7.922816E+28. - // - } + foreach (object value in values) + { + try + { + result = Convert.ToSingle(value); + Console.WriteLine($"Converted the {value.GetType().Name} value '{value}' to the {result.GetType().Name} value {result}."); + } + catch (FormatException) + { + Console.WriteLine($"The {value.GetType().Name} value {value} is not recognized as a valid Single value."); + } + catch (OverflowException) + { + Console.WriteLine($"The {value.GetType().Name} value {value} is outside the range of the Single type."); + } + catch (InvalidCastException) + { + Console.WriteLine($"Conversion of the {value.GetType().Name} value {value} to a Single is not supported."); + } + } + // The example displays the following output: + // Converted the Boolean value 'True' to the Single value 1. + // Conversion of the Char value a to a Single is not supported. + // Converted the Int32 value '123' to the Single value 123. + // Converted the Double value '1.764E+32' to the Single value 1.764E+32. + // Converted the String value '9.78' to the Single value 9.78. + // Converted the String value '1e-02' to the Single value 0.01. + // Converted the Double value '1670' to the Single value 1670. + // The String value A100 is not recognized as a valid Single value. + // Converted the String value '1,033.67' to the Single value 1033.67. + // Conversion of the DateTime value 11/7/2008 08:02:35 AM to a Single is not supported. + // Converted the Decimal value '79228162514264337593543950335' to the Single value 7.922816E+28. + // + } - private static void ConvertSByte() - { - // - sbyte[] numbers = { SByte.MinValue, -23, 0, 17, SByte.MaxValue }; - float result; + private static void ConvertSByte() + { + // + sbyte[] numbers = { sbyte.MinValue, -23, 0, 17, sbyte.MaxValue }; + float result; - foreach (sbyte number in numbers) - { - result = Convert.ToSingle(number); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the SByte value '-128' to the Single value -128. - // Converted the SByte value '-23' to the Single value -23. - // Converted the SByte value '0' to the Single value 0. - // Converted the SByte value '17' to the Single value 17. - // Converted the SByte value '127' to the Single value 127. - // - } + foreach (sbyte number in numbers) + { + result = Convert.ToSingle(number); + Console.WriteLine($"Converted the {number.GetType().Name} value '{number}' to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the SByte value '-128' to the Single value -128. + // Converted the SByte value '-23' to the Single value -23. + // Converted the SByte value '0' to the Single value 0. + // Converted the SByte value '17' to the Single value 17. + // Converted the SByte value '127' to the Single value 127. + // + } - private static void ConvertString() - { - // - string[] values= { "-1,035.77219", "1AFF", "1e-35", "1.63f", + private static void ConvertString() + { + // + string[] values = { "-1,035.77219", "1AFF", "1e-35", "1.63f", "1,635,592,999,999,999,999,999,999", "-17.455", "190.34001", "1.29e325"}; - float result; + float result; - foreach (string value in values) - { - try { - result = Convert.ToSingle(value); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - value.GetType().Name, value, - result.GetType().Name, result); - } - catch (FormatException) { - Console.WriteLine("Unable to convert '{0}' to a Single.", value); - } - catch (OverflowException) { - Console.WriteLine("'{0}' is outside the range of a Single.", value); - } - } - // The example displays the following output: - // Converted the String value '-1,035.77219' to the Single value -1035.772. - // Unable to convert '1AFF' to a Single. - // Converted the String value '1e-35' to the Single value 1E-35. - // Unable to convert '1.63f' to a Single. - // Converted the String value '1,635,592,999,999,999,999,999,999' to the Single value 1.635593E+24. - // Converted the String value '-17.455' to the Single value -17.455. - // Converted the String value '190.34001' to the Single value 190.34. - // 1.29e325' is outside the range of a Single. - // - } + foreach (string value in values) + { + try + { + result = Convert.ToSingle(value); + Console.WriteLine($"Converted the {value.GetType().Name} value '{value}' to the {result.GetType().Name} value {result}."); + } + catch (FormatException) + { + Console.WriteLine($"Unable to convert '{value}' to a Single."); + } + catch (OverflowException) + { + Console.WriteLine($"'{value}' is outside the range of a Single."); + } + } + // The example displays the following output: + // Converted the String value '-1,035.77219' to the Single value -1035.772. + // Unable to convert '1AFF' to a Single. + // Converted the String value '1e-35' to the Single value 1E-35. + // Unable to convert '1.63f' to a Single. + // Converted the String value '1,635,592,999,999,999,999,999,999' to the Single value 1.635593E+24. + // Converted the String value '-17.455' to the Single value -17.455. + // Converted the String value '190.34001' to the Single value 190.34. + // 1.29e325' is outside the range of a Single. + // + } - private static void ConvertUInt16() - { - // - ushort[] numbers = { UInt16.MinValue, 121, 12345, UInt16.MaxValue }; - float result; + private static void ConvertUInt16() + { + // + ushort[] numbers = { ushort.MinValue, 121, 12345, ushort.MaxValue }; + float result; - foreach (ushort number in numbers) - { - result = Convert.ToSingle(number); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the UInt16 value '0' to the Single value 0. - // Converted the UInt16 value '121' to the Single value 121. - // Converted the UInt16 value '12345' to the Single value 12345. - // Converted the UInt16 value '65535' to the Single value 65535. - // - } + foreach (ushort number in numbers) + { + result = Convert.ToSingle(number); + Console.WriteLine($"Converted the {number.GetType().Name} value '{number}' to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the UInt16 value '0' to the Single value 0. + // Converted the UInt16 value '121' to the Single value 121. + // Converted the UInt16 value '12345' to the Single value 12345. + // Converted the UInt16 value '65535' to the Single value 65535. + // + } - private static void ConvertUInt32() - { - // - uint[] numbers = { UInt32.MinValue, 121, 12345, UInt32.MaxValue }; - float result; + private static void ConvertUInt32() + { + // + uint[] numbers = { uint.MinValue, 121, 12345, uint.MaxValue }; + float result; - foreach (uint number in numbers) - { - result = Convert.ToSingle(number); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the UInt32 value '0' to the Single value 0. - // Converted the UInt32 value '121' to the Single value 121. - // Converted the UInt32 value '12345' to the Single value 12345. - // Converted the UInt32 value '4294967295' to the Single value 4.294967E+09. - // - } + foreach (uint number in numbers) + { + result = Convert.ToSingle(number); + Console.WriteLine($"Converted the {number.GetType().Name} value '{number}' to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the UInt32 value '0' to the Single value 0. + // Converted the UInt32 value '121' to the Single value 121. + // Converted the UInt32 value '12345' to the Single value 12345. + // Converted the UInt32 value '4294967295' to the Single value 4.294967E+09. + // + } - private static void ConvertUInt64() - { - // - ulong[] numbers = { UInt64.MinValue, 121, 12345, UInt64.MaxValue }; - float result; + private static void ConvertUInt64() + { + // + ulong[] numbers = { ulong.MinValue, 121, 12345, ulong.MaxValue }; + float result; - foreach (ulong number in numbers) - { - result = Convert.ToSingle(number); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the UInt64 value '0' to the Single value 0. - // Converted the UInt64 value '121' to the Single value 121. - // Converted the UInt64 value '12345' to the Single value 12345. - // Converted the UInt64 value '18446744073709551615' to the Single value 1.844674E+19. - // - } + foreach (ulong number in numbers) + { + result = Convert.ToSingle(number); + Console.WriteLine($"Converted the {number.GetType().Name} value '{number}' to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the UInt64 value '0' to the Single value 0. + // Converted the UInt64 value '121' to the Single value 121. + // Converted the UInt64 value '12345' to the Single value 12345. + // Converted the UInt64 value '18446744073709551615' to the Single value 1.844674E+19. + // + } } diff --git a/snippets/csharp/System/Convert/ToSingle/tosingle2.cs b/snippets/csharp/System/Convert/ToSingle/tosingle2.cs index 5b647ed31fe..8e453d4d5f5 100644 --- a/snippets/csharp/System/Convert/ToSingle/tosingle2.cs +++ b/snippets/csharp/System/Convert/ToSingle/tosingle2.cs @@ -1,212 +1,167 @@ // using System; -using System.Globalization; + public class Temperature : IConvertible { - private float m_Temp; - - public Temperature(float temperature) - { - this.m_Temp = temperature; - } - - public float Celsius - { - get { return this.m_Temp; } - } - - public float Kelvin - { - get { return this.m_Temp + 273.15f; } - } - - public float Fahrenheit - { - get { return (float) Math.Round(this.m_Temp * 9 / 5 + 32, 2); } - } - - public override string ToString() - { - return m_Temp.ToString("N2") + " °C"; - } - - // IConvertible implementations. - public TypeCode GetTypeCode() - { - return TypeCode.Object; - } - - public bool ToBoolean(IFormatProvider provider) - { - if (m_Temp == 0) - return false; - else - return true; - } - - public byte ToByte(IFormatProvider provider) - { - if (m_Temp < Byte.MinValue || m_Temp > Byte.MaxValue) - throw new OverflowException(String.Format("{0} is out of range of the Byte type.", - this.m_Temp)); - else - return Convert.ToByte(this.m_Temp); - } - - public char ToChar(IFormatProvider provider) - { - throw new InvalidCastException("Temperature to Char conversion is not supported."); - } - - public DateTime ToDateTime(IFormatProvider provider) - { - throw new InvalidCastException("Temperature to DateTime conversion is not supported."); - } - - public decimal ToDecimal(IFormatProvider provider) - { - return Convert.ToDecimal(this.m_Temp); - } - - public double ToDouble(IFormatProvider provider) - { - return Convert.ToDouble(this.m_Temp); - } - - public short ToInt16(IFormatProvider provider) - { - if (this.m_Temp < Int16.MinValue || this.m_Temp > Int16.MaxValue) - throw new OverflowException(String.Format("{0} is out of range of the Int16 type.", - this.m_Temp)); - else - return Convert.ToInt16(this.m_Temp); - } - - public int ToInt32(IFormatProvider provider) - { - if (this.m_Temp < Int32.MinValue || this.m_Temp > Int32.MaxValue) - throw new OverflowException(String.Format("{0} is out of range of the Int32 type.", - this.m_Temp)); - else - return Convert.ToInt32(this.m_Temp); - } - - public long ToInt64(IFormatProvider provider) - { - if (this.m_Temp < Int64.MinValue || this.m_Temp > Int64.MaxValue) - throw new OverflowException(String.Format("{0} is out of range of the Int64 type.", - this.m_Temp)); - else - return Convert.ToInt64(this.m_Temp); - } - - public sbyte ToSByte(IFormatProvider provider) - { - if (this.m_Temp < SByte.MinValue || this.m_Temp > SByte.MaxValue) - throw new OverflowException(String.Format("{0} is out of range of the SByte type.", - this.m_Temp)); - else - return Convert.ToSByte(this.m_Temp); - } - - public float ToSingle(IFormatProvider provider) - { - return this.m_Temp; - } - - public string ToString(IFormatProvider provider) - { - return m_Temp.ToString("N2", provider) + " °C"; - } - - public object ToType(Type conversionType, IFormatProvider provider) - { - switch (Type.GetTypeCode(conversionType)) - { - case TypeCode.Boolean: - return this.ToBoolean(null); - case TypeCode.Byte: - return this.ToByte(null); - case TypeCode.Char: - return this.ToChar(null); - case TypeCode.DateTime: - return this.ToDateTime(null); - case TypeCode.Decimal: - return this.ToDecimal(null); - case TypeCode.Double: - return this.ToDouble(null); - case TypeCode.Int16: - return this.ToInt16(null); - case TypeCode.Int32: - return this.ToInt32(null); - case TypeCode.Int64: - return this.ToInt64(null); - case TypeCode.Object: - if (typeof(Temperature).Equals(conversionType)) - return this; - else - throw new InvalidCastException(String.Format("Conversion to a {0} is not supported.", - conversionType.Name)); - case TypeCode.SByte: - return this.ToSByte(null); - case TypeCode.Single: - return this.ToSingle(null); - case TypeCode.String: - return this.ToString(provider); - case TypeCode.UInt16: - return this.ToUInt16(null); - case TypeCode.UInt32: - return this.ToUInt32(null); - case TypeCode.UInt64: - return this.ToUInt64(null); - default: - throw new InvalidCastException(String.Format("Conversion to {0} is not supported.", conversionType.Name)); - } - } - - public ushort ToUInt16(IFormatProvider provider) - { - if (this.m_Temp < UInt16.MinValue || this.m_Temp > UInt16.MaxValue) - throw new OverflowException(String.Format("{0} is out of range of the UInt16 type.", - this.m_Temp)); - else - return Convert.ToUInt16(this.m_Temp); - } - - public uint ToUInt32(IFormatProvider provider) - { - if (this.m_Temp < UInt32.MinValue || this.m_Temp > UInt32.MaxValue) - throw new OverflowException(String.Format("{0} is out of range of the UInt32 type.", - this.m_Temp)); - else - return Convert.ToUInt32(this.m_Temp); - } - - public ulong ToUInt64(IFormatProvider provider) - { - if (this.m_Temp < UInt64.MinValue || this.m_Temp > UInt64.MaxValue) - throw new OverflowException(String.Format("{0} is out of range of the UInt64 type.", - this.m_Temp)); - else - return Convert.ToUInt64(this.m_Temp); - } + private float m_Temp; + + public Temperature(float temperature) => this.m_Temp = temperature; + + public float Celsius => this.m_Temp; + + public float Kelvin => this.m_Temp + 273.15f; + + public float Fahrenheit => (float)Math.Round(this.m_Temp * 9 / 5 + 32, 2); + + public override string ToString() => m_Temp.ToString("N2") + " °C"; + + // IConvertible implementations. + public TypeCode GetTypeCode() => TypeCode.Object; + + public bool ToBoolean(IFormatProvider provider) + { + if (m_Temp == 0) + return false; + else + return true; + } + + public byte ToByte(IFormatProvider provider) + { + if (m_Temp < byte.MinValue || m_Temp > byte.MaxValue) + throw new OverflowException($"{this.m_Temp} is out of range of the Byte type."); + else + return Convert.ToByte(this.m_Temp); + } + + public char ToChar(IFormatProvider provider) => throw new InvalidCastException("Temperature to Char conversion is not supported."); + + public DateTime ToDateTime(IFormatProvider provider) => throw new InvalidCastException("Temperature to DateTime conversion is not supported."); + + public decimal ToDecimal(IFormatProvider provider) => Convert.ToDecimal(this.m_Temp); + + public double ToDouble(IFormatProvider provider) => Convert.ToDouble(this.m_Temp); + + public short ToInt16(IFormatProvider provider) + { + if (this.m_Temp < short.MinValue || this.m_Temp > short.MaxValue) + throw new OverflowException($"{this.m_Temp} is out of range of the Int16 type."); + else + return Convert.ToInt16(this.m_Temp); + } + + public int ToInt32(IFormatProvider provider) + { + if (this.m_Temp < int.MinValue || this.m_Temp > int.MaxValue) + throw new OverflowException($"{this.m_Temp} is out of range of the Int32 type."); + else + return Convert.ToInt32(this.m_Temp); + } + + public long ToInt64(IFormatProvider provider) + { + if (this.m_Temp < long.MinValue || this.m_Temp > long.MaxValue) + throw new OverflowException($"{this.m_Temp} is out of range of the Int64 type."); + else + return Convert.ToInt64(this.m_Temp); + } + + public sbyte ToSByte(IFormatProvider provider) + { + if (this.m_Temp < sbyte.MinValue || this.m_Temp > sbyte.MaxValue) + throw new OverflowException($"{this.m_Temp} is out of range of the SByte type."); + else + return Convert.ToSByte(this.m_Temp); + } + + public float ToSingle(IFormatProvider provider) => this.m_Temp; + + public string ToString(IFormatProvider provider) => m_Temp.ToString("N2", provider) + " °C"; + + public object ToType(Type conversionType, IFormatProvider provider) + { + switch (Type.GetTypeCode(conversionType)) + { + case TypeCode.Boolean: + return this.ToBoolean(null); + case TypeCode.Byte: + return this.ToByte(null); + case TypeCode.Char: + return this.ToChar(null); + case TypeCode.DateTime: + return this.ToDateTime(null); + case TypeCode.Decimal: + return this.ToDecimal(null); + case TypeCode.Double: + return this.ToDouble(null); + case TypeCode.Int16: + return this.ToInt16(null); + case TypeCode.Int32: + return this.ToInt32(null); + case TypeCode.Int64: + return this.ToInt64(null); + case TypeCode.Object: + if (typeof(Temperature).Equals(conversionType)) + return this; + else + throw new InvalidCastException($"Conversion to a {conversionType.Name} is not supported."); + case TypeCode.SByte: + return this.ToSByte(null); + case TypeCode.Single: + return this.ToSingle(null); + case TypeCode.String: + return this.ToString(provider); + case TypeCode.UInt16: + return this.ToUInt16(null); + case TypeCode.UInt32: + return this.ToUInt32(null); + case TypeCode.UInt64: + return this.ToUInt64(null); + default: + throw new InvalidCastException($"Conversion to {conversionType.Name} is not supported."); + } + } + + public ushort ToUInt16(IFormatProvider provider) + { + if (this.m_Temp < ushort.MinValue || this.m_Temp > ushort.MaxValue) + throw new OverflowException($"{this.m_Temp} is out of range of the UInt16 type."); + else + return Convert.ToUInt16(this.m_Temp); + } + + public uint ToUInt32(IFormatProvider provider) + { + if (this.m_Temp < uint.MinValue || this.m_Temp > uint.MaxValue) + throw new OverflowException($"{this.m_Temp} is out of range of the UInt32 type."); + else + return Convert.ToUInt32(this.m_Temp); + } + + public ulong ToUInt64(IFormatProvider provider) + { + if (this.m_Temp < ulong.MinValue || this.m_Temp > ulong.MaxValue) + throw new OverflowException($"{this.m_Temp} is out of range of the UInt64 type."); + else + return Convert.ToUInt64(this.m_Temp); + } } // // public class Example { - public static void Main() - { - Temperature cold = new Temperature(-40); - Temperature freezing = new Temperature(0); - Temperature boiling = new Temperature(100); - - Console.WriteLine(Convert.ToInt32(cold, null)); - Console.WriteLine(Convert.ToInt32(freezing, null)); - Console.WriteLine(Convert.ToDouble(boiling, null)); - } + public static void Main() + { + Temperature cold = new(-40); + Temperature freezing = new(0); + Temperature boiling = new(100); + + Console.WriteLine(Convert.ToInt32(cold, null)); + Console.WriteLine(Convert.ToInt32(freezing, null)); + Console.WriteLine(Convert.ToDouble(boiling, null)); + } } // The example dosplays the following output: // -40 diff --git a/snippets/csharp/System/Convert/ToSingle/tosingle3.cs b/snippets/csharp/System/Convert/ToSingle/tosingle3.cs index c317d98b533..aa004b58e51 100644 --- a/snippets/csharp/System/Convert/ToSingle/tosingle3.cs +++ b/snippets/csharp/System/Convert/ToSingle/tosingle3.cs @@ -4,35 +4,37 @@ public class Example { - public static void Main() - { - string[] values = { "123456789", "12345.6789", "12 345,6789", + public static void Main() + { + string[] values = { "123456789", "12345.6789", "12 345,6789", "123,456.789", "123 456,789", "123,456,789.0123", "123 456 789,0123", "1.235e12", "1.03221e-05", - Double.MaxValue.ToString() }; - CultureInfo[] cultures = { new CultureInfo("en-US"), + double.MaxValue.ToString() }; + CultureInfo[] cultures = { new CultureInfo("en-US"), new CultureInfo("fr-FR") }; - foreach (CultureInfo culture in cultures) - { - Console.WriteLine("String -> Single Conversion Using the {0} Culture", - culture.Name); - foreach (string value in values) - { - Console.Write("{0,22} -> ", value); - try { - Console.WriteLine(Convert.ToSingle(value, culture)); + foreach (CultureInfo culture in cultures) + { + Console.WriteLine($"String -> Single Conversion Using the {culture.Name} Culture"); + foreach (string value in values) + { + Console.Write($"{value,22} -> "); + try + { + Console.WriteLine(Convert.ToSingle(value, culture)); + } + catch (FormatException) + { + Console.WriteLine("FormatException"); + } + catch (OverflowException) + { + Console.WriteLine("OverflowException"); + } } - catch (FormatException) { - Console.WriteLine("FormatException"); - } - catch (OverflowException) { - Console.WriteLine("OverflowException"); - } - } - Console.WriteLine(); - } - } + Console.WriteLine(); + } + } } // The example displays the following output: // String -> Single Conversion Using the en-US Culture diff --git a/snippets/csharp/System/Convert/ToString/ToString.Byte1.cs b/snippets/csharp/System/Convert/ToString/ToString.Byte1.cs index 7c80b25cd2c..6913ffc7b0a 100644 --- a/snippets/csharp/System/Convert/ToString/ToString.Byte1.cs +++ b/snippets/csharp/System/Convert/ToString/ToString.Byte1.cs @@ -3,15 +3,13 @@ public class Example { - public static void Main() - { - byte[] values = { Byte.MinValue, 12, 100, 179, Byte.MaxValue } ; + public static void Main() + { + byte[] values = { byte.MinValue, 12, 100, 179, byte.MaxValue }; - foreach (var value in values) - Console.WriteLine("{0,3} ({1}) --> {2}", value, - value.GetType().Name, - Convert.ToString(value)); - } + foreach (byte value in values) + Console.WriteLine($"{value,3} ({value.GetType().Name}) --> {Convert.ToString(value)}"); + } } // The example displays the following output: // 0 (Byte) --> 0 diff --git a/snippets/csharp/System/Convert/ToString/ToString_Bool1.cs b/snippets/csharp/System/Convert/ToString/ToString_Bool1.cs index a395a89a7fc..cc8b0d75398 100644 --- a/snippets/csharp/System/Convert/ToString/ToString_Bool1.cs +++ b/snippets/csharp/System/Convert/ToString/ToString_Bool1.cs @@ -2,21 +2,21 @@ public class Class1 { - public static void Main() - { - // - bool falseFlag = false; - bool trueFlag = true; + public static void Main() + { + // + bool falseFlag = false; + bool trueFlag = true; - Console.WriteLine(Convert.ToString(falseFlag)); - Console.WriteLine(Convert.ToString(falseFlag).Equals(Boolean.FalseString)); - Console.WriteLine(Convert.ToString(trueFlag)); - Console.WriteLine(Convert.ToString(trueFlag).Equals(Boolean.TrueString)); - // The example displays the following output: - // False - // True - // True - // True - // - } + Console.WriteLine(Convert.ToString(falseFlag)); + Console.WriteLine(Convert.ToString(falseFlag).Equals(bool.FalseString)); + Console.WriteLine(Convert.ToString(trueFlag)); + Console.WriteLine(Convert.ToString(trueFlag).Equals(bool.TrueString)); + // The example displays the following output: + // False + // True + // True + // True + // + } } diff --git a/snippets/csharp/System/Convert/ToString/nonnumeric.cs b/snippets/csharp/System/Convert/ToString/nonnumeric.cs index 79ffbccd5fa..139af2cc241 100644 --- a/snippets/csharp/System/Convert/ToString/nonnumeric.cs +++ b/snippets/csharp/System/Convert/ToString/nonnumeric.cs @@ -1,7 +1,7 @@ // // Example of Convert.ToString( non-numeric types, IFormatProvider ). using System; -using System.Globalization; + // An instance of this class can be passed to methods that require // an IFormatProvider. @@ -9,66 +9,66 @@ public class DummyProvider : IFormatProvider { // Normally, GetFormat returns an object of the requested type // (usually itself) if it is able; otherwise, it returns Nothing. - public object GetFormat( Type argType ) + public object GetFormat(Type argType) { // Here, the type of argType is displayed, and GetFormat // always returns Nothing. - Console.Write( "{0,-40}", argType.ToString( ) ); + Console.Write($"{argType.ToString(),-40}"); return null; } } class ConvertNonNumericProviderDemo { - static void Main( ) + static void Main() { // Create an instance of the IFormatProvider. - DummyProvider provider = new DummyProvider( ); + DummyProvider provider = new(); string converted; // Convert these values using DummyProvider. - int Int32A = -252645135; - double DoubleA = 61680.3855; - object ObjDouble = (object)( -98765.4321 ); - DateTime DayTimeA = new DateTime( 2001, 9, 11, 13, 45, 0 ); + int Int32A = -252645135; + double DoubleA = 61680.3855; + object ObjDouble = (object)(-98765.4321); + DateTime DayTimeA = new(2001, 9, 11, 13, 45, 0); - bool BoolA = true; - string StringA = "Qwerty"; - char CharA = '$'; - TimeSpan TSpanA = new TimeSpan( 0, 18, 0 ); - object ObjOther = (object)provider; + bool BoolA = true; + string StringA = "Qwerty"; + char CharA = '$'; + TimeSpan TSpanA = new(0, 18, 0); + object ObjOther = (object)provider; - Console.WriteLine( "This example of " + + Console.WriteLine("This example of " + "Convert.ToString( non-numeric, IFormatProvider ) \n" + "generates the following output. The provider type, " + - "argument type, \nand argument value are displayed." ); - Console.WriteLine( "\nNote: The IFormatProvider object is " + + "argument type, \nand argument value are displayed."); + Console.WriteLine("\nNote: The IFormatProvider object is " + "not called for Boolean, String, \nChar, TimeSpan, " + - "and non-numeric Object." ); + "and non-numeric Object."); // The format provider is called for these conversions. - Console.WriteLine( ); - converted = Convert.ToString( Int32A, provider ); - Console.WriteLine( "int {0}", converted ); - converted = Convert.ToString( DoubleA, provider ); - Console.WriteLine( "double {0}", converted ); - converted = Convert.ToString( ObjDouble, provider ); - Console.WriteLine( "object {0}", converted ); - converted = Convert.ToString( DayTimeA, provider ); - Console.WriteLine( "DateTime {0}", converted ); + Console.WriteLine(); + converted = Convert.ToString(Int32A, provider); + Console.WriteLine($"int {converted}"); + converted = Convert.ToString(DoubleA, provider); + Console.WriteLine($"double {converted}"); + converted = Convert.ToString(ObjDouble, provider); + Console.WriteLine($"object {converted}"); + converted = Convert.ToString(DayTimeA, provider); + Console.WriteLine($"DateTime {converted}"); // The format provider is not called for these conversions. - Console.WriteLine( ); - converted = Convert.ToString( BoolA, provider ); - Console.WriteLine( "bool {0}", converted ); - converted = Convert.ToString( StringA, provider ); - Console.WriteLine( "string {0}", converted ); - converted = Convert.ToString( CharA, provider ); - Console.WriteLine( "char {0}", converted ); - converted = Convert.ToString( TSpanA, provider ); - Console.WriteLine( "TimeSpan {0}", converted ); - converted = Convert.ToString( ObjOther, provider ); - Console.WriteLine( "object {0}", converted ); + Console.WriteLine(); + converted = Convert.ToString(BoolA, provider); + Console.WriteLine($"bool {converted}"); + converted = Convert.ToString(StringA, provider); + Console.WriteLine($"string {converted}"); + converted = Convert.ToString(CharA, provider); + Console.WriteLine($"char {converted}"); + converted = Convert.ToString(TSpanA, provider); + Console.WriteLine($"TimeSpan {converted}"); + converted = Convert.ToString(ObjOther, provider); + Console.WriteLine($"object {converted}"); } } diff --git a/snippets/csharp/System/Convert/ToString/tostring1.cs b/snippets/csharp/System/Convert/ToString/tostring1.cs index 03931ae8033..44b9a4f96f8 100644 --- a/snippets/csharp/System/Convert/ToString/tostring1.cs +++ b/snippets/csharp/System/Convert/ToString/tostring1.cs @@ -2,205 +2,189 @@ public class Example { - public static void Main() - { - ConvertDateTime(); - Console.WriteLine("-----"); - ConvertInt16(); - Console.WriteLine("-----"); - ConvertObject(); - Console.WriteLine("-----"); - ConvertSByte(); - Console.WriteLine("-----"); - ConvertSingle(); - Console.WriteLine("-----"); - ConvertUInt16(); - Console.WriteLine("-----"); - ConvertUInt32(); - Console.WriteLine("-----"); - ConvertUInt64(); - } - - private static void ConvertDateTime() - { - // - DateTime[] dates = { new DateTime(2009, 7, 14), + public static void Main() + { + ConvertDateTime(); + Console.WriteLine("-----"); + ConvertInt16(); + Console.WriteLine("-----"); + ConvertObject(); + Console.WriteLine("-----"); + ConvertSByte(); + Console.WriteLine("-----"); + ConvertSingle(); + Console.WriteLine("-----"); + ConvertUInt16(); + Console.WriteLine("-----"); + ConvertUInt32(); + Console.WriteLine("-----"); + ConvertUInt64(); + } + + private static void ConvertDateTime() + { + // + DateTime[] dates = { new DateTime(2009, 7, 14), new DateTime(1, 1, 1, 18, 32, 0), new DateTime(2009, 2, 12, 7, 16, 0) }; - string result; - - foreach (DateTime dateValue in dates) - { - result = Convert.ToString(dateValue); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - dateValue.GetType().Name, dateValue, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the DateTime value 7/14/2009 12:00:00 AM to a String value 7/14/2009 12:00:00 AM. - // Converted the DateTime value 1/1/0001 06:32:00 PM to a String value 1/1/0001 06:32:00 PM. - // Converted the DateTime value 2/12/2009 07:16:00 AM to a String value 2/12/2009 07:16:00 AM. - // - } - - private static void ConvertInt16() - { - // - short[] numbers = { Int16.MinValue, -138, 0, 19, Int16.MaxValue }; - string result; - - foreach (short number in numbers) - { - result = Convert.ToString(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the Int16 value -32768 to the String value -32768. - // Converted the Int16 value -138 to the String value -138. - // Converted the Int16 value 0 to the String value 0. - // Converted the Int16 value 19 to the String value 19. - // Converted the Int16 value 32767 to the String value 32767. - // - } - - private static void ConvertObject() - { - // - object[] values = { false, 12.63m, new DateTime(2009, 6, 1, 6, 32, 15), 16.09e-12, - 'Z', 15.15322, SByte.MinValue, Int32.MaxValue }; - string result; - - foreach (object value in values) - { - result = Convert.ToString(value); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - value.GetType().Name, value, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the Boolean value False to the String value False. - // Converted the Decimal value 12.63 to the String value 12.63. - // Converted the DateTime value 6/1/2009 06:32:15 AM to the String value 6/1/2009 06:32:15 AM. - // Converted the Double value 1.609E-11 to the String value 1.609E-11. - // Converted the Char value Z to the String value Z. - // Converted the Double value 15.15322 to the String value 15.15322. - // Converted the SByte value -128 to the String value -128. - // Converted the Int32 value 2147483647 to the String value 2147483647. - // - } - - private static void ConvertSByte() - { - // - sbyte[] numbers = { SByte.MinValue, -12, 0, 16, SByte.MaxValue }; - string result; - - foreach (sbyte number in numbers) - { - result = Convert.ToString(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the SByte value -128 to the String value -128. - // Converted the SByte value -12 to the String value -12. - // Converted the SByte value 0 to the String value 0. - // Converted the SByte value 16 to the String value 16. - // Converted the SByte value 127 to the String value 127. - // - } - - private static void ConvertSingle() - { - // - float[] numbers = { Single.MinValue, -1011.351f, -17.45f, -3e-16f, - 0f, 4.56e-12f, 16.0001f, 10345.1221f, Single.MaxValue }; - string result; - - foreach (float number in numbers) - { - result = Convert.ToString(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the Single value -3.402823E+38 to the String value -3.402823E+38. - // Converted the Single value -1011.351 to the String value -1011.351. - // Converted the Single value -17.45 to the String value -17.45. - // Converted the Single value -3E-16 to the String value -3E-16. - // Converted the Single value 0 to the String value 0. - // Converted the Single value 4.56E-12 to the String value 4.56E-12. - // Converted the Single value 16.0001 to the String value 16.0001. - // Converted the Single value 10345.12 to the String value 10345.12. - // Converted the Single value 3.402823E+38 to the String value 3.402823E+38. - // - } - - private static void ConvertUInt16() - { - // - ushort[] numbers = { UInt16.MinValue, 103, 1045, UInt16.MaxValue }; - string result; - - foreach (ushort number in numbers) - { - result = Convert.ToString(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the UInt16 value 0 to the String value 0. - // Converted the UInt16 value 103 to the String value 103. - // Converted the UInt16 value 1045 to the String value 1045. - // Converted the UInt16 value 65535 to the String value 65535. - // - } - - private static void ConvertUInt32() - { - // - uint[] numbers = { UInt32.MinValue, 103, 1045, 119543, UInt32.MaxValue }; - string result; - - foreach (uint number in numbers) - { - result = Convert.ToString(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the UInt32 value 0 to the String value 0. - // Converted the UInt32 value 103 to the String value 103. - // Converted the UInt32 value 1045 to the String value 1045. - // Converted the UInt32 value 119543 to the String value 119543. - // Converted the UInt32 value 4294967295 to the String value 4294967295. - // - } - - private static void ConvertUInt64() - { - // - ulong[] numbers = { UInt64.MinValue, 1031, 189045, UInt64.MaxValue }; - string result; - - foreach (ulong number in numbers) - { - result = Convert.ToString(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the UInt64 value 0 to the String value 0. - // Converted the UInt64 value 1031 to the String value 1031. - // Converted the UInt64 value 189045 to the String value 189045. - // Converted the UInt64 value 18446744073709551615 to the String value 18446744073709551615. - // - } + string result; + + foreach (DateTime dateValue in dates) + { + result = Convert.ToString(dateValue); + Console.WriteLine($"Converted the {dateValue.GetType().Name} value {dateValue} to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the DateTime value 7/14/2009 12:00:00 AM to a String value 7/14/2009 12:00:00 AM. + // Converted the DateTime value 1/1/0001 06:32:00 PM to a String value 1/1/0001 06:32:00 PM. + // Converted the DateTime value 2/12/2009 07:16:00 AM to a String value 2/12/2009 07:16:00 AM. + // + } + + private static void ConvertInt16() + { + // + short[] numbers = { short.MinValue, -138, 0, 19, short.MaxValue }; + string result; + + foreach (short number in numbers) + { + result = Convert.ToString(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the Int16 value -32768 to the String value -32768. + // Converted the Int16 value -138 to the String value -138. + // Converted the Int16 value 0 to the String value 0. + // Converted the Int16 value 19 to the String value 19. + // Converted the Int16 value 32767 to the String value 32767. + // + } + + private static void ConvertObject() + { + // + object[] values = { false, 12.63m, new DateTime(2009, 6, 1, 6, 32, 15), 16.09e-12, + 'Z', 15.15322, sbyte.MinValue, int.MaxValue }; + string result; + + foreach (object value in values) + { + result = Convert.ToString(value); + Console.WriteLine($"Converted the {value.GetType().Name} value {value} to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the Boolean value False to the String value False. + // Converted the Decimal value 12.63 to the String value 12.63. + // Converted the DateTime value 6/1/2009 06:32:15 AM to the String value 6/1/2009 06:32:15 AM. + // Converted the Double value 1.609E-11 to the String value 1.609E-11. + // Converted the Char value Z to the String value Z. + // Converted the Double value 15.15322 to the String value 15.15322. + // Converted the SByte value -128 to the String value -128. + // Converted the Int32 value 2147483647 to the String value 2147483647. + // + } + + private static void ConvertSByte() + { + // + sbyte[] numbers = { sbyte.MinValue, -12, 0, 16, sbyte.MaxValue }; + string result; + + foreach (sbyte number in numbers) + { + result = Convert.ToString(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the SByte value -128 to the String value -128. + // Converted the SByte value -12 to the String value -12. + // Converted the SByte value 0 to the String value 0. + // Converted the SByte value 16 to the String value 16. + // Converted the SByte value 127 to the String value 127. + // + } + + private static void ConvertSingle() + { + // + float[] numbers = { float.MinValue, -1011.351f, -17.45f, -3e-16f, + 0f, 4.56e-12f, 16.0001f, 10345.1221f, float.MaxValue }; + string result; + + foreach (float number in numbers) + { + result = Convert.ToString(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the Single value -3.402823E+38 to the String value -3.402823E+38. + // Converted the Single value -1011.351 to the String value -1011.351. + // Converted the Single value -17.45 to the String value -17.45. + // Converted the Single value -3E-16 to the String value -3E-16. + // Converted the Single value 0 to the String value 0. + // Converted the Single value 4.56E-12 to the String value 4.56E-12. + // Converted the Single value 16.0001 to the String value 16.0001. + // Converted the Single value 10345.12 to the String value 10345.12. + // Converted the Single value 3.402823E+38 to the String value 3.402823E+38. + // + } + + private static void ConvertUInt16() + { + // + ushort[] numbers = { ushort.MinValue, 103, 1045, ushort.MaxValue }; + string result; + + foreach (ushort number in numbers) + { + result = Convert.ToString(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the UInt16 value 0 to the String value 0. + // Converted the UInt16 value 103 to the String value 103. + // Converted the UInt16 value 1045 to the String value 1045. + // Converted the UInt16 value 65535 to the String value 65535. + // + } + + private static void ConvertUInt32() + { + // + uint[] numbers = { uint.MinValue, 103, 1045, 119543, uint.MaxValue }; + string result; + + foreach (uint number in numbers) + { + result = Convert.ToString(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the UInt32 value 0 to the String value 0. + // Converted the UInt32 value 103 to the String value 103. + // Converted the UInt32 value 1045 to the String value 1045. + // Converted the UInt32 value 119543 to the String value 119543. + // Converted the UInt32 value 4294967295 to the String value 4294967295. + // + } + + private static void ConvertUInt64() + { + // + ulong[] numbers = { ulong.MinValue, 1031, 189045, ulong.MaxValue }; + string result; + + foreach (ulong number in numbers) + { + result = Convert.ToString(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the UInt64 value 0 to the String value 0. + // Converted the UInt64 value 1031 to the String value 1031. + // Converted the UInt64 value 189045 to the String value 189045. + // Converted the UInt64 value 18446744073709551615 to the String value 18446744073709551615. + // + } } diff --git a/snippets/csharp/System/Convert/ToString/tostring2.cs b/snippets/csharp/System/Convert/ToString/tostring2.cs index bd38578499e..b4802ddb317 100644 --- a/snippets/csharp/System/Convert/ToString/tostring2.cs +++ b/snippets/csharp/System/Convert/ToString/tostring2.cs @@ -2,204 +2,200 @@ public class Example { - public static void Main() - { - ConvertByte(); - Console.WriteLine("-----"); - ConvertShort(); - Console.WriteLine("-----"); - ConvertInt(); - Console.WriteLine("-----"); - ConvertLong(); - } + public static void Main() + { + ConvertByte(); + Console.WriteLine("-----"); + ConvertShort(); + Console.WriteLine("-----"); + ConvertInt(); + Console.WriteLine("-----"); + ConvertLong(); + } - private static void ConvertByte() - { - // - int[] bases = { 2, 8, 10, 16}; - byte[] numbers = { Byte.MinValue, 12, 103, Byte.MaxValue}; + private static void ConvertByte() + { + // + int[] bases = { 2, 8, 10, 16 }; + byte[] numbers = { byte.MinValue, 12, 103, byte.MaxValue }; - foreach (int baseValue in bases) - { - Console.WriteLine("Base {0} conversion:", baseValue); - foreach (byte number in numbers) - { - Console.WriteLine(" {0,-5} --> 0x{1}", - number, Convert.ToString(number, baseValue)); - } - } - // The example displays the following output: - // Base 2 conversion: - // 0 --> 0x0 - // 12 --> 0x1100 - // 103 --> 0x1100111 - // 255 --> 0x11111111 - // Base 8 conversion: - // 0 --> 0x0 - // 12 --> 0x14 - // 103 --> 0x147 - // 255 --> 0x377 - // Base 10 conversion: - // 0 --> 0x0 - // 12 --> 0x12 - // 103 --> 0x103 - // 255 --> 0x255 - // Base 16 conversion: - // 0 --> 0x0 - // 12 --> 0xc - // 103 --> 0x67 - // 255 --> 0xff - // - } + foreach (int baseValue in bases) + { + Console.WriteLine($"Base {baseValue} conversion:"); + foreach (byte number in numbers) + { + Console.WriteLine($" {number,-5} --> 0x{Convert.ToString(number, baseValue)}"); + } + } + // The example displays the following output: + // Base 2 conversion: + // 0 --> 0x0 + // 12 --> 0x1100 + // 103 --> 0x1100111 + // 255 --> 0x11111111 + // Base 8 conversion: + // 0 --> 0x0 + // 12 --> 0x14 + // 103 --> 0x147 + // 255 --> 0x377 + // Base 10 conversion: + // 0 --> 0x0 + // 12 --> 0x12 + // 103 --> 0x103 + // 255 --> 0x255 + // Base 16 conversion: + // 0 --> 0x0 + // 12 --> 0xc + // 103 --> 0x67 + // 255 --> 0xff + // + } - private static void ConvertShort() - { - // - int[] bases = { 2, 8, 10, 16}; - short[] numbers = { Int16.MinValue, -13621, -18, 12, 19142, Int16.MaxValue }; + private static void ConvertShort() + { + // + int[] bases = { 2, 8, 10, 16 }; + short[] numbers = { short.MinValue, -13621, -18, 12, 19142, short.MaxValue }; - foreach (int baseValue in bases) - { - Console.WriteLine("Base {0} conversion:", baseValue); - foreach (short number in numbers) - { - Console.WriteLine(" {0,-8} --> 0x{1}", - number, Convert.ToString(number, baseValue)); - } - } - // The example displays the following output: - // Base 2 conversion: - // -32768 --> 0x1000000000000000 - // -13621 --> 0x1100101011001011 - // -18 --> 0x1111111111101110 - // 12 --> 0x1100 - // 19142 --> 0x100101011000110 - // 32767 --> 0x111111111111111 - // Base 8 conversion: - // -32768 --> 0x100000 - // -13621 --> 0x145313 - // -18 --> 0x177756 - // 12 --> 0x14 - // 19142 --> 0x45306 - // 32767 --> 0x77777 - // Base 10 conversion: - // -32768 --> 0x-32768 - // -13621 --> 0x-13621 - // -18 --> 0x-18 - // 12 --> 0x12 - // 19142 --> 0x19142 - // 32767 --> 0x32767 - // Base 16 conversion: - // -32768 --> 0x8000 - // -13621 --> 0xcacb - // -18 --> 0xffee - // 12 --> 0xc - // 19142 --> 0x4ac6 - // 32767 --> 0x7fff - // - } + foreach (int baseValue in bases) + { + Console.WriteLine($"Base {baseValue} conversion:"); + foreach (short number in numbers) + { + Console.WriteLine($" {number,-8} --> 0x{Convert.ToString(number, baseValue)}"); + } + } + // The example displays the following output: + // Base 2 conversion: + // -32768 --> 0x1000000000000000 + // -13621 --> 0x1100101011001011 + // -18 --> 0x1111111111101110 + // 12 --> 0x1100 + // 19142 --> 0x100101011000110 + // 32767 --> 0x111111111111111 + // Base 8 conversion: + // -32768 --> 0x100000 + // -13621 --> 0x145313 + // -18 --> 0x177756 + // 12 --> 0x14 + // 19142 --> 0x45306 + // 32767 --> 0x77777 + // Base 10 conversion: + // -32768 --> 0x-32768 + // -13621 --> 0x-13621 + // -18 --> 0x-18 + // 12 --> 0x12 + // 19142 --> 0x19142 + // 32767 --> 0x32767 + // Base 16 conversion: + // -32768 --> 0x8000 + // -13621 --> 0xcacb + // -18 --> 0xffee + // 12 --> 0xc + // 19142 --> 0x4ac6 + // 32767 --> 0x7fff + // + } - private static void ConvertInt() - { - // - int[] bases = { 2, 8, 10, 16}; - int[] numbers = { Int32.MinValue, -19327543, -13621, -18, 12, - 19142, Int32.MaxValue }; + private static void ConvertInt() + { + // + int[] bases = { 2, 8, 10, 16 }; + int[] numbers = { int.MinValue, -19327543, -13621, -18, 12, + 19142, int.MaxValue }; - foreach (int baseValue in bases) - { - Console.WriteLine("Base {0} conversion:", baseValue); - foreach (int number in numbers) - { - Console.WriteLine(" {0,-15} --> 0x{1}", - number, Convert.ToString(number, baseValue)); - } - } - // The example displays the following output: - // Base 2 conversion: - // -2147483648 --> 0x10000000000000000000000000000000 - // -19327543 --> 0x11111110110110010001010111001001 - // -13621 --> 0x11111111111111111100101011001011 - // -18 --> 0x11111111111111111111111111101110 - // 12 --> 0x1100 - // 19142 --> 0x100101011000110 - // 2147483647 --> 0x1111111111111111111111111111111 - // Base 8 conversion: - // -2147483648 --> 0x20000000000 - // -19327543 --> 0x37666212711 - // -13621 --> 0x37777745313 - // -18 --> 0x37777777756 - // 12 --> 0x14 - // 19142 --> 0x45306 - // 2147483647 --> 0x17777777777 - // Base 10 conversion: - // -2147483648 --> 0x-2147483648 - // -19327543 --> 0x-19327543 - // -13621 --> 0x-13621 - // -18 --> 0x-18 - // 12 --> 0x12 - // 19142 --> 0x19142 - // 2147483647 --> 0x2147483647 - // Base 16 conversion: - // -2147483648 --> 0x80000000 - // -19327543 --> 0xfed915c9 - // -13621 --> 0xffffcacb - // -18 --> 0xffffffee - // 12 --> 0xc - // 19142 --> 0x4ac6 - // 2147483647 --> 0x7fffffff - // - } + foreach (int baseValue in bases) + { + Console.WriteLine($"Base {baseValue} conversion:"); + foreach (int number in numbers) + { + Console.WriteLine($" {number,-15} --> 0x{Convert.ToString(number, baseValue)}"); + } + } + // The example displays the following output: + // Base 2 conversion: + // -2147483648 --> 0x10000000000000000000000000000000 + // -19327543 --> 0x11111110110110010001010111001001 + // -13621 --> 0x11111111111111111100101011001011 + // -18 --> 0x11111111111111111111111111101110 + // 12 --> 0x1100 + // 19142 --> 0x100101011000110 + // 2147483647 --> 0x1111111111111111111111111111111 + // Base 8 conversion: + // -2147483648 --> 0x20000000000 + // -19327543 --> 0x37666212711 + // -13621 --> 0x37777745313 + // -18 --> 0x37777777756 + // 12 --> 0x14 + // 19142 --> 0x45306 + // 2147483647 --> 0x17777777777 + // Base 10 conversion: + // -2147483648 --> 0x-2147483648 + // -19327543 --> 0x-19327543 + // -13621 --> 0x-13621 + // -18 --> 0x-18 + // 12 --> 0x12 + // 19142 --> 0x19142 + // 2147483647 --> 0x2147483647 + // Base 16 conversion: + // -2147483648 --> 0x80000000 + // -19327543 --> 0xfed915c9 + // -13621 --> 0xffffcacb + // -18 --> 0xffffffee + // 12 --> 0xc + // 19142 --> 0x4ac6 + // 2147483647 --> 0x7fffffff + // + } - private static void ConvertLong() - { - // - int[] bases = { 2, 8, 10, 16}; - long[] numbers = { Int64.MinValue, -193275430, -13621, -18, 12, - 1914206117, Int64.MaxValue }; + private static void ConvertLong() + { + // + int[] bases = { 2, 8, 10, 16 }; + long[] numbers = { long.MinValue, -193275430, -13621, -18, 12, + 1914206117, long.MaxValue }; - foreach (int baseValue in bases) - { - Console.WriteLine("Base {0} conversion:", baseValue); - foreach (long number in numbers) - { - Console.WriteLine(" {0,-23} --> 0x{1}", - number, Convert.ToString(number, baseValue)); - } - } - // The example displays the following output: - // Base 2 conversion: - // -9223372036854775808 --> 0x1000000000000000000000000000000000000000000000000000000000000000 - // -193275430 --> 0x1111111111111111111111111111111111110100011110101101100111011010 - // -13621 --> 0x1111111111111111111111111111111111111111111111111100101011001011 - // -18 --> 0x1111111111111111111111111111111111111111111111111111111111101110 - // 12 --> 0x1100 - // 1914206117 --> 0x1110010000110000111011110100101 - // 9223372036854775807 --> 0x111111111111111111111111111111111111111111111111111111111111111 - // Base 8 conversion: - // -9223372036854775808 --> 0x1000000000000000000000 - // -193275430 --> 0x1777777777776436554732 - // -13621 --> 0x1777777777777777745313 - // -18 --> 0x1777777777777777777756 - // 12 --> 0x14 - // 1914206117 --> 0x16206073645 - // 9223372036854775807 --> 0x777777777777777777777 - // Base 10 conversion: - // -9223372036854775808 --> 0x-9223372036854775808 - // -193275430 --> 0x-193275430 - // -13621 --> 0x-13621 - // -18 --> 0x-18 - // 12 --> 0x12 - // 1914206117 --> 0x1914206117 - // 9223372036854775807 --> 0x9223372036854775807 - // Base 16 conversion: - // -9223372036854775808 --> 0x8000000000000000 - // -193275430 --> 0xfffffffff47ad9da - // -13621 --> 0xffffffffffffcacb - // -18 --> 0xffffffffffffffee - // 12 --> 0xc - // 1914206117 --> 0x721877a5 - // 9223372036854775807 --> 0x7fffffffffffffff - // - } + foreach (int baseValue in bases) + { + Console.WriteLine($"Base {baseValue} conversion:"); + foreach (long number in numbers) + { + Console.WriteLine($" {number,-23} --> 0x{Convert.ToString(number, baseValue)}"); + } + } + // The example displays the following output: + // Base 2 conversion: + // -9223372036854775808 --> 0x1000000000000000000000000000000000000000000000000000000000000000 + // -193275430 --> 0x1111111111111111111111111111111111110100011110101101100111011010 + // -13621 --> 0x1111111111111111111111111111111111111111111111111100101011001011 + // -18 --> 0x1111111111111111111111111111111111111111111111111111111111101110 + // 12 --> 0x1100 + // 1914206117 --> 0x1110010000110000111011110100101 + // 9223372036854775807 --> 0x111111111111111111111111111111111111111111111111111111111111111 + // Base 8 conversion: + // -9223372036854775808 --> 0x1000000000000000000000 + // -193275430 --> 0x1777777777776436554732 + // -13621 --> 0x1777777777777777745313 + // -18 --> 0x1777777777777777777756 + // 12 --> 0x14 + // 1914206117 --> 0x16206073645 + // 9223372036854775807 --> 0x777777777777777777777 + // Base 10 conversion: + // -9223372036854775808 --> 0x-9223372036854775808 + // -193275430 --> 0x-193275430 + // -13621 --> 0x-13621 + // -18 --> 0x-18 + // 12 --> 0x12 + // 1914206117 --> 0x1914206117 + // 9223372036854775807 --> 0x9223372036854775807 + // Base 16 conversion: + // -9223372036854775808 --> 0x8000000000000000 + // -193275430 --> 0xfffffffff47ad9da + // -13621 --> 0xffffffffffffcacb + // -18 --> 0xffffffffffffffee + // 12 --> 0xc + // 1914206117 --> 0x721877a5 + // 9223372036854775807 --> 0x7fffffffffffffff + // + } } diff --git a/snippets/csharp/System/Convert/ToString/tostring3.cs b/snippets/csharp/System/Convert/ToString/tostring3.cs index bb62d7ef9b1..a0ef79a311b 100644 --- a/snippets/csharp/System/Convert/ToString/tostring3.cs +++ b/snippets/csharp/System/Convert/ToString/tostring3.cs @@ -2,357 +2,353 @@ public class Example { - public static void Main() - { - ConvertDateTimeWithProvider(); - Console.WriteLine("-----"); - ConvertDecimalWithProvider(); - Console.WriteLine("-----"); - ConvertDoubleWithProvider(); - Console.WriteLine(); - ConvertByteWithProvider(); - Console.WriteLine(); - ConvertSByteWithProvider(); - Console.WriteLine("-----"); - ConvertSingleWithProvider(); - Console.WriteLine("-----"); - ConvertInt16WithProvider(); - Console.WriteLine("-----"); - ConvertInt32WithProvider(); - Console.WriteLine("-----"); - ConvertInt64WithProvider(); - ConvertUInt16WithProvider(); - Console.WriteLine("-----"); - ConvertUInt32WithProvider(); - Console.WriteLine("-----"); - ConvertUInt64WithProvider(); - } + public static void Main() + { + ConvertDateTimeWithProvider(); + Console.WriteLine("-----"); + ConvertDecimalWithProvider(); + Console.WriteLine("-----"); + ConvertDoubleWithProvider(); + Console.WriteLine(); + ConvertByteWithProvider(); + Console.WriteLine(); + ConvertSByteWithProvider(); + Console.WriteLine("-----"); + ConvertSingleWithProvider(); + Console.WriteLine("-----"); + ConvertInt16WithProvider(); + Console.WriteLine("-----"); + ConvertInt32WithProvider(); + Console.WriteLine("-----"); + ConvertInt64WithProvider(); + ConvertUInt16WithProvider(); + Console.WriteLine("-----"); + ConvertUInt32WithProvider(); + Console.WriteLine("-----"); + ConvertUInt64WithProvider(); + } - private static void ConvertDateTimeWithProvider() - { - // - // Specify the date to be formatted using various cultures. - DateTime tDate = new DateTime(2010, 4, 15, 20, 30, 40, 333); - // Specify the cultures. - string[] cultureNames = { "en-US", "es-AR", "fr-FR", "hi-IN", + private static void ConvertDateTimeWithProvider() + { + // + // Specify the date to be formatted using various cultures. + DateTime tDate = new(2010, 4, 15, 20, 30, 40, 333); + // Specify the cultures. + string[] cultureNames = { "en-US", "es-AR", "fr-FR", "hi-IN", "ja-JP", "nl-NL", "ru-RU", "ur-PK" }; - Console.WriteLine("Converting the date {0}: ", - Convert.ToString(tDate, - System.Globalization.CultureInfo.InvariantCulture)); + Console.WriteLine($"Converting the date {Convert.ToString(tDate, + System.Globalization.CultureInfo.InvariantCulture)}: "); - foreach (string cultureName in cultureNames) - { - System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(cultureName); - string dateString = Convert.ToString(tDate, culture); - Console.WriteLine(" {0}: {1,-12}", - culture.Name, dateString); - } - // The example displays the following output: - // Converting the date 04/15/2010 20:30:40: - // en-US: 4/15/2010 8:30:40 PM - // es-AR: 15/04/2010 08:30:40 p.m. - // fr-FR: 15/04/2010 20:30:40 - // hi-IN: 15-04-2010 20:30:40 - // ja-JP: 2010/04/15 20:30:40 - // nl-NL: 15-4-2010 20:30:40 - // ru-RU: 15.04.2010 20:30:40 - // ur-PK: 15/04/2010 8:30:40 PM - // - } + foreach (string cultureName in cultureNames) + { + System.Globalization.CultureInfo culture = new(cultureName); + string dateString = Convert.ToString(tDate, culture); + Console.WriteLine($" {culture.Name}: {dateString,-12}"); + } + // The example displays the following output: + // Converting the date 04/15/2010 20:30:40: + // en-US: 4/15/2010 8:30:40 PM + // es-AR: 15/04/2010 08:30:40 p.m. + // fr-FR: 15/04/2010 20:30:40 + // hi-IN: 15-04-2010 20:30:40 + // ja-JP: 2010/04/15 20:30:40 + // nl-NL: 15-4-2010 20:30:40 + // ru-RU: 15.04.2010 20:30:40 + // ur-PK: 15/04/2010 8:30:40 PM + // + } - private static void ConvertDecimalWithProvider() - { - // - // Define an array of numbers to display. - decimal[] numbers = { 1734231911290.16m, -17394.32921m, + private static void ConvertDecimalWithProvider() + { + // + // Define an array of numbers to display. + decimal[] numbers = { 1734231911290.16m, -17394.32921m, 3193.23m, 98012368321.684m }; - // Define the culture names used to display them. - string[] cultureNames = { "en-US", "fr-FR", "ja-JP", "ru-RU" }; + // Define the culture names used to display them. + string[] cultureNames = { "en-US", "fr-FR", "ja-JP", "ru-RU" }; - foreach (decimal number in numbers) - { - Console.WriteLine("{0}:", Convert.ToString(number, - System.Globalization.CultureInfo.InvariantCulture)); - foreach (string cultureName in cultureNames) - { - System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(cultureName); - Console.WriteLine(" {0}: {1,20}", - culture.Name, Convert.ToString(number, culture)); - } - Console.WriteLine(); - } - // The example displays the following output: - // 1734231911290.16: - // en-US: 1734231911290.16 - // fr-FR: 1734231911290,16 - // ja-JP: 1734231911290.16 - // ru-RU: 1734231911290,16 - // - // -17394.32921: - // en-US: -17394.32921 - // fr-FR: -17394,32921 - // ja-JP: -17394.32921 - // ru-RU: -17394,32921 - // - // 3193.23: - // en-US: 3193.23 - // fr-FR: 3193,23 - // ja-JP: 3193.23 - // ru-RU: 3193,23 - // - // 98012368321.684: - // en-US: 98012368321.684 - // fr-FR: 98012368321,684 - // ja-JP: 98012368321.684 - // ru-RU: 98012368321,684 - // - } + foreach (decimal number in numbers) + { + Console.WriteLine($"{Convert.ToString(number, + System.Globalization.CultureInfo.InvariantCulture)}:"); + foreach (string cultureName in cultureNames) + { + System.Globalization.CultureInfo culture = new(cultureName); + Console.WriteLine($" {culture.Name}: {Convert.ToString(number, culture),20}"); + } + Console.WriteLine(); + } + // The example displays the following output: + // 1734231911290.16: + // en-US: 1734231911290.16 + // fr-FR: 1734231911290,16 + // ja-JP: 1734231911290.16 + // ru-RU: 1734231911290,16 + // + // -17394.32921: + // en-US: -17394.32921 + // fr-FR: -17394,32921 + // ja-JP: -17394.32921 + // ru-RU: -17394,32921 + // + // 3193.23: + // en-US: 3193.23 + // fr-FR: 3193,23 + // ja-JP: 3193.23 + // ru-RU: 3193,23 + // + // 98012368321.684: + // en-US: 98012368321.684 + // fr-FR: 98012368321,684 + // ja-JP: 98012368321.684 + // ru-RU: 98012368321,684 + // + } - private static void ConvertDoubleWithProvider() - { - // - // Define an array of numbers to display. - double[] numbers = { -1.5345e16, -123.4321, 19092.123, 1.1734231911290e16 }; - // Define the culture names used to display them. - string[] cultureNames = { "en-US", "fr-FR", "ja-JP", "ru-RU" }; + private static void ConvertDoubleWithProvider() + { + // + // Define an array of numbers to display. + double[] numbers = { -1.5345e16, -123.4321, 19092.123, 1.1734231911290e16 }; + // Define the culture names used to display them. + string[] cultureNames = { "en-US", "fr-FR", "ja-JP", "ru-RU" }; - foreach (double number in numbers) - { - Console.WriteLine("{0}:", Convert.ToString(number, - System.Globalization.CultureInfo.InvariantCulture)); - foreach (string cultureName in cultureNames) - { - System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(cultureName); - Console.WriteLine(" {0}: {1,20}", - culture.Name, Convert.ToString(number, culture)); - } - Console.WriteLine(); - } - // The example displays the following output: - // -1.5345E+16: - // en-US: -1.5345E+16 - // fr-FR: -1,5345E+16 - // ja-JP: -1.5345E+16 - // ru-RU: -1,5345E+16 - // - // -123.4321: - // en-US: -123.4321 - // fr-FR: -123,4321 - // ja-JP: -123.4321 - // ru-RU: -123,4321 - // - // 19092.123: - // en-US: 19092.123 - // fr-FR: 19092,123 - // ja-JP: 19092.123 - // ru-RU: 19092,123 - // - // 1.173423191129E+16: - // en-US: 1.173423191129E+16 - // fr-FR: 1,173423191129E+16 - // ja-JP: 1.173423191129E+16 - // ru-RU: 1,173423191129E+16 - // - } + foreach (double number in numbers) + { + Console.WriteLine($"{Convert.ToString(number, + System.Globalization.CultureInfo.InvariantCulture)}:"); + foreach (string cultureName in cultureNames) + { + System.Globalization.CultureInfo culture = new(cultureName); + Console.WriteLine($" {culture.Name}: {Convert.ToString(number, culture),20}"); + } + Console.WriteLine(); + } + // The example displays the following output: + // -1.5345E+16: + // en-US: -1.5345E+16 + // fr-FR: -1,5345E+16 + // ja-JP: -1.5345E+16 + // ru-RU: -1,5345E+16 + // + // -123.4321: + // en-US: -123.4321 + // fr-FR: -123,4321 + // ja-JP: -123.4321 + // ru-RU: -123,4321 + // + // 19092.123: + // en-US: 19092.123 + // fr-FR: 19092,123 + // ja-JP: 19092.123 + // ru-RU: 19092,123 + // + // 1.173423191129E+16: + // en-US: 1.173423191129E+16 + // fr-FR: 1,173423191129E+16 + // ja-JP: 1.173423191129E+16 + // ru-RU: 1,173423191129E+16 + // + } - private static void ConvertByteWithProvider() - { - // - byte[] numbers = { 12, 100, Byte.MaxValue }; - // Define the culture names used to display them. - string[] cultureNames = { "en-US", "fr-FR" }; + private static void ConvertByteWithProvider() + { + // + byte[] numbers = { 12, 100, byte.MaxValue }; + // Define the culture names used to display them. + string[] cultureNames = { "en-US", "fr-FR" }; - foreach (byte number in numbers) - { - Console.WriteLine("{0}:", Convert.ToString(number, - System.Globalization.CultureInfo.InvariantCulture)); - foreach (string cultureName in cultureNames) - { - System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(cultureName); - Console.WriteLine(" {0}: {1,20}", - culture.Name, Convert.ToString(number, culture)); - } - Console.WriteLine(); - } - // The example displays the following output: - // 12: - // en-US: 12 - // fr-FR: 12 - // - // 100: - // en-US: 100 - // fr-FR: 100 - // - // 255: - // en-US: 255 - // fr-FR: 255 - // - } + foreach (byte number in numbers) + { + Console.WriteLine($"{Convert.ToString(number, + System.Globalization.CultureInfo.InvariantCulture)}:"); + foreach (string cultureName in cultureNames) + { + System.Globalization.CultureInfo culture = new(cultureName); + Console.WriteLine($" {culture.Name}: {Convert.ToString(number, culture),20}"); + } + Console.WriteLine(); + } + // The example displays the following output: + // 12: + // en-US: 12 + // fr-FR: 12 + // + // 100: + // en-US: 100 + // fr-FR: 100 + // + // 255: + // en-US: 255 + // fr-FR: 255 + // + } - private static void ConvertSByteWithProvider() - { - // - sbyte[] numbers = { SByte.MinValue, -12, 17, SByte.MaxValue}; - System.Globalization.NumberFormatInfo nfi = new System.Globalization.NumberFormatInfo(); - nfi.NegativeSign = "~"; - nfi.PositiveSign = "!"; - foreach (sbyte number in numbers) - Console.WriteLine(Convert.ToString(number, nfi)); - // The example displays the following output: - // ~128 - // ~12 - // 17 - // 127 - // - } + private static void ConvertSByteWithProvider() + { + // + sbyte[] numbers = { sbyte.MinValue, -12, 17, sbyte.MaxValue }; + System.Globalization.NumberFormatInfo nfi = new() + { + NegativeSign = "~", + PositiveSign = "!" + }; + foreach (sbyte number in numbers) + Console.WriteLine(Convert.ToString(number, nfi)); + // The example displays the following output: + // ~128 + // ~12 + // 17 + // 127 + // + } - private static void ConvertSingleWithProvider() - { - // - // Define an array of numbers to display. - float[] numbers = { -1.5345e16f, -123.4321f, 19092.123f, 1.1734231911290e16f }; - // Define the culture names used to display them. - string[] cultureNames = { "en-US", "fr-FR", "ja-JP", "ru-RU" }; + private static void ConvertSingleWithProvider() + { + // + // Define an array of numbers to display. + float[] numbers = { -1.5345e16f, -123.4321f, 19092.123f, 1.1734231911290e16f }; + // Define the culture names used to display them. + string[] cultureNames = { "en-US", "fr-FR", "ja-JP", "ru-RU" }; - foreach (float number in numbers) - { - Console.WriteLine("{0}:", Convert.ToString(number, - System.Globalization.CultureInfo.InvariantCulture)); - foreach (string cultureName in cultureNames) - { - System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(cultureName); - Console.WriteLine(" {0}: {1,20}", - culture.Name, Convert.ToString(number, culture)); - } - Console.WriteLine(); - } - // The example displays the following output: - // -1.5345E+16: - // en-US: -1.5345E+16 - // fr-FR: -1,5345E+16 - // ja-JP: -1.5345E+16 - // ru-RU: -1,5345E+16 - // - // -123.4321: - // en-US: -123.4321 - // fr-FR: -123,4321 - // ja-JP: -123.4321 - // ru-RU: -123,4321 - // - // 19092.123: - // en-US: 19092.123 - // fr-FR: 19092,123 - // ja-JP: 19092.123 - // ru-RU: 19092,123 - // - // 1.173423191129E+16: - // en-US: 1.173423191129E+16 - // fr-FR: 1,173423191129E+16 - // ja-JP: 1.173423191129E+16 - // ru-RU: 1,173423191129E+16 - // - } + foreach (float number in numbers) + { + Console.WriteLine($"{Convert.ToString(number, + System.Globalization.CultureInfo.InvariantCulture)}:"); + foreach (string cultureName in cultureNames) + { + System.Globalization.CultureInfo culture = new(cultureName); + Console.WriteLine($" {culture.Name}: {Convert.ToString(number, culture),20}"); + } + Console.WriteLine(); + } + // The example displays the following output: + // -1.5345E+16: + // en-US: -1.5345E+16 + // fr-FR: -1,5345E+16 + // ja-JP: -1.5345E+16 + // ru-RU: -1,5345E+16 + // + // -123.4321: + // en-US: -123.4321 + // fr-FR: -123,4321 + // ja-JP: -123.4321 + // ru-RU: -123,4321 + // + // 19092.123: + // en-US: 19092.123 + // fr-FR: 19092,123 + // ja-JP: 19092.123 + // ru-RU: 19092,123 + // + // 1.173423191129E+16: + // en-US: 1.173423191129E+16 + // fr-FR: 1,173423191129E+16 + // ja-JP: 1.173423191129E+16 + // ru-RU: 1,173423191129E+16 + // + } - private static void ConvertInt16WithProvider() - { - // - short[] numbers = { Int16.MinValue, Int16.MaxValue}; - System.Globalization.NumberFormatInfo nfi = new System.Globalization.NumberFormatInfo(); - nfi.NegativeSign = "~"; - nfi.PositiveSign = "!"; + private static void ConvertInt16WithProvider() + { + // + short[] numbers = { short.MinValue, short.MaxValue }; + System.Globalization.NumberFormatInfo nfi = new() + { + NegativeSign = "~", + PositiveSign = "!" + }; - foreach (short number in numbers) - Console.WriteLine("{0,-8} --> {1,8}", - Convert.ToString(number, System.Globalization.CultureInfo.InvariantCulture), - Convert.ToString(number, nfi)); - // The example displays the following output: - // -32768 --> ~32768 - // 32767 --> 32767 - // - } + foreach (short number in numbers) + Console.WriteLine($"{Convert.ToString(number, System.Globalization.CultureInfo.InvariantCulture),-8} --> {Convert.ToString(number, nfi),8}"); + // The example displays the following output: + // -32768 --> ~32768 + // 32767 --> 32767 + // + } - private static void ConvertInt32WithProvider() - { - // - int[] numbers = { Int32.MinValue, Int32.MaxValue}; - System.Globalization.NumberFormatInfo nfi = new System.Globalization.NumberFormatInfo(); - nfi.NegativeSign = "~"; - nfi.PositiveSign = "!"; + private static void ConvertInt32WithProvider() + { + // + int[] numbers = { int.MinValue, int.MaxValue }; + System.Globalization.NumberFormatInfo nfi = new() + { + NegativeSign = "~", + PositiveSign = "!" + }; - foreach (int number in numbers) - Console.WriteLine("{0,-12} --> {1,12}", - Convert.ToString(number, System.Globalization.CultureInfo.InvariantCulture), - Convert.ToString(number, nfi)); - // The example displays the following output: - // -2147483648 --> ~2147483648 - // 2147483647 --> 2147483647 - // - } + foreach (int number in numbers) + Console.WriteLine($"{Convert.ToString(number, System.Globalization.CultureInfo.InvariantCulture),-12} --> {Convert.ToString(number, nfi),12}"); + // The example displays the following output: + // -2147483648 --> ~2147483648 + // 2147483647 --> 2147483647 + // + } - private static void ConvertInt64WithProvider() - { - // - long[] numbers = { ((long) Int32.MinValue) * 2, ((long) Int32.MaxValue) * 2}; - System.Globalization.NumberFormatInfo nfi = new System.Globalization.NumberFormatInfo(); - nfi.NegativeSign = "~"; - nfi.PositiveSign = "!"; + private static void ConvertInt64WithProvider() + { + // + long[] numbers = { ((long)int.MinValue) * 2, ((long)int.MaxValue) * 2 }; + System.Globalization.NumberFormatInfo nfi = new() + { + NegativeSign = "~", + PositiveSign = "!" + }; - foreach (long number in numbers) - Console.WriteLine("{0,-12} --> {1,12}", - Convert.ToString(number, System.Globalization.CultureInfo.InvariantCulture), - Convert.ToString(number, nfi)); - // The example displays the following output: - // -4294967296 --> ~4294967296 - // 4294967294 --> 4294967294 - // - } + foreach (long number in numbers) + Console.WriteLine($"{Convert.ToString(number, System.Globalization.CultureInfo.InvariantCulture),-12} --> {Convert.ToString(number, nfi),12}"); + // The example displays the following output: + // -4294967296 --> ~4294967296 + // 4294967294 --> 4294967294 + // + } - private static void ConvertUInt16WithProvider() - { - // - ushort number = UInt16.MaxValue; - System.Globalization.NumberFormatInfo nfi = new System.Globalization.NumberFormatInfo(); - nfi.NegativeSign = "~"; - nfi.PositiveSign = "!"; + private static void ConvertUInt16WithProvider() + { + // + ushort number = ushort.MaxValue; + System.Globalization.NumberFormatInfo nfi = new() + { + NegativeSign = "~", + PositiveSign = "!" + }; - Console.WriteLine("{0,-6} --> {1,6}", - Convert.ToString(number, System.Globalization.CultureInfo.InvariantCulture), - Convert.ToString(number, nfi)); - // The example displays the following output: - // 65535 --> 65535 - // - } + Console.WriteLine($"{Convert.ToString(number, System.Globalization.CultureInfo.InvariantCulture),-6} --> {Convert.ToString(number, nfi),6}"); + // The example displays the following output: + // 65535 --> 65535 + // + } - private static void ConvertUInt32WithProvider() - { - // - uint number = UInt32.MaxValue; - System.Globalization.NumberFormatInfo nfi = new System.Globalization.NumberFormatInfo(); - nfi.NegativeSign = "~"; - nfi.PositiveSign = "!"; + private static void ConvertUInt32WithProvider() + { + // + uint number = uint.MaxValue; + System.Globalization.NumberFormatInfo nfi = new() + { + NegativeSign = "~", + PositiveSign = "!" + }; - Console.WriteLine("{0,-8} --> {1,8}", - Convert.ToString(number, System.Globalization.CultureInfo.InvariantCulture), - Convert.ToString(number, nfi)); - // The example displays the following output: - // 4294967295 --> 4294967295 - // - } + Console.WriteLine($"{Convert.ToString(number, System.Globalization.CultureInfo.InvariantCulture),-8} --> {Convert.ToString(number, nfi),8}"); + // The example displays the following output: + // 4294967295 --> 4294967295 + // + } - private static void ConvertUInt64WithProvider() - { - // - ulong number = UInt64.MaxValue; - System.Globalization.NumberFormatInfo nfi = new System.Globalization.NumberFormatInfo(); - nfi.NegativeSign = "~"; - nfi.PositiveSign = "!"; + private static void ConvertUInt64WithProvider() + { + // + ulong number = ulong.MaxValue; + System.Globalization.NumberFormatInfo nfi = new() + { + NegativeSign = "~", + PositiveSign = "!" + }; - Console.WriteLine("{0,-12} --> {1,12}", - Convert.ToString(number, System.Globalization.CultureInfo.InvariantCulture), - Convert.ToString(number, nfi)); - // The example displays the following output: - // 18446744073709551615 --> 18446744073709551615 - // - } + Console.WriteLine($"{Convert.ToString(number, System.Globalization.CultureInfo.InvariantCulture),-12} --> {Convert.ToString(number, nfi),12}"); + // The example displays the following output: + // 18446744073709551615 --> 18446744073709551615 + // + } } diff --git a/snippets/csharp/System/Convert/ToString/tostring5.cs b/snippets/csharp/System/Convert/ToString/tostring5.cs index ec700e064c2..0bb9f4444b5 100644 --- a/snippets/csharp/System/Convert/ToString/tostring5.cs +++ b/snippets/csharp/System/Convert/ToString/tostring5.cs @@ -3,46 +3,31 @@ public class Temperature { - private decimal m_Temp; - - public Temperature(decimal temperature) - { - this.m_Temp = temperature; - } - - public decimal Celsius - { - get { return this.m_Temp; } - } - - public decimal Kelvin - { - get { return this.m_Temp + 273.15m; } - } - - public decimal Fahrenheit - { - get { return Math.Round((decimal) (this.m_Temp * 9 / 5 + 32), 2); } - } - - public override string ToString() - { - return m_Temp.ToString("N2") + " °C"; - } + private decimal m_Temp; + + public Temperature(decimal temperature) => this.m_Temp = temperature; + + public decimal Celsius => this.m_Temp; + + public decimal Kelvin => this.m_Temp + 273.15m; + + public decimal Fahrenheit => Math.Round((decimal)(this.m_Temp * 9 / 5 + 32), 2); + + public override string ToString() => m_Temp.ToString("N2") + " °C"; } public class Example { - public static void Main() - { - Temperature cold = new Temperature(-40); - Temperature freezing = new Temperature(0); - Temperature boiling = new Temperature(100); - - Console.WriteLine(Convert.ToString(cold, null)); - Console.WriteLine(Convert.ToString(freezing, null)); - Console.WriteLine(Convert.ToString(boiling, null)); - } + public static void Main() + { + Temperature cold = new(-40); + Temperature freezing = new(0); + Temperature boiling = new(100); + + Console.WriteLine(Convert.ToString(cold, null)); + Console.WriteLine(Convert.ToString(freezing, null)); + Console.WriteLine(Convert.ToString(boiling, null)); + } } // The example dosplays the following output: // -40.00 °C diff --git a/snippets/csharp/System/Convert/ToString/tostring6.cs b/snippets/csharp/System/Convert/ToString/tostring6.cs index 142c6fc3514..94f061030ad 100644 --- a/snippets/csharp/System/Convert/ToString/tostring6.cs +++ b/snippets/csharp/System/Convert/ToString/tostring6.cs @@ -2,48 +2,50 @@ public class Example { - public static void Main() - { - ConvertInt32(); - Console.WriteLine("-----"); - ConvertInt64(); - } + public static void Main() + { + ConvertInt32(); + Console.WriteLine("-----"); + ConvertInt64(); + } - private static void ConvertInt32() - { - // Create a NumberFormatInfo object and set several of its - // properties that control default integer formatting. - System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo(); - provider.NegativeSign = "minus "; + private static void ConvertInt32() + { + // Create a NumberFormatInfo object and set several of its + // properties that control default integer formatting. + System.Globalization.NumberFormatInfo provider = new() + { + NegativeSign = "minus " + }; - int[] values = { -20, 0, 100 }; + int[] values = { -20, 0, 100 }; - foreach (int value in values) - Console.WriteLine("{0,-5} --> {1,8}", - value, Convert.ToString(value, provider)); - // The example displays the following output: - // -20 --> minus 20 - // 0 --> 0 - // 100 --> 100 - } + foreach (int value in values) + Console.WriteLine($"{value,-5} --> {Convert.ToString(value, provider),8}"); + // The example displays the following output: + // -20 --> minus 20 + // 0 --> 0 + // 100 --> 100 + } - private static void ConvertInt64() - { - // - // Create a NumberFormatInfo object and set several of its - // properties that control default integer formatting. - System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo(); - provider.NegativeSign = "minus "; + private static void ConvertInt64() + { + // + // Create a NumberFormatInfo object and set several of its + // properties that control default integer formatting. + System.Globalization.NumberFormatInfo provider = new() + { + NegativeSign = "minus " + }; - long[] values = { -200, 0, 1000 }; + long[] values = { -200, 0, 1000 }; - foreach (long value in values) - Console.WriteLine("{0,-6} --> {1,10}", - value, Convert.ToString(value, provider)); - // The example displays the following output: - // -200 --> minus 200 - // 0 --> 0 - // 1000 --> 1000 - // - } + foreach (long value in values) + Console.WriteLine($"{value,-6} --> {Convert.ToString(value, provider),10}"); + // The example displays the following output: + // -200 --> minus 200 + // 0 --> 0 + // 1000 --> 1000 + // + } } diff --git a/snippets/csharp/System/Convert/ToString/tostring7.cs b/snippets/csharp/System/Convert/ToString/tostring7.cs index 682b69f8bfe..e51defed3d8 100644 --- a/snippets/csharp/System/Convert/ToString/tostring7.cs +++ b/snippets/csharp/System/Convert/ToString/tostring7.cs @@ -4,28 +4,26 @@ public class Example { - public static void Main() - { - // Create a NumberFormatInfo object and set its NegativeSigns - // property to use for integer formatting. - NumberFormatInfo provider = new NumberFormatInfo(); - provider.NegativeSign = "minus "; + public static void Main() + { + // Create a NumberFormatInfo object and set its NegativeSigns + // property to use for integer formatting. + NumberFormatInfo provider = new() + { + NegativeSign = "minus " + }; - int[] values = { -20, 0, 100 }; + int[] values = { -20, 0, 100 }; - Console.WriteLine("{0,-8} --> {1,10} {2,10}\n", "Value", - CultureInfo.CurrentCulture.Name, - "Custom"); - foreach (int value in values) - Console.WriteLine("{0,-8} --> {1,10} {2,10}", - value, Convert.ToString(value), - Convert.ToString(value, provider)); - // The example displays output like the following: - // Value --> en-US Custom - // - // -20 --> -20 minus 20 - // 0 --> 0 0 - // 100 --> 100 100 - } + Console.WriteLine($"{"Value",-8} --> {CultureInfo.CurrentCulture.Name,10} {"Custom",10}\n"); + foreach (int value in values) + Console.WriteLine($"{value,-8} --> {Convert.ToString(value),10} {Convert.ToString(value, provider),10}"); + // The example displays output like the following: + // Value --> en-US Custom + // + // -20 --> -20 minus 20 + // 0 --> 0 0 + // 100 --> 100 100 + } } -// \ No newline at end of file +// diff --git a/snippets/csharp/System/Convert/ToString/tostring_obj30.cs b/snippets/csharp/System/Convert/ToString/tostring_obj30.cs index 33baea1bd25..47c3f2a6d3c 100644 --- a/snippets/csharp/System/Convert/ToString/tostring_obj30.cs +++ b/snippets/csharp/System/Convert/ToString/tostring_obj30.cs @@ -3,83 +3,72 @@ public class Temperature : IFormattable { - private decimal m_Temp; + private decimal m_Temp; - public Temperature(decimal temperature) - { - this.m_Temp = temperature; - } + public Temperature(decimal temperature) => this.m_Temp = temperature; - public decimal Celsius - { get { return this.m_Temp; } } + public decimal Celsius => this.m_Temp; - public decimal Kelvin - { get { return this.m_Temp + 273.15m; } } + public decimal Kelvin => this.m_Temp + 273.15m; - public decimal Fahrenheit - { get { return Math.Round(this.m_Temp * 9m / 5m + 32m, 2); } } + public decimal Fahrenheit => Math.Round(this.m_Temp * 9m / 5m + 32m, 2); - public override String ToString() - { - return ToString("G", null); - } + public override string ToString() => ToString("G", null); - public String ToString(String fmt, IFormatProvider provider) - { - TemperatureProvider formatter = null; - if (provider != null) - formatter = provider.GetFormat(typeof(TemperatureProvider)) - as TemperatureProvider; + public string ToString(string fmt, IFormatProvider provider) + { + TemperatureProvider formatter = null; + if (provider != null) + formatter = provider.GetFormat(typeof(TemperatureProvider)) + as TemperatureProvider; - if (String.IsNullOrWhiteSpace(fmt)) { - if (formatter != null) - fmt = formatter.Format; - else - fmt = "G"; - } + if (string.IsNullOrWhiteSpace(fmt)) + { + if (formatter != null) + fmt = formatter.Format; + else + fmt = "G"; + } - switch (fmt.ToUpper()) { - case "G": - case "C": - return m_Temp.ToString("N2") + " °C"; - case "F": - return Fahrenheit.ToString("N2") + " °F"; - case "K": - return Kelvin.ToString("N2") + " K"; - default: - throw new FormatException(String.Format("'{0}' is not a valid format specifier.", fmt)); - } - } + switch (fmt.ToUpper()) + { + case "G": + case "C": + return m_Temp.ToString("N2") + " °C"; + case "F": + return Fahrenheit.ToString("N2") + " °F"; + case "K": + return Kelvin.ToString("N2") + " K"; + default: + throw new FormatException($"'{fmt}' is not a valid format specifier."); + } + } } public class TemperatureProvider : IFormatProvider { - private String[] fmtStrings = { "C", "G", "F", "K" }; - private Random rnd = new Random(); + private string[] fmtStrings = { "C", "G", "F", "K" }; + private Random rnd = new(); - public Object GetFormat(Type formatType) - { - return this; - } + public object GetFormat(Type formatType) => this; - public String Format - { get { return fmtStrings[rnd.Next(0, fmtStrings.Length)]; } } + public string Format => fmtStrings[rnd.Next(0, fmtStrings.Length)]; } public class Example { - public static void Main() - { - Temperature cold = new Temperature (-40); - Temperature freezing = new Temperature (0); - Temperature boiling = new Temperature (100); + public static void Main() + { + Temperature cold = new(-40); + Temperature freezing = new(0); + Temperature boiling = new(100); - TemperatureProvider tp = new TemperatureProvider(); + TemperatureProvider tp = new(); - Console.WriteLine(Convert.ToString(cold, tp)); - Console.WriteLine(Convert.ToString(freezing, tp)); - Console.WriteLine(Convert.ToString(boiling, tp)); - } + Console.WriteLine(Convert.ToString(cold, tp)); + Console.WriteLine(Convert.ToString(freezing, tp)); + Console.WriteLine(Convert.ToString(boiling, tp)); + } } // The example displays output like the following: // -40.00 °C diff --git a/snippets/csharp/System/Convert/ToString/tostring_string1.cs b/snippets/csharp/System/Convert/ToString/tostring_string1.cs index 4116f18f92c..02c7f87913f 100644 --- a/snippets/csharp/System/Convert/ToString/tostring_string1.cs +++ b/snippets/csharp/System/Convert/ToString/tostring_string1.cs @@ -3,18 +3,16 @@ public class Example { - public static void Main() - { - String article = "An"; - String noun = "apple"; - String str1 = String.Format("{0} {1}", article, noun); - String str2 = Convert.ToString(str1); + public static void Main() + { + string article = "An"; + string noun = "apple"; + string str1 = $"{article} {noun}"; + string str2 = Convert.ToString(str1); - Console.WriteLine("str1 is interned: {0}", - ! (String.IsInterned(str1) == null)); - Console.WriteLine("str1 and str2 are the same reference: {0}", - Object.ReferenceEquals(str1, str2)); - } + Console.WriteLine($"str1 is interned: {!(string.IsInterned(str1) == null)}"); + Console.WriteLine($"str1 and str2 are the same reference: {object.ReferenceEquals(str1, str2)}"); + } } // The example displays the following output: // str1 is interned: False diff --git a/snippets/csharp/System/Convert/ToUInt16/touint16_1.cs b/snippets/csharp/System/Convert/ToUInt16/touint16_1.cs index 0238d65f5e7..864c671d50a 100644 --- a/snippets/csharp/System/Convert/ToUInt16/touint16_1.cs +++ b/snippets/csharp/System/Convert/ToUInt16/touint16_1.cs @@ -2,444 +2,430 @@ public class Example { - public static void Main() - { - ConvertBoolean(); - Console.WriteLine("-----"); - ConvertByte(); - Console.WriteLine("-----"); - ConvertChar(); - Console.WriteLine("-----"); - ConvertDecimal(); - Console.WriteLine("-----"); - ConvertDouble(); - Console.WriteLine("----"); - ConvertInt16(); - Console.WriteLine("-----"); - ConvertInt32(); - Console.WriteLine("-----"); - ConvertInt64(); - Console.WriteLine("-----"); - ConvertObject(); - Console.WriteLine("-----"); - ConvertSByte(); - Console.WriteLine("-----"); - ConvertSingle(); - Console.WriteLine("----"); - ConvertString(); - Console.WriteLine("-----"); - ConvertUInt32(); - Console.WriteLine("-----"); - ConvertUInt64(); - } + public static void Main() + { + ConvertBoolean(); + Console.WriteLine("-----"); + ConvertByte(); + Console.WriteLine("-----"); + ConvertChar(); + Console.WriteLine("-----"); + ConvertDecimal(); + Console.WriteLine("-----"); + ConvertDouble(); + Console.WriteLine("----"); + ConvertInt16(); + Console.WriteLine("-----"); + ConvertInt32(); + Console.WriteLine("-----"); + ConvertInt64(); + Console.WriteLine("-----"); + ConvertObject(); + Console.WriteLine("-----"); + ConvertSByte(); + Console.WriteLine("-----"); + ConvertSingle(); + Console.WriteLine("----"); + ConvertString(); + Console.WriteLine("-----"); + ConvertUInt32(); + Console.WriteLine("-----"); + ConvertUInt64(); + } - private static void ConvertBoolean() - { - // - bool falseFlag = false; - bool trueFlag = true; + private static void ConvertBoolean() + { + // + bool falseFlag = false; + bool trueFlag = true; - Console.WriteLine("{0} converts to {1}.", falseFlag, - Convert.ToInt16(falseFlag)); - Console.WriteLine("{0} converts to {1}.", trueFlag, - Convert.ToUInt16(trueFlag)); - // The example displays the following output: - // False converts to 0. - // True converts to 1. - // - } + Console.WriteLine($"{falseFlag} converts to {Convert.ToInt16(falseFlag)}."); + Console.WriteLine($"{trueFlag} converts to {Convert.ToUInt16(trueFlag)}."); + // The example displays the following output: + // False converts to 0. + // True converts to 1. + // + } - private static void ConvertByte() - { - // - byte[] bytes = { Byte.MinValue, 14, 122, Byte.MaxValue}; - ushort result; + private static void ConvertByte() + { + // + byte[] bytes = { byte.MinValue, 14, 122, byte.MaxValue }; + ushort result; - foreach (byte byteValue in bytes) - { - result = Convert.ToUInt16(byteValue); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - byteValue.GetType().Name, byteValue, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the Byte value '0' to the UInt16 value 0. - // Converted the Byte value '14' to the UInt16 value 14. - // Converted the Byte value '122' to the UInt16 value 122. - // Converted the Byte value '255' to the UInt16 value 255. - // - } + foreach (byte byteValue in bytes) + { + result = Convert.ToUInt16(byteValue); + Console.WriteLine($"Converted the {byteValue.GetType().Name} value '{byteValue}' to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the Byte value '0' to the UInt16 value 0. + // Converted the Byte value '14' to the UInt16 value 14. + // Converted the Byte value '122' to the UInt16 value 122. + // Converted the Byte value '255' to the UInt16 value 255. + // + } - private static void ConvertChar() - { - // - char[] chars = { 'a', 'z', '\x0007', '\x03FF', + private static void ConvertChar() + { + // + char[] chars = { 'a', 'z', '\x0007', '\x03FF', '\x7FFF', '\xFFFE' }; - ushort result; + ushort result; - foreach (char ch in chars) - { - try { - result = Convert.ToUInt16(ch); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - ch.GetType().Name, ch, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("Unable to convert u+{0} to a UInt16.", - ((int)ch).ToString("X4")); - } - } - // The example displays the following output: - // Converted the Char value 'a' to the UInt16 value 97. - // Converted the Char value 'z' to the UInt16 value 122. - // Converted the Char value '' to the UInt16 value 7. - // Converted the Char value '?' to the UInt16 value 1023. - // Converted the Char value '?' to the UInt16 value 32767. - // Converted the Char value '?' to the UInt16 value 65534. - // - } + foreach (char ch in chars) + { + try + { + result = Convert.ToUInt16(ch); + Console.WriteLine($"Converted the {ch.GetType().Name} value '{ch}' to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"Unable to convert u+{((int)ch).ToString("X4")} to a UInt16."); + } + } + // The example displays the following output: + // Converted the Char value 'a' to the UInt16 value 97. + // Converted the Char value 'z' to the UInt16 value 122. + // Converted the Char value '' to the UInt16 value 7. + // Converted the Char value '?' to the UInt16 value 1023. + // Converted the Char value '?' to the UInt16 value 32767. + // Converted the Char value '?' to the UInt16 value 65534. + // + } - private static void ConvertDecimal() - { - // - decimal[] numbers = { Decimal.MinValue, -1034.23m, -12m, 0m, 147m, - 9214.16m, Decimal.MaxValue }; - ushort result; + private static void ConvertDecimal() + { + // + decimal[] numbers = { decimal.MinValue, -1034.23m, -12m, 0m, 147m, + 9214.16m, decimal.MaxValue }; + ushort result; - foreach (decimal number in numbers) - { - try { - result = Convert.ToUInt16(number); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) - { - Console.WriteLine("{0} is outside the range of the UInt16 type.", - number); - } - } - // The example displays the following output: - // -79228162514264337593543950335 is outside the range of the UInt16 type. - // -1034.23 is outside the range of the UInt16 type. - // -12 is outside the range of the UInt16 type. - // Converted the Decimal value '0' to the UInt16 value 0. - // Converted the Decimal value '147' to the UInt16 value 147. - // Converted the Decimal value '9214.16' to the UInt16 value 9214. - // 79228162514264337593543950335 is outside the range of the UInt16 type. - // - } + foreach (decimal number in numbers) + { + try + { + result = Convert.ToUInt16(number); + Console.WriteLine($"Converted the {number.GetType().Name} value '{number}' to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"{number} is outside the range of the UInt16 type."); + } + } + // The example displays the following output: + // -79228162514264337593543950335 is outside the range of the UInt16 type. + // -1034.23 is outside the range of the UInt16 type. + // -12 is outside the range of the UInt16 type. + // Converted the Decimal value '0' to the UInt16 value 0. + // Converted the Decimal value '147' to the UInt16 value 147. + // Converted the Decimal value '9214.16' to the UInt16 value 9214. + // 79228162514264337593543950335 is outside the range of the UInt16 type. + // + } - private static void ConvertDouble() - { - // - double[] numbers = { Double.MinValue, -1.38e10, -1023.299, -12.98, - 0, 9.113e-16, 103.919, 17834.191, Double.MaxValue }; - ushort result; + private static void ConvertDouble() + { + // + double[] numbers = { double.MinValue, -1.38e10, -1023.299, -12.98, + 0, 9.113e-16, 103.919, 17834.191, double.MaxValue }; + ushort result; - foreach (double number in numbers) - { - try { - result = Convert.ToUInt16(number); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) - { - Console.WriteLine("{0} is outside the range of the UInt16 type.", number); - } - } - // The example displays the following output: - // -1.79769313486232E+308 is outside the range of the UInt16 type. - // -13800000000 is outside the range of the UInt16 type. - // -1023.299 is outside the range of the UInt16 type. - // -12.98 is outside the range of the UInt16 type. - // Converted the Double value '0' to the UInt16 value 0. - // Converted the Double value '9.113E-16' to the UInt16 value 0. - // Converted the Double value '103.919' to the UInt16 value 104. - // Converted the Double value '17834.191' to the UInt16 value 17834. - // 1.79769313486232E+308 is outside the range of the UInt16 type. - // - } + foreach (double number in numbers) + { + try + { + result = Convert.ToUInt16(number); + Console.WriteLine($"Converted the {number.GetType().Name} value '{number}' to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"{number} is outside the range of the UInt16 type."); + } + } + // The example displays the following output: + // -1.79769313486232E+308 is outside the range of the UInt16 type. + // -13800000000 is outside the range of the UInt16 type. + // -1023.299 is outside the range of the UInt16 type. + // -12.98 is outside the range of the UInt16 type. + // Converted the Double value '0' to the UInt16 value 0. + // Converted the Double value '9.113E-16' to the UInt16 value 0. + // Converted the Double value '103.919' to the UInt16 value 104. + // Converted the Double value '17834.191' to the UInt16 value 17834. + // 1.79769313486232E+308 is outside the range of the UInt16 type. + // + } - private static void ConvertInt16() - { - // - short[] numbers = { Int16.MinValue, -132, 0, 121, 16103, Int16.MaxValue }; - ushort result; + private static void ConvertInt16() + { + // + short[] numbers = { short.MinValue, -132, 0, 121, 16103, short.MaxValue }; + ushort result; - foreach (short number in numbers) - { - try { - result = Convert.ToUInt16(number); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the UInt16 type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // The Int16 value -32768 is outside the range of the UInt16 type. - // The Int16 value -132 is outside the range of the UInt16 type. - // Converted the Int16 value '0' to the UInt16 value 0. - // Converted the Int16 value '121' to the UInt16 value 121. - // Converted the Int16 value '16103' to the UInt16 value 16103. - // Converted the Int16 value '32767' to the UInt16 value 32767. - // - } + foreach (short number in numbers) + { + try + { + result = Convert.ToUInt16(number); + Console.WriteLine($"Converted the {number.GetType().Name} value '{number}' to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the UInt16 type."); + } + } + // The example displays the following output: + // The Int16 value -32768 is outside the range of the UInt16 type. + // The Int16 value -132 is outside the range of the UInt16 type. + // Converted the Int16 value '0' to the UInt16 value 0. + // Converted the Int16 value '121' to the UInt16 value 121. + // Converted the Int16 value '16103' to the UInt16 value 16103. + // Converted the Int16 value '32767' to the UInt16 value 32767. + // + } - private static void ConvertInt32() - { - // - int[] numbers = { Int32.MinValue, -1, 0, 121, 340, Int32.MaxValue }; - ushort result; + private static void ConvertInt32() + { + // + int[] numbers = { int.MinValue, -1, 0, 121, 340, int.MaxValue }; + ushort result; - foreach (int number in numbers) - { - try { - result = Convert.ToUInt16(number); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the UInt16 type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // The Int32 value -2147483648 is outside the range of the UInt16 type. - // The Int32 value -1 is outside the range of the UInt16 type. - // Converted the Int32 value '0' to the UInt16 value 0. - // Converted the Int32 value '121' to the UInt16 value 121. - // Converted the Int32 value '340' to the UInt16 value 340. - // The Int32 value 2147483647 is outside the range of the UInt16 type. - // - } + foreach (int number in numbers) + { + try + { + result = Convert.ToUInt16(number); + Console.WriteLine($"Converted the {number.GetType().Name} value '{number}' to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the UInt16 type."); + } + } + // The example displays the following output: + // The Int32 value -2147483648 is outside the range of the UInt16 type. + // The Int32 value -1 is outside the range of the UInt16 type. + // Converted the Int32 value '0' to the UInt16 value 0. + // Converted the Int32 value '121' to the UInt16 value 121. + // Converted the Int32 value '340' to the UInt16 value 340. + // The Int32 value 2147483647 is outside the range of the UInt16 type. + // + } - private static void ConvertInt64() - { - // - long[] numbers = { Int64.MinValue, -1, 0, 121, 340, Int64.MaxValue }; - ushort result; + private static void ConvertInt64() + { + // + long[] numbers = { long.MinValue, -1, 0, 121, 340, long.MaxValue }; + ushort result; - foreach (long number in numbers) - { - try { - result = Convert.ToUInt16(number); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the UInt16 type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // The Int64 value -9223372036854775808 is outside the range of the UInt16 type. - // The Int64 value -1 is outside the range of the UInt16 type. - // Converted the Int64 value '0' to the UInt16 value 0. - // Converted the Int64 value '121' to the UInt16 value 121. - // Converted the Int64 value '340' to the UInt16 value 340. - // The Int64 value 9223372036854775807 is outside the range of the UInt16 type. - // - } + foreach (long number in numbers) + { + try + { + result = Convert.ToUInt16(number); + Console.WriteLine($"Converted the {number.GetType().Name} value '{number}' to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the UInt16 type."); + } + } + // The example displays the following output: + // The Int64 value -9223372036854775808 is outside the range of the UInt16 type. + // The Int64 value -1 is outside the range of the UInt16 type. + // Converted the Int64 value '0' to the UInt16 value 0. + // Converted the Int64 value '121' to the UInt16 value 121. + // Converted the Int64 value '340' to the UInt16 value 340. + // The Int64 value 9223372036854775807 is outside the range of the UInt16 type. + // + } - private static void ConvertObject() - { - // - object[] values= { true, -12, 163, 935, 'x', new DateTime(2009, 5, 12), + private static void ConvertObject() + { + // + object[] values = { true, -12, 163, 935, 'x', new DateTime(2009, 5, 12), "104", "103.0", "-1", "1.00e2", "One", 1.00e2}; - ushort result; + ushort result; - foreach (object value in values) - { - try { - result = Convert.ToUInt16(value); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - value.GetType().Name, value, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the UInt16 type.", - value.GetType().Name, value); - } - catch (FormatException) { - Console.WriteLine("The {0} value {1} is not in a recognizable format.", - value.GetType().Name, value); - } - catch (InvalidCastException) { - Console.WriteLine("No conversion to a UInt16 exists for the {0} value {1}.", - value.GetType().Name, value); - } - } - // The example displays the following output: - // Converted the Boolean value 'True' to the UInt16 value 1. - // The Int32 value -12 is outside the range of the UInt16 type. - // Converted the Int32 value '163' to the UInt16 value 163. - // Converted the Int32 value '935' to the UInt16 value 935. - // Converted the Char value 'x' to the UInt16 value 120. - // No conversion to a UInt16 exists for the DateTime value 5/12/2009 12:00:00 AM. - // Converted the String value '104' to the UInt16 value 104. - // The String value 103.0 is not in a recognizable format. - // The String value -1 is outside the range of the UInt16 type. - // The String value 1.00e2 is not in a recognizable format. - // The String value One is not in a recognizable format. - // Converted the Double value '100' to the UInt16 value 100. - // - } + foreach (object value in values) + { + try + { + result = Convert.ToUInt16(value); + Console.WriteLine($"Converted the {value.GetType().Name} value '{value}' to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {value.GetType().Name} value {value} is outside the range of the UInt16 type."); + } + catch (FormatException) + { + Console.WriteLine($"The {value.GetType().Name} value {value} is not in a recognizable format."); + } + catch (InvalidCastException) + { + Console.WriteLine($"No conversion to a UInt16 exists for the {value.GetType().Name} value {value}."); + } + } + // The example displays the following output: + // Converted the Boolean value 'True' to the UInt16 value 1. + // The Int32 value -12 is outside the range of the UInt16 type. + // Converted the Int32 value '163' to the UInt16 value 163. + // Converted the Int32 value '935' to the UInt16 value 935. + // Converted the Char value 'x' to the UInt16 value 120. + // No conversion to a UInt16 exists for the DateTime value 5/12/2009 12:00:00 AM. + // Converted the String value '104' to the UInt16 value 104. + // The String value 103.0 is not in a recognizable format. + // The String value -1 is outside the range of the UInt16 type. + // The String value 1.00e2 is not in a recognizable format. + // The String value One is not in a recognizable format. + // Converted the Double value '100' to the UInt16 value 100. + // + } - private static void ConvertSByte() - { - // - sbyte[] numbers = { SByte.MinValue, -1, 0, 10, SByte.MaxValue }; - ushort result; + private static void ConvertSByte() + { + // + sbyte[] numbers = { sbyte.MinValue, -1, 0, 10, sbyte.MaxValue }; + ushort result; - foreach (sbyte number in numbers) - { - try { - result = Convert.ToUInt16(number); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the UInt16 type.", number); - } - } - // The example displays the following output: - // -128 is outside the range of the UInt16 type. - // -1 is outside the range of the UInt16 type. - // Converted the SByte value '0' to the UInt16 value 0. - // Converted the SByte value '10' to the UInt16 value 10. - // Converted the SByte value '127' to the UInt16 value 127. - // - } + foreach (sbyte number in numbers) + { + try + { + result = Convert.ToUInt16(number); + Console.WriteLine($"Converted the {number.GetType().Name} value '{number}' to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"{number} is outside the range of the UInt16 type."); + } + } + // The example displays the following output: + // -128 is outside the range of the UInt16 type. + // -1 is outside the range of the UInt16 type. + // Converted the SByte value '0' to the UInt16 value 0. + // Converted the SByte value '10' to the UInt16 value 10. + // Converted the SByte value '127' to the UInt16 value 127. + // + } - private static void ConvertSingle() - { - // - float[] numbers = { Single.MinValue, -1.38e10f, -1023.299f, -12.98f, - 0f, 9.113e-16f, 103.919f, 17834.191f, Single.MaxValue }; - ushort result; + private static void ConvertSingle() + { + // + float[] numbers = { float.MinValue, -1.38e10f, -1023.299f, -12.98f, + 0f, 9.113e-16f, 103.919f, 17834.191f, float.MaxValue }; + ushort result; - foreach (float number in numbers) - { - try { - result = Convert.ToUInt16(number); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the UInt16 type.", number); - } - } - // The example displays the following output: - // -3.402823E+38 is outside the range of the UInt16 type. - // -1.38E+10 is outside the range of the UInt16 type. - // -1023.299 is outside the range of the UInt16 type. - // -12.98 is outside the range of the UInt16 type. - // Converted the Single value '0' to the UInt16 value 0. - // Converted the Single value '9.113E-16' to the UInt16 value 0. - // Converted the Single value '103.919' to the UInt16 value 104. - // Converted the Single value '17834.19' to the UInt16 value 17834. - // 3.402823E+38 is outside the range of the UInt16 type. - // - } + foreach (float number in numbers) + { + try + { + result = Convert.ToUInt16(number); + Console.WriteLine($"Converted the {number.GetType().Name} value '{number}' to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"{number} is outside the range of the UInt16 type."); + } + } + // The example displays the following output: + // -3.402823E+38 is outside the range of the UInt16 type. + // -1.38E+10 is outside the range of the UInt16 type. + // -1023.299 is outside the range of the UInt16 type. + // -12.98 is outside the range of the UInt16 type. + // Converted the Single value '0' to the UInt16 value 0. + // Converted the Single value '9.113E-16' to the UInt16 value 0. + // Converted the Single value '103.919' to the UInt16 value 104. + // Converted the Single value '17834.19' to the UInt16 value 17834. + // 3.402823E+38 is outside the range of the UInt16 type. + // + } - private static void ConvertString() - { - // - string[] values = { "1603", "1,603", "one", "1.6e03", "1.2e-02", + private static void ConvertString() + { + // + string[] values = { "1603", "1,603", "one", "1.6e03", "1.2e-02", "-1326", "1074122" }; - ushort result; + ushort result; - foreach (string value in values) - { - try { - result = Convert.ToUInt16(value); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - value.GetType().Name, value, - result.GetType().Name, result); - } - catch (FormatException) { - Console.WriteLine("The {0} value {1} is not in a recognizable format.", - value.GetType().Name, value); - } - catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the UInt16 type.", value); - } - } - // The example displays the following output: - // Converted the String value '1603' to the UInt16 value 1603. - // The String value 1,603 is not in a recognizable format. - // The String value one is not in a recognizable format. - // The String value 1.6e03 is not in a recognizable format. - // The String value 1.2e-02 is not in a recognizable format. - // -1326 is outside the range of the UInt16 type. - // 1074122 is outside the range of the UInt16 type. - // - } + foreach (string value in values) + { + try + { + result = Convert.ToUInt16(value); + Console.WriteLine($"Converted the {value.GetType().Name} value '{value}' to the {result.GetType().Name} value {result}."); + } + catch (FormatException) + { + Console.WriteLine($"The {value.GetType().Name} value {value} is not in a recognizable format."); + } + catch (OverflowException) + { + Console.WriteLine($"{value} is outside the range of the UInt16 type."); + } + } + // The example displays the following output: + // Converted the String value '1603' to the UInt16 value 1603. + // The String value 1,603 is not in a recognizable format. + // The String value one is not in a recognizable format. + // The String value 1.6e03 is not in a recognizable format. + // The String value 1.2e-02 is not in a recognizable format. + // -1326 is outside the range of the UInt16 type. + // 1074122 is outside the range of the UInt16 type. + // + } - private static void ConvertUInt32() - { - // - uint[] numbers = { UInt32.MinValue, 121, 340, UInt32.MaxValue }; - ushort result; + private static void ConvertUInt32() + { + // + uint[] numbers = { uint.MinValue, 121, 340, uint.MaxValue }; + ushort result; - foreach (uint number in numbers) - { - try { - result = Convert.ToUInt16(number); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the UInt16 type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // Converted the UInt32 value '0' to the UInt16 value 0. - // Converted the UInt32 value '121' to the UInt16 value 121. - // Converted the UInt32 value '340' to the UInt16 value 340. - // The UInt32 value 4294967295 is outside the range of the UInt16 type. - // - } + foreach (uint number in numbers) + { + try + { + result = Convert.ToUInt16(number); + Console.WriteLine($"Converted the {number.GetType().Name} value '{number}' to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the UInt16 type."); + } + } + // The example displays the following output: + // Converted the UInt32 value '0' to the UInt16 value 0. + // Converted the UInt32 value '121' to the UInt16 value 121. + // Converted the UInt32 value '340' to the UInt16 value 340. + // The UInt32 value 4294967295 is outside the range of the UInt16 type. + // + } - private static void ConvertUInt64() - { - // - ulong[] numbers = { UInt64.MinValue, 121, 340, UInt64.MaxValue }; - ushort result; + private static void ConvertUInt64() + { + // + ulong[] numbers = { ulong.MinValue, 121, 340, ulong.MaxValue }; + ushort result; - foreach (ulong number in numbers) - { - try { - result = Convert.ToUInt16(number); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the UInt16 type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // Converted the UInt64 value '0' to the UInt16 value 0. - // Converted the UInt64 value '121' to the UInt16 value 121. - // Converted the UInt64 value '340' to the UInt16 value 340. - // The UInt64 value 18446744073709551615 is outside the range of the UInt16 type. - // - } + foreach (ulong number in numbers) + { + try + { + result = Convert.ToUInt16(number); + Console.WriteLine($"Converted the {number.GetType().Name} value '{number}' to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the UInt16 type."); + } + } + // The example displays the following output: + // Converted the UInt64 value '0' to the UInt16 value 0. + // Converted the UInt64 value '121' to the UInt16 value 121. + // Converted the UInt64 value '340' to the UInt16 value 340. + // The UInt64 value 18446744073709551615 is outside the range of the UInt16 type. + // + } } diff --git a/snippets/csharp/System/Convert/ToUInt16/touint16_2.cs b/snippets/csharp/System/Convert/ToUInt16/touint16_2.cs index 89854b09ac7..a979ea9786d 100644 --- a/snippets/csharp/System/Convert/ToUInt16/touint16_2.cs +++ b/snippets/csharp/System/Convert/ToUInt16/touint16_2.cs @@ -3,28 +3,31 @@ public class Example { - public static void Main() - { - string[] hexStrings = { "8000", "0FFF", "f000", "00A30", "D", "-13", + public static void Main() + { + string[] hexStrings = { "8000", "0FFF", "f000", "00A30", "D", "-13", "9AC61", "GAD" }; - foreach (string hexString in hexStrings) - { - try { - ushort number = Convert.ToUInt16(hexString, 16); - Console.WriteLine("Converted '{0}' to {1:N0}.", hexString, number); - } - catch (FormatException) { - Console.WriteLine("'{0}' is not in the correct format for a hexadecimal number.", - hexString); - } - catch (OverflowException) { - Console.WriteLine("'{0}' is outside the range of an Int16.", hexString); - } - catch (ArgumentException) { - Console.WriteLine("'{0}' is invalid in base 16.", hexString); - } - } - } + foreach (string hexString in hexStrings) + { + try + { + ushort number = Convert.ToUInt16(hexString, 16); + Console.WriteLine($"Converted '{hexString}' to {number:N0}."); + } + catch (FormatException) + { + Console.WriteLine($"'{hexString}' is not in the correct format for a hexadecimal number."); + } + catch (OverflowException) + { + Console.WriteLine($"'{hexString}' is outside the range of an Int16."); + } + catch (ArgumentException) + { + Console.WriteLine($"'{hexString}' is invalid in base 16."); + } + } + } } // The example displays the following output: // Converted '8000' to 32,768. diff --git a/snippets/csharp/System/Convert/ToUInt16/touint16_3.cs b/snippets/csharp/System/Convert/ToUInt16/touint16_3.cs index 5941a9ab04b..e5437da0abe 100644 --- a/snippets/csharp/System/Convert/ToUInt16/touint16_3.cs +++ b/snippets/csharp/System/Convert/ToUInt16/touint16_3.cs @@ -3,260 +3,260 @@ using System.Globalization; using System.Text.RegularExpressions; -public enum SignBit { Negative=-1, Zero=0, Positive=1 }; +public enum SignBit { Negative = -1, Zero = 0, Positive = 1 }; public struct HexString : IConvertible { - private SignBit signBit; - private string hexString; + private SignBit signBit; + private string hexString; - public SignBit Sign + public SignBit Sign { - set { signBit = value; } - get { return signBit; } + set => signBit = value; + get => signBit; } - public string Value - { - set { - if (value.Trim().Length > 4) - throw new ArgumentException("The string representation of a 160bit integer cannot have more than four characters."); - else if (!Regex.IsMatch(value, "([0-9,A-F]){1,4}", RegexOptions.IgnoreCase)) - throw new ArgumentException("The hexadecimal representation of a 16-bit integer contains invalid characters."); - else - hexString = value; - } - get { return hexString; } - } + public string Value + { + set + { + if (value.Trim().Length > 4) + throw new ArgumentException("The string representation of a 160bit integer cannot have more than four characters."); + else if (!Regex.IsMatch(value, "([0-9,A-F]){1,4}", RegexOptions.IgnoreCase)) + throw new ArgumentException("The hexadecimal representation of a 16-bit integer contains invalid characters."); + else + hexString = value; + } + get => hexString; + } - // IConvertible implementations. - public TypeCode GetTypeCode() { - return TypeCode.Object; - } + // IConvertible implementations. + public TypeCode GetTypeCode() => TypeCode.Object; - public bool ToBoolean(IFormatProvider provider) - { - return signBit != SignBit.Zero; - } + public bool ToBoolean(IFormatProvider provider) => signBit != SignBit.Zero; - public byte ToByte(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - throw new OverflowException(String.Format("{0} is out of range of the Byte type.", Convert.ToInt16(hexString, 16))); - else - try { - return Convert.ToByte(UInt16.Parse(hexString, NumberStyles.HexNumber)); - } - catch (OverflowException e) { - throw new OverflowException(String.Format("{0} is out of range of the UInt16 type.", Convert.ToUInt16(hexString, 16)), e); - } - } + public byte ToByte(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + throw new OverflowException($"{Convert.ToInt16(hexString, 16)} is out of range of the Byte type."); + else + try + { + return Convert.ToByte(ushort.Parse(hexString, NumberStyles.HexNumber)); + } + catch (OverflowException e) + { + throw new OverflowException($"{Convert.ToUInt16(hexString, 16)} is out of range of the UInt16 type.", e); + } + } - public char ToChar(IFormatProvider provider) - { - if (signBit == SignBit.Negative) { - throw new OverflowException(String.Format("{0} is out of range of the Char type.", Convert.ToInt16(hexString, 16))); - } + public char ToChar(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + { + throw new OverflowException($"{Convert.ToInt16(hexString, 16)} is out of range of the Char type."); + } - UInt16 codePoint = UInt16.Parse(this.hexString, NumberStyles.HexNumber); - return Convert.ToChar(codePoint); - } + ushort codePoint = ushort.Parse(this.hexString, NumberStyles.HexNumber); + return Convert.ToChar(codePoint); + } - public DateTime ToDateTime(IFormatProvider provider) - { - throw new InvalidCastException("Hexadecimal to DateTime conversion is not supported."); - } + public DateTime ToDateTime(IFormatProvider provider) => throw new InvalidCastException("Hexadecimal to DateTime conversion is not supported."); - public decimal ToDecimal(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - { - short hexValue = Int16.Parse(hexString, NumberStyles.HexNumber); - return Convert.ToDecimal(hexValue); - } - else - { - ushort hexValue = UInt16.Parse(hexString, NumberStyles.HexNumber); - return Convert.ToDecimal(hexValue); - } - } + public decimal ToDecimal(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + { + short hexValue = short.Parse(hexString, NumberStyles.HexNumber); + return Convert.ToDecimal(hexValue); + } + else + { + ushort hexValue = ushort.Parse(hexString, NumberStyles.HexNumber); + return Convert.ToDecimal(hexValue); + } + } - public double ToDouble(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - return Convert.ToDouble(Int16.Parse(hexString, NumberStyles.HexNumber)); - else - return Convert.ToDouble(UInt16.Parse(hexString, NumberStyles.HexNumber)); - } + public double ToDouble(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + return Convert.ToDouble(short.Parse(hexString, NumberStyles.HexNumber)); + else + return Convert.ToDouble(ushort.Parse(hexString, NumberStyles.HexNumber)); + } - public short ToInt16(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - return Int16.Parse(hexString, NumberStyles.HexNumber); - else - try { - return Convert.ToInt16(UInt16.Parse(hexString, NumberStyles.HexNumber)); - } - catch (OverflowException e) { - throw new OverflowException(String.Format("{0} is out of range of the Int16 type.", - Convert.ToUInt16(hexString, 16)), e); - } - } + public short ToInt16(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + return short.Parse(hexString, NumberStyles.HexNumber); + else + try + { + return Convert.ToInt16(ushort.Parse(hexString, NumberStyles.HexNumber)); + } + catch (OverflowException e) + { + throw new OverflowException($"{Convert.ToUInt16(hexString, 16)} is out of range of the Int16 type.", e); + } + } - public int ToInt32(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - return Convert.ToInt32(Int16.Parse(hexString, NumberStyles.HexNumber)); - else - return Convert.ToInt32(UInt16.Parse(hexString, NumberStyles.HexNumber)); - } + public int ToInt32(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + return Convert.ToInt32(short.Parse(hexString, NumberStyles.HexNumber)); + else + return Convert.ToInt32(ushort.Parse(hexString, NumberStyles.HexNumber)); + } - public long ToInt64(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - return Convert.ToInt64(Int16.Parse(hexString, NumberStyles.HexNumber)); - else - return Int64.Parse(hexString, NumberStyles.HexNumber); - } + public long ToInt64(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + return Convert.ToInt64(short.Parse(hexString, NumberStyles.HexNumber)); + else + return long.Parse(hexString, NumberStyles.HexNumber); + } - public sbyte ToSByte(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - try { - return Convert.ToSByte(Int16.Parse(hexString, NumberStyles.HexNumber)); - } - catch (OverflowException e) { - throw new OverflowException(String.Format("{0} is outside the range of the SByte type.", - Int16.Parse(hexString, NumberStyles.HexNumber), e)); - } - else - try { - return Convert.ToSByte(UInt16.Parse(hexString, NumberStyles.HexNumber)); - } - catch (OverflowException e) { - throw new OverflowException(String.Format("{0} is outside the range of the SByte type.", - UInt16.Parse(hexString, NumberStyles.HexNumber)), e); - } - } + public sbyte ToSByte(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + try + { + return Convert.ToSByte(short.Parse(hexString, NumberStyles.HexNumber)); + } + catch (OverflowException e) + { + throw new OverflowException(string.Format("{0} is outside the range of the SByte type.", + short.Parse(hexString, NumberStyles.HexNumber), e)); + } + else + try + { + return Convert.ToSByte(ushort.Parse(hexString, NumberStyles.HexNumber)); + } + catch (OverflowException e) + { + throw new OverflowException($"{ushort.Parse(hexString, NumberStyles.HexNumber)} is outside the range of the SByte type.", e); + } + } - public float ToSingle(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - return Convert.ToSingle(Int16.Parse(hexString, NumberStyles.HexNumber)); - else - return Convert.ToSingle(UInt16.Parse(hexString, NumberStyles.HexNumber)); - } + public float ToSingle(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + return Convert.ToSingle(short.Parse(hexString, NumberStyles.HexNumber)); + else + return Convert.ToSingle(ushort.Parse(hexString, NumberStyles.HexNumber)); + } - public string ToString(IFormatProvider provider) - { - return "0x" + this.hexString; - } + public string ToString(IFormatProvider provider) => "0x" + this.hexString; - public object ToType(Type conversionType, IFormatProvider provider) - { - switch (Type.GetTypeCode(conversionType)) - { - case TypeCode.Boolean: - return this.ToBoolean(null); - case TypeCode.Byte: - return this.ToByte(null); - case TypeCode.Char: - return this.ToChar(null); - case TypeCode.DateTime: - return this.ToDateTime(null); - case TypeCode.Decimal: - return this.ToDecimal(null); - case TypeCode.Double: - return this.ToDouble(null); - case TypeCode.Int16: - return this.ToInt16(null); - case TypeCode.Int32: - return this.ToInt32(null); - case TypeCode.Int64: - return this.ToInt64(null); - case TypeCode.Object: - if (typeof(HexString).Equals(conversionType)) - return this; - else - throw new InvalidCastException(String.Format("Conversion to a {0} is not supported.", conversionType.Name)); - case TypeCode.SByte: - return this.ToSByte(null); - case TypeCode.Single: - return this.ToSingle(null); - case TypeCode.String: - return this.ToString(null); - case TypeCode.UInt16: - return this.ToUInt16(null); - case TypeCode.UInt32: - return this.ToUInt32(null); - case TypeCode.UInt64: - return this.ToUInt64(null); - default: - throw new InvalidCastException(String.Format("Conversion to {0} is not supported.", conversionType.Name)); - } - } + public object ToType(Type conversionType, IFormatProvider provider) + { + switch (Type.GetTypeCode(conversionType)) + { + case TypeCode.Boolean: + return this.ToBoolean(null); + case TypeCode.Byte: + return this.ToByte(null); + case TypeCode.Char: + return this.ToChar(null); + case TypeCode.DateTime: + return this.ToDateTime(null); + case TypeCode.Decimal: + return this.ToDecimal(null); + case TypeCode.Double: + return this.ToDouble(null); + case TypeCode.Int16: + return this.ToInt16(null); + case TypeCode.Int32: + return this.ToInt32(null); + case TypeCode.Int64: + return this.ToInt64(null); + case TypeCode.Object: + if (typeof(HexString).Equals(conversionType)) + return this; + else + throw new InvalidCastException($"Conversion to a {conversionType.Name} is not supported."); + case TypeCode.SByte: + return this.ToSByte(null); + case TypeCode.Single: + return this.ToSingle(null); + case TypeCode.String: + return this.ToString(null); + case TypeCode.UInt16: + return this.ToUInt16(null); + case TypeCode.UInt32: + return this.ToUInt32(null); + case TypeCode.UInt64: + return this.ToUInt64(null); + default: + throw new InvalidCastException($"Conversion to {conversionType.Name} is not supported."); + } + } - public UInt16 ToUInt16(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - throw new OverflowException(String.Format("{0} is outside the range of the UInt16 type.", - Int16.Parse(hexString, NumberStyles.HexNumber))); - else - return UInt16.Parse(hexString, NumberStyles.HexNumber); - } + public ushort ToUInt16(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + throw new OverflowException($"{short.Parse(hexString, NumberStyles.HexNumber)} is outside the range of the UInt16 type."); + else + return ushort.Parse(hexString, NumberStyles.HexNumber); + } - public UInt32 ToUInt32(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - throw new OverflowException(String.Format("{0} is outside the range of the UInt32 type.", - Int16.Parse(hexString, NumberStyles.HexNumber))); - else - return Convert.ToUInt32(hexString, 16); - } + public uint ToUInt32(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + throw new OverflowException($"{short.Parse(hexString, NumberStyles.HexNumber)} is outside the range of the UInt32 type."); + else + return Convert.ToUInt32(hexString, 16); + } - public UInt64 ToUInt64(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - throw new OverflowException(String.Format("{0} is outside the range of the UInt64 type.", - Int64.Parse(hexString, NumberStyles.HexNumber))); - else - return Convert.ToUInt64(hexString, 16); - } + public ulong ToUInt64(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + throw new OverflowException($"{long.Parse(hexString, NumberStyles.HexNumber)} is outside the range of the UInt64 type."); + else + return Convert.ToUInt64(hexString, 16); + } } // // public class Example { - public static void Main() - { - ushort positiveValue = 32000; - short negativeValue = -1; + public static void Main() + { + ushort positiveValue = 32000; + short negativeValue = -1; - HexString positiveString = new HexString(); - positiveString.Sign = (SignBit) Math.Sign(positiveValue); - positiveString.Value = positiveValue.ToString("X2"); + HexString positiveString = new() + { + Sign = (SignBit)Math.Sign(positiveValue), + Value = positiveValue.ToString("X2") + }; - HexString negativeString = new HexString(); - negativeString.Sign = (SignBit) Math.Sign(negativeValue); - negativeString.Value = negativeValue.ToString("X2"); + HexString negativeString = new() + { + Sign = (SignBit)Math.Sign(negativeValue), + Value = negativeValue.ToString("X2") + }; - try { - Console.WriteLine("0x{0} converts to {1}.", positiveString.Value, Convert.ToUInt16(positiveString)); - } - catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the UInt16 type.", - Int16.Parse(negativeString.Value, NumberStyles.HexNumber)); - } + try + { + Console.WriteLine($"0x{positiveString.Value} converts to {Convert.ToUInt16(positiveString)}."); + } + catch (OverflowException) + { + Console.WriteLine($"{short.Parse(negativeString.Value, NumberStyles.HexNumber)} is outside the range of the UInt16 type."); + } - try { - Console.WriteLine("0x{0} converts to {1}.", negativeString.Value, Convert.ToUInt16(negativeString)); - } - catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the UInt16 type.", - Int16.Parse(negativeString.Value, NumberStyles.HexNumber)); - } - } + try + { + Console.WriteLine($"0x{negativeString.Value} converts to {Convert.ToUInt16(negativeString)}."); + } + catch (OverflowException) + { + Console.WriteLine($"{short.Parse(negativeString.Value, NumberStyles.HexNumber)} is outside the range of the UInt16 type."); + } + } } // The example displays the following output: // 0x7D00 converts to 32000. diff --git a/snippets/csharp/System/Convert/ToUInt16/touint16_4.cs b/snippets/csharp/System/Convert/ToUInt16/touint16_4.cs index ce890f2ddeb..d4befa5bc8f 100644 --- a/snippets/csharp/System/Convert/ToUInt16/touint16_4.cs +++ b/snippets/csharp/System/Convert/ToUInt16/touint16_4.cs @@ -4,29 +4,33 @@ public class Example { - public static void Main() - { - // Create a NumberFormatInfo object and set several of its - // properties that apply to numbers. - NumberFormatInfo provider = new NumberFormatInfo(); - provider.PositiveSign = "pos "; - provider.NegativeSign = "neg "; + public static void Main() + { + // Create a NumberFormatInfo object and set several of its + // properties that apply to numbers. + NumberFormatInfo provider = new() + { + PositiveSign = "pos ", + NegativeSign = "neg " + }; - // Define an array of strings to convert to UInt16 values. - string[] values= { "34567", "+34567", "pos 34567", "34567.", + // Define an array of strings to convert to UInt16 values. + string[] values = { "34567", "+34567", "pos 34567", "34567.", "34567.", "65535", "65535", "65535" }; - foreach (string value in values) - { - Console.Write("{0,-12:} --> ", value); - try { - Console.WriteLine("{0,17}", Convert.ToUInt16(value, provider)); - } - catch (FormatException e) { - Console.WriteLine("{0,17}", e.GetType().Name); - } - } - } + foreach (string value in values) + { + Console.Write($"{value,-12} --> "); + try + { + Console.WriteLine($"{Convert.ToUInt16(value, provider),17}"); + } + catch (FormatException e) + { + Console.WriteLine($"{e.GetType().Name,17}"); + } + } + } } // The example displays the following output: // 34567 --> 34567 diff --git a/snippets/csharp/System/Convert/ToUInt32/touint32_1.cs b/snippets/csharp/System/Convert/ToUInt32/touint32_1.cs index 676374ed730..3ffc83334f0 100644 --- a/snippets/csharp/System/Convert/ToUInt32/touint32_1.cs +++ b/snippets/csharp/System/Convert/ToUInt32/touint32_1.cs @@ -2,435 +2,417 @@ public class Example { - public static void Main() - { - ConvertBoolean(); - Console.WriteLine("-----"); - ConvertByte(); - Console.WriteLine("-----"); - ConvertChar(); - Console.WriteLine("-----"); - ConvertDecimal(); - Console.WriteLine("-----"); - ConvertDouble(); - Console.WriteLine("-----"); - ConvertInt16(); - Console.WriteLine("-----"); - ConvertInt32(); - Console.WriteLine("-----"); - ConvertInt64(); - Console.WriteLine("-----"); - ConvertObject(); - Console.WriteLine("-----"); - ConvertSByte(); - Console.WriteLine("-----"); - ConvertSingle(); - Console.WriteLine("----"); - ConvertString(); - Console.WriteLine("-----"); - ConvertUInt16(); - Console.WriteLine("-----"); - ConvertUInt64(); + public static void Main() + { + ConvertBoolean(); + Console.WriteLine("-----"); + ConvertByte(); + Console.WriteLine("-----"); + ConvertChar(); + Console.WriteLine("-----"); + ConvertDecimal(); + Console.WriteLine("-----"); + ConvertDouble(); + Console.WriteLine("-----"); + ConvertInt16(); + Console.WriteLine("-----"); + ConvertInt32(); + Console.WriteLine("-----"); + ConvertInt64(); + Console.WriteLine("-----"); + ConvertObject(); + Console.WriteLine("-----"); + ConvertSByte(); + Console.WriteLine("-----"); + ConvertSingle(); + Console.WriteLine("----"); + ConvertString(); + Console.WriteLine("-----"); + ConvertUInt16(); + Console.WriteLine("-----"); + ConvertUInt64(); } - private static void ConvertBoolean() - { - // - bool falseFlag = false; - bool trueFlag = true; + private static void ConvertBoolean() + { + // + bool falseFlag = false; + bool trueFlag = true; - Console.WriteLine("{0} converts to {1}.", falseFlag, - Convert.ToUInt32(falseFlag)); - Console.WriteLine("{0} converts to {1}.", trueFlag, - Convert.ToUInt32(trueFlag)); - // The example displays the following output: - // False converts to 0. - // True converts to 1. - // - } + Console.WriteLine($"{falseFlag} converts to {Convert.ToUInt32(falseFlag)}."); + Console.WriteLine($"{trueFlag} converts to {Convert.ToUInt32(trueFlag)}."); + // The example displays the following output: + // False converts to 0. + // True converts to 1. + // + } - private static void ConvertByte() - { - // - byte[] bytes = { Byte.MinValue, 14, 122, Byte.MaxValue}; - uint result; + private static void ConvertByte() + { + // + byte[] bytes = { byte.MinValue, 14, 122, byte.MaxValue }; + uint result; - foreach (byte byteValue in bytes) - { - result = Convert.ToUInt32(byteValue); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - byteValue.GetType().Name, byteValue, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the Byte value 0 to the UInt32 value 0. - // Converted the Byte value 14 to the UInt32 value 14. - // Converted the Byte value 122 to the UInt32 value 122. - // Converted the Byte value 255 to the UInt32 value 255. - // - } + foreach (byte byteValue in bytes) + { + result = Convert.ToUInt32(byteValue); + Console.WriteLine($"Converted the {byteValue.GetType().Name} value {byteValue} to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the Byte value 0 to the UInt32 value 0. + // Converted the Byte value 14 to the UInt32 value 14. + // Converted the Byte value 122 to the UInt32 value 122. + // Converted the Byte value 255 to the UInt32 value 255. + // + } - private static void ConvertChar() - { - // - char[] chars = { 'a', 'z', '\u0007', '\u03FF', + private static void ConvertChar() + { + // + char[] chars = { 'a', 'z', '\u0007', '\u03FF', '\u7FFF', '\uFFFE' }; - uint result; + uint result; - foreach (char ch in chars) - { - result = Convert.ToUInt32(ch); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - ch.GetType().Name, ch, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the Char value 'a' to the UInt32 value 97. - // Converted the Char value 'z' to the UInt32 value 122. - // Converted the Char value '' to the UInt32 value 7. - // Converted the Char value 'Ͽ' to the UInt32 value 1023. - // Converted the Char value '翿' to the UInt32 value 32767. - // Converted the Char value '￾' to the UInt32 value 65534. - // - } + foreach (char ch in chars) + { + result = Convert.ToUInt32(ch); + Console.WriteLine($"Converted the {ch.GetType().Name} value '{ch}' to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the Char value 'a' to the UInt32 value 97. + // Converted the Char value 'z' to the UInt32 value 122. + // Converted the Char value '' to the UInt32 value 7. + // Converted the Char value 'Ͽ' to the UInt32 value 1023. + // Converted the Char value '翿' to the UInt32 value 32767. + // Converted the Char value '￾' to the UInt32 value 65534. + // + } - private static void ConvertDecimal() - { - // - decimal[] values= { Decimal.MinValue, -1034.23m, -12m, 0m, 147m, - 199.55m, 9214.16m, Decimal.MaxValue }; - uint result; + private static void ConvertDecimal() + { + // + decimal[] values = { decimal.MinValue, -1034.23m, -12m, 0m, 147m, + 199.55m, 9214.16m, decimal.MaxValue }; + uint result; - foreach (decimal value in values) - { - try { - result = Convert.ToUInt32(value); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - value.GetType().Name, value, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the UInt32 type.", - value.GetType().Name, value); - } - } - // The example displays the following output: - // The Decimal value -79228162514264337593543950335 is outside the range of the UInt32 type. - // The Decimal value -1034.23 is outside the range of the UInt32 type. - // The Decimal value -12 is outside the range of the UInt32 type. - // Converted the Decimal value '0' to the UInt32 value 0. - // Converted the Decimal value '147' to the UInt32 value 147. - // Converted the Decimal value '199.55' to the UInt32 value 200. - // Converted the Decimal value '9214.16' to the UInt32 value 9214. - // The Decimal value 79228162514264337593543950335 is outside the range of the UInt32 type. - // - } + foreach (decimal value in values) + { + try + { + result = Convert.ToUInt32(value); + Console.WriteLine($"Converted the {value.GetType().Name} value '{value}' to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {value.GetType().Name} value {value} is outside the range of the UInt32 type."); + } + } + // The example displays the following output: + // The Decimal value -79228162514264337593543950335 is outside the range of the UInt32 type. + // The Decimal value -1034.23 is outside the range of the UInt32 type. + // The Decimal value -12 is outside the range of the UInt32 type. + // Converted the Decimal value '0' to the UInt32 value 0. + // Converted the Decimal value '147' to the UInt32 value 147. + // Converted the Decimal value '199.55' to the UInt32 value 200. + // Converted the Decimal value '9214.16' to the UInt32 value 9214. + // The Decimal value 79228162514264337593543950335 is outside the range of the UInt32 type. + // + } - private static void ConvertDouble() - { - // - double[] values= { Double.MinValue, -1.38e10, -1023.299, -12.98, - 0, 9.113e-16, 103.919, 17834.191, Double.MaxValue }; - uint result; + private static void ConvertDouble() + { + // + double[] values = { double.MinValue, -1.38e10, -1023.299, -12.98, + 0, 9.113e-16, 103.919, 17834.191, double.MaxValue }; + uint result; - foreach (double value in values) - { - try { - result = Convert.ToUInt32(value); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - value.GetType().Name, value, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the UInt32 type.", - value.GetType().Name, value); - } - } - // The example displays the following output: - // The Double value -1.79769313486232E+308 is outside the range of the UInt32 type. - // The Double value -13800000000 is outside the range of the UInt32 type. - // The Double value -1023.299 is outside the range of the UInt32 type. - // The Double value -12.98 is outside the range of the UInt32 type. - // Converted the Double value '0' to the UInt32 value 0. - // Converted the Double value '9.113E-16' to the UInt32 value 0. - // Converted the Double value '103.919' to the UInt32 value 104. - // Converted the Double value '17834.191' to the UInt32 value 17834. - // The Double value 1.79769313486232E+308 is outside the range of the UInt32 type. - // - } + foreach (double value in values) + { + try + { + result = Convert.ToUInt32(value); + Console.WriteLine($"Converted the {value.GetType().Name} value '{value}' to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {value.GetType().Name} value {value} is outside the range of the UInt32 type."); + } + } + // The example displays the following output: + // The Double value -1.79769313486232E+308 is outside the range of the UInt32 type. + // The Double value -13800000000 is outside the range of the UInt32 type. + // The Double value -1023.299 is outside the range of the UInt32 type. + // The Double value -12.98 is outside the range of the UInt32 type. + // Converted the Double value '0' to the UInt32 value 0. + // Converted the Double value '9.113E-16' to the UInt32 value 0. + // Converted the Double value '103.919' to the UInt32 value 104. + // Converted the Double value '17834.191' to the UInt32 value 17834. + // The Double value 1.79769313486232E+308 is outside the range of the UInt32 type. + // + } - private static void ConvertInt16() - { - // - short[] numbers= { Int16.MinValue, -1, 0, 121, 340, Int16.MaxValue }; - uint result; + private static void ConvertInt16() + { + // + short[] numbers = { short.MinValue, -1, 0, 121, 340, short.MaxValue }; + uint result; - foreach (short number in numbers) - { - try { - result = Convert.ToUInt32(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the UInt32 type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // The Int16 value -32768 is outside the range of the UInt32 type. - // The Int16 value -1 is outside the range of the UInt32 type. - // Converted the Int16 value 0 to the UInt32 value 0. - // Converted the Int16 value 121 to the UInt32 value 121. - // Converted the Int16 value 340 to the UInt32 value 340. - // Converted the Int16 value 32767 to the UInt32 value 32767. - // - } + foreach (short number in numbers) + { + try + { + result = Convert.ToUInt32(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the UInt32 type."); + } + } + // The example displays the following output: + // The Int16 value -32768 is outside the range of the UInt32 type. + // The Int16 value -1 is outside the range of the UInt32 type. + // Converted the Int16 value 0 to the UInt32 value 0. + // Converted the Int16 value 121 to the UInt32 value 121. + // Converted the Int16 value 340 to the UInt32 value 340. + // Converted the Int16 value 32767 to the UInt32 value 32767. + // + } - private static void ConvertInt32() - { - // - int[] numbers = { Int32.MinValue, -1203, 0, 121, 1340, Int32.MaxValue }; - uint result; - foreach (int number in numbers) - { - try { - result = Convert.ToUInt32(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the UInt32 type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // The Int32 value -2147483648 is outside the range of the UInt32 type. - // The Int32 value -1203 is outside the range of the UInt32 type. - // Converted the Int32 value 0 to the UInt32 value 0. - // Converted the Int32 value 121 to the UInt32 value 121. - // Converted the Int32 value 1340 to the UInt32 value 1340. - // Converted the Int32 value 2147483647 to the UInt32 value 2147483647. - // - } + private static void ConvertInt32() + { + // + int[] numbers = { int.MinValue, -1203, 0, 121, 1340, int.MaxValue }; + uint result; + foreach (int number in numbers) + { + try + { + result = Convert.ToUInt32(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the UInt32 type."); + } + } + // The example displays the following output: + // The Int32 value -2147483648 is outside the range of the UInt32 type. + // The Int32 value -1203 is outside the range of the UInt32 type. + // Converted the Int32 value 0 to the UInt32 value 0. + // Converted the Int32 value 121 to the UInt32 value 121. + // Converted the Int32 value 1340 to the UInt32 value 1340. + // Converted the Int32 value 2147483647 to the UInt32 value 2147483647. + // + } - private static void ConvertInt64() - { - // - long[] numbers = { Int64.MinValue, -1, 0, 121, 340, Int64.MaxValue }; - uint result; - foreach (long number in numbers) - { - try { - result = Convert.ToUInt32(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the UInt32 type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // The Int64 value -9223372036854775808 is outside the range of the UInt32 type. - // The Int64 value -1 is outside the range of the UInt32 type. - // Converted the Int64 value 0 to the UInt32 value 0. - // Converted the Int64 value 121 to the UInt32 value 121. - // Converted the Int64 value 340 to the UInt32 value 340. - // The Int64 value 9223372036854775807 is outside the range of the UInt32 type. - // - } + private static void ConvertInt64() + { + // + long[] numbers = { long.MinValue, -1, 0, 121, 340, long.MaxValue }; + uint result; + foreach (long number in numbers) + { + try + { + result = Convert.ToUInt32(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the UInt32 type."); + } + } + // The example displays the following output: + // The Int64 value -9223372036854775808 is outside the range of the UInt32 type. + // The Int64 value -1 is outside the range of the UInt32 type. + // Converted the Int64 value 0 to the UInt32 value 0. + // Converted the Int64 value 121 to the UInt32 value 121. + // Converted the Int64 value 340 to the UInt32 value 340. + // The Int64 value 9223372036854775807 is outside the range of the UInt32 type. + // + } - private static void ConvertObject() - { - // - object[] values = { true, -12, 163, 935, 'x', new DateTime(2009, 5, 12), + private static void ConvertObject() + { + // + object[] values = { true, -12, 163, 935, 'x', new DateTime(2009, 5, 12), "104", "103.0", "-1", "1.00e2", "One", 1.00e2, 16.3e42}; - uint result; + uint result; - foreach (object value in values) - { - try { - result = Convert.ToUInt32(value); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - value.GetType().Name, value, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value '{1}' is outside the range of the UInt32 type.", - value.GetType().Name, value); - } - catch (FormatException) { - Console.WriteLine("The {0} value {1} is not in a recognizable format.", - value.GetType().Name, value); - } - catch (InvalidCastException) { - Console.WriteLine("No conversion to a UInt32 exists for the {0} value '{1}'.", - value.GetType().Name, value); - } - } - // The example displays the following output: - // Converted the Boolean value True to the UInt32 value 1. - // The Int32 value '-12' is outside the range of the UInt32 type. - // Converted the Int32 value 163 to the UInt32 value 163. - // Converted the Int32 value 935 to the UInt32 value 935. - // Converted the Char value x to the UInt32 value 120. - // No conversion to a UInt32 exists for the DateTime value '5/12/2009 12:00:00 AM'. - // Converted the String value 104 to the UInt32 value 104. - // The String value 103.0 is not in a recognizable format. - // The String value '-1' is outside the range of the UInt32 type. - // The String value 1.00e2 is not in a recognizable format. - // The String value One is not in a recognizable format. - // Converted the Double value 100 to the UInt32 value 100. - // The Double value '1.63E+43' is outside the range of the UInt32 type. - // - } + foreach (object value in values) + { + try + { + result = Convert.ToUInt32(value); + Console.WriteLine($"Converted the {value.GetType().Name} value {value} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {value.GetType().Name} value '{value}' is outside the range of the UInt32 type."); + } + catch (FormatException) + { + Console.WriteLine($"The {value.GetType().Name} value {value} is not in a recognizable format."); + } + catch (InvalidCastException) + { + Console.WriteLine($"No conversion to a UInt32 exists for the {value.GetType().Name} value '{value}'."); + } + } + // The example displays the following output: + // Converted the Boolean value True to the UInt32 value 1. + // The Int32 value '-12' is outside the range of the UInt32 type. + // Converted the Int32 value 163 to the UInt32 value 163. + // Converted the Int32 value 935 to the UInt32 value 935. + // Converted the Char value x to the UInt32 value 120. + // No conversion to a UInt32 exists for the DateTime value '5/12/2009 12:00:00 AM'. + // Converted the String value 104 to the UInt32 value 104. + // The String value 103.0 is not in a recognizable format. + // The String value '-1' is outside the range of the UInt32 type. + // The String value 1.00e2 is not in a recognizable format. + // The String value One is not in a recognizable format. + // Converted the Double value 100 to the UInt32 value 100. + // The Double value '1.63E+43' is outside the range of the UInt32 type. + // + } - private static void ConvertSByte() - { - // - sbyte[] numbers = { SByte.MinValue, -1, 0, 10, SByte.MaxValue }; - uint result; + private static void ConvertSByte() + { + // + sbyte[] numbers = { sbyte.MinValue, -1, 0, 10, sbyte.MaxValue }; + uint result; - foreach (sbyte number in numbers) - { - try { - result = Convert.ToUInt32(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the UInt32 type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // The SByte value -128 is outside the range of the UInt32 type. - // The SByte value -1 is outside the range of the UInt32 type. - // Converted the SByte value 0 to the UInt32 value 0. - // Converted the SByte value 10 to the UInt32 value 10. - // Converted the SByte value 127 to the UInt32 value 127. - // - } - - private static void ConvertSingle() - { - // - float[] values= { Single.MinValue, -1.38e10f, -1023.299f, -12.98f, - 0f, 9.113e-16f, 103.919f, 17834.191f, Single.MaxValue }; - uint result; + foreach (sbyte number in numbers) + { + try + { + result = Convert.ToUInt32(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the UInt32 type."); + } + } + // The example displays the following output: + // The SByte value -128 is outside the range of the UInt32 type. + // The SByte value -1 is outside the range of the UInt32 type. + // Converted the SByte value 0 to the UInt32 value 0. + // Converted the SByte value 10 to the UInt32 value 10. + // Converted the SByte value 127 to the UInt32 value 127. + // + } - foreach (float value in values) - { - try { - result = Convert.ToUInt32(value); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - value.GetType().Name, value, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the UInt32 type.", - value.GetType().Name, value); - } - } - // The example displays the following output: - // The Single value -3.402823E+38 is outside the range of the UInt32 type. - // The Single value -1.38E+10 is outside the range of the UInt32 type. - // The Single value -1023.299 is outside the range of the UInt32 type. - // The Single value -12.98 is outside the range of the UInt32 type. - // Converted the Single value 0 to the UInt32 value 0. - // Converted the Single value 9.113E-16 to the UInt32 value 0. - // Converted the Single value 103.919 to the UInt32 value 104. - // Converted the Single value 17834.19 to the UInt32 value 17834. - // The Single value 3.402823E+38 is outside the range of the UInt32 type. - // - } + private static void ConvertSingle() + { + // + float[] values = { float.MinValue, -1.38e10f, -1023.299f, -12.98f, + 0f, 9.113e-16f, 103.919f, 17834.191f, float.MaxValue }; + uint result; - private static void ConvertString() - { - // - string[] values = { "One", "1.34e28", "-26.87", "-18", "-6.00", - " 0", "137", "1601.9", Int32.MaxValue.ToString() }; - uint result; + foreach (float value in values) + { + try + { + result = Convert.ToUInt32(value); + Console.WriteLine($"Converted the {value.GetType().Name} value {value} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {value.GetType().Name} value {value} is outside the range of the UInt32 type."); + } + } + // The example displays the following output: + // The Single value -3.402823E+38 is outside the range of the UInt32 type. + // The Single value -1.38E+10 is outside the range of the UInt32 type. + // The Single value -1023.299 is outside the range of the UInt32 type. + // The Single value -12.98 is outside the range of the UInt32 type. + // Converted the Single value 0 to the UInt32 value 0. + // Converted the Single value 9.113E-16 to the UInt32 value 0. + // Converted the Single value 103.919 to the UInt32 value 104. + // Converted the Single value 17834.19 to the UInt32 value 17834. + // The Single value 3.402823E+38 is outside the range of the UInt32 type. + // + } - foreach (string value in values) - { - try { - result = Convert.ToUInt32(value); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - value.GetType().Name, value, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value '{1}' is outside the range of the UInt32 type.", - value.GetType().Name, value); - } - catch (FormatException) { - Console.WriteLine("The {0} value '{1}' is not in a recognizable format.", - value.GetType().Name, value); - } - } - // The example displays the following output: - // The String value 'One' is not in a recognizable format. - // The String value '1.34e28' is not in a recognizable format. - // The String value '-26.87' is not in a recognizable format. - // The String value '-18' is outside the range of the UInt32 type. - // The String value '-6.00' is not in a recognizable format. - // Converted the String value ' 0' to the UInt32 value 0. - // Converted the String value '137' to the UInt32 value 137. - // The String value '1601.9' is not in a recognizable format. - // Converted the String value '2147483647' to the UInt32 value 2147483647. - // - } + private static void ConvertString() + { + // + string[] values = { "One", "1.34e28", "-26.87", "-18", "-6.00", + " 0", "137", "1601.9", int.MaxValue.ToString() }; + uint result; - private static void ConvertUInt16() - { - // - ushort[] numbers = { UInt16.MinValue, 121, 340, UInt16.MaxValue }; - uint result; - foreach (ushort number in numbers) - { - result = Convert.ToUInt32(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the UInt16 value 0 to the UInt32 value 0. - // Converted the UInt16 value 121 to the UInt32 value 121. - // Converted the UInt16 value 340 to the UInt32 value 340. - // Converted the UInt16 value 65535 to the UInt32 value 65535. - // - } + foreach (string value in values) + { + try + { + result = Convert.ToUInt32(value); + Console.WriteLine($"Converted the {value.GetType().Name} value '{value}' to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {value.GetType().Name} value '{value}' is outside the range of the UInt32 type."); + } + catch (FormatException) + { + Console.WriteLine($"The {value.GetType().Name} value '{value}' is not in a recognizable format."); + } + } + // The example displays the following output: + // The String value 'One' is not in a recognizable format. + // The String value '1.34e28' is not in a recognizable format. + // The String value '-26.87' is not in a recognizable format. + // The String value '-18' is outside the range of the UInt32 type. + // The String value '-6.00' is not in a recognizable format. + // Converted the String value ' 0' to the UInt32 value 0. + // Converted the String value '137' to the UInt32 value 137. + // The String value '1601.9' is not in a recognizable format. + // Converted the String value '2147483647' to the UInt32 value 2147483647. + // + } - private static void ConvertUInt64() - { - // - ulong[] numbers = { UInt64.MinValue, 121, 340, UInt64.MaxValue }; - uint result; - foreach (ulong number in numbers) - { - try { + private static void ConvertUInt16() + { + // + ushort[] numbers = { ushort.MinValue, 121, 340, ushort.MaxValue }; + uint result; + foreach (ushort number in numbers) + { result = Convert.ToUInt32(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the UInt32 type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // Converted the UInt64 value 0 to the UInt32 value 0. - // Converted the UInt64 value 121 to the UInt32 value 121. - // Converted the UInt64 value 340 to the UInt32 value 340. - // The UInt64 value 18446744073709551615 is outside the range of the UInt32 type. - // - } + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the UInt16 value 0 to the UInt32 value 0. + // Converted the UInt16 value 121 to the UInt32 value 121. + // Converted the UInt16 value 340 to the UInt32 value 340. + // Converted the UInt16 value 65535 to the UInt32 value 65535. + // + } + + private static void ConvertUInt64() + { + // + ulong[] numbers = { ulong.MinValue, 121, 340, ulong.MaxValue }; + uint result; + foreach (ulong number in numbers) + { + try + { + result = Convert.ToUInt32(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the UInt32 type."); + } + } + // The example displays the following output: + // Converted the UInt64 value 0 to the UInt32 value 0. + // Converted the UInt64 value 121 to the UInt32 value 121. + // Converted the UInt64 value 340 to the UInt32 value 340. + // The UInt64 value 18446744073709551615 is outside the range of the UInt32 type. + // + } } diff --git a/snippets/csharp/System/Convert/ToUInt32/touint32_2.cs b/snippets/csharp/System/Convert/ToUInt32/touint32_2.cs index e9b9bc41e04..50f1d2ef075 100644 --- a/snippets/csharp/System/Convert/ToUInt32/touint32_2.cs +++ b/snippets/csharp/System/Convert/ToUInt32/touint32_2.cs @@ -4,33 +4,38 @@ public class Class1 { - public static void Main() - { - // Create a NumberFormatInfo object and set several of its - // properties that apply to numbers. - NumberFormatInfo provider = new NumberFormatInfo(); - provider.PositiveSign = "pos "; - provider.NegativeSign = "neg "; + public static void Main() + { + // Create a NumberFormatInfo object and set several of its + // properties that apply to numbers. + NumberFormatInfo provider = new() + { + PositiveSign = "pos ", + NegativeSign = "neg " + }; - // Define an array of numeric strings. - string[] values = { "123456789", "+123456789", "pos 123456789", + // Define an array of numeric strings. + string[] values = { "123456789", "+123456789", "pos 123456789", "123456789.", "123,456,789", "4294967295", "4294967296", "-1", "neg 1" }; - foreach (string value in values) - { - Console.Write("{0,-20} -->", value); - try { - Console.WriteLine("{0,20}", Convert.ToUInt32(value, provider)); - } - catch (FormatException) { - Console.WriteLine("{0,20}", "Bad Format"); - } - catch (OverflowException) { - Console.WriteLine("{0,20}", "Numeric Overflow"); - } - } - } + foreach (string value in values) + { + Console.Write($"{value,-20} -->"); + try + { + Console.WriteLine($"{Convert.ToUInt32(value, provider),20}"); + } + catch (FormatException) + { + Console.WriteLine($"{"Bad Format",20}"); + } + catch (OverflowException) + { + Console.WriteLine($"{"Numeric Overflow",20}"); + } + } + } } // The example displays the following output: // 123456789 --> 123456789 diff --git a/snippets/csharp/System/Convert/ToUInt32/touint32_3.cs b/snippets/csharp/System/Convert/ToUInt32/touint32_3.cs index 9964628773a..f2f13eeb6d5 100644 --- a/snippets/csharp/System/Convert/ToUInt32/touint32_3.cs +++ b/snippets/csharp/System/Convert/ToUInt32/touint32_3.cs @@ -3,30 +3,33 @@ public class Example { - public static void Main() - { - string[] hexStrings = { "80000000", "0FFFFFFF", "F0000000", "00A3000", "D", + public static void Main() + { + string[] hexStrings = { "80000000", "0FFFFFFF", "F0000000", "00A3000", "D", "-13", "9AC61", "GAD", "FFFFFFFFFF" }; - foreach (string hexString in hexStrings) - { - Console.Write("{0,-12} --> ", hexString); - try { - uint number = Convert.ToUInt32(hexString, 16); - Console.WriteLine("{0,18:N0}", number); - } - catch (FormatException) { - Console.WriteLine("{0,18}", "Bad Format"); - } - catch (OverflowException) - { - Console.WriteLine("{0,18}", "Numeric Overflow"); - } - catch (ArgumentException) { - Console.WriteLine("{0,18}", "Invalid in Base 16"); - } - } - } + foreach (string hexString in hexStrings) + { + Console.Write($"{hexString,-12} --> "); + try + { + uint number = Convert.ToUInt32(hexString, 16); + Console.WriteLine($"{number,18:N0}"); + } + catch (FormatException) + { + Console.WriteLine($"{"Bad Format",18}"); + } + catch (OverflowException) + { + Console.WriteLine($"{"Numeric Overflow",18}"); + } + catch (ArgumentException) + { + Console.WriteLine($"{"Invalid in Base 16",18}"); + } + } + } } // The example displays the following output: // 80000000 --> 2,147,483,648 diff --git a/snippets/csharp/System/Convert/ToUInt32/touint32_4.cs b/snippets/csharp/System/Convert/ToUInt32/touint32_4.cs index 19be8f73c38..0b8e47fcedb 100644 --- a/snippets/csharp/System/Convert/ToUInt32/touint32_4.cs +++ b/snippets/csharp/System/Convert/ToUInt32/touint32_4.cs @@ -3,279 +3,286 @@ using System.Globalization; using System.Text.RegularExpressions; -public enum SignBit { Negative=-1, Zero=0, Positive=1 }; +public enum SignBit { Negative = -1, Zero = 0, Positive = 1 }; public struct HexString : IConvertible { - private SignBit signBit; - private string hexString; + private SignBit signBit; + private string hexString; - public SignBit Sign + public SignBit Sign { - set { signBit = value; } - get { return signBit; } + set => signBit = value; + get => signBit; } - public string Value - { - set { - if (value.Trim().Length > 8) - throw new ArgumentException("The string representation of a 32-bit integer cannot have more than 8 characters."); - else if (!Regex.IsMatch(value, "([0-9,A-F]){1,8}", RegexOptions.IgnoreCase)) - throw new ArgumentException("The hexadecimal representation of a 32-bit integer contains invalid characters."); - else - hexString = value; - } - get { return hexString; } - } + public string Value + { + set + { + if (value.Trim().Length > 8) + throw new ArgumentException("The string representation of a 32-bit integer cannot have more than 8 characters."); + else if (!Regex.IsMatch(value, "([0-9,A-F]){1,8}", RegexOptions.IgnoreCase)) + throw new ArgumentException("The hexadecimal representation of a 32-bit integer contains invalid characters."); + else + hexString = value; + } + get => hexString; + } - // IConvertible implementations. - public TypeCode GetTypeCode() - { - return TypeCode.Object; - } + // IConvertible implementations. + public TypeCode GetTypeCode() => TypeCode.Object; - public bool ToBoolean(IFormatProvider provider) - { - return signBit != SignBit.Zero; - } + public bool ToBoolean(IFormatProvider provider) => signBit != SignBit.Zero; - public byte ToByte(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - throw new OverflowException(String.Format("{0} is out of range of the Byte type.", Convert.ToInt32(hexString, 16))); - else - try { - return Byte.Parse(hexString, NumberStyles.HexNumber); - } - catch (OverflowException e) { - throw new OverflowException(String.Format("{0} is out of range of the Byte type.", Convert.ToUInt32(hexString, 16)), e); - } - } + public byte ToByte(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + throw new OverflowException($"{Convert.ToInt32(hexString, 16)} is out of range of the Byte type."); + else + try + { + return byte.Parse(hexString, NumberStyles.HexNumber); + } + catch (OverflowException e) + { + throw new OverflowException($"{Convert.ToUInt32(hexString, 16)} is out of range of the Byte type.", e); + } + } - public char ToChar(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - throw new OverflowException(String.Format("{0} is out of range of the Char type.", Convert.ToInt32(hexString, 16))); + public char ToChar(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + throw new OverflowException($"{Convert.ToInt32(hexString, 16)} is out of range of the Char type."); - try { - ushort codePoint = UInt16.Parse(this.hexString, NumberStyles.HexNumber); - return Convert.ToChar(codePoint); - } - catch (OverflowException e) { - throw new OverflowException(String.Format("{0} is out of range of the Char type.", Convert.ToUInt32(hexString, 16)), e); - } - } + try + { + ushort codePoint = ushort.Parse(this.hexString, NumberStyles.HexNumber); + return Convert.ToChar(codePoint); + } + catch (OverflowException e) + { + throw new OverflowException($"{Convert.ToUInt32(hexString, 16)} is out of range of the Char type.", e); + } + } - public DateTime ToDateTime(IFormatProvider provider) - { - throw new InvalidCastException("Hexadecimal to DateTime conversion is not supported."); - } + public DateTime ToDateTime(IFormatProvider provider) => throw new InvalidCastException("Hexadecimal to DateTime conversion is not supported."); - public decimal ToDecimal(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - { - int hexValue = Int32.Parse(hexString, NumberStyles.HexNumber); - return Convert.ToDecimal(hexValue); - } - else - { - uint hexValue = UInt32.Parse(hexString, NumberStyles.HexNumber); - return Convert.ToDecimal(hexValue); - } - } + public decimal ToDecimal(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + { + int hexValue = int.Parse(hexString, NumberStyles.HexNumber); + return Convert.ToDecimal(hexValue); + } + else + { + uint hexValue = uint.Parse(hexString, NumberStyles.HexNumber); + return Convert.ToDecimal(hexValue); + } + } - public double ToDouble(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - return Convert.ToDouble(Int32.Parse(hexString, NumberStyles.HexNumber)); - else - return Convert.ToDouble(UInt32.Parse(hexString, NumberStyles.HexNumber)); - } + public double ToDouble(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + return Convert.ToDouble(int.Parse(hexString, NumberStyles.HexNumber)); + else + return Convert.ToDouble(uint.Parse(hexString, NumberStyles.HexNumber)); + } - public short ToInt16(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - try { - return Convert.ToInt16(Int32.Parse(hexString, NumberStyles.HexNumber)); - } - catch (OverflowException e) { - throw new OverflowException(String.Format("{0} is out of range of the Int16 type.", Convert.ToInt32(hexString, 16)), e); - } - else - try { - return Convert.ToInt16(UInt32.Parse(hexString, NumberStyles.HexNumber)); - } - catch (OverflowException e) { - throw new OverflowException(String.Format("{0} is out of range of the Int16 type.", Convert.ToUInt32(hexString, 16)), e); - } - } + public short ToInt16(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + try + { + return Convert.ToInt16(int.Parse(hexString, NumberStyles.HexNumber)); + } + catch (OverflowException e) + { + throw new OverflowException($"{Convert.ToInt32(hexString, 16)} is out of range of the Int16 type.", e); + } + else + try + { + return Convert.ToInt16(uint.Parse(hexString, NumberStyles.HexNumber)); + } + catch (OverflowException e) + { + throw new OverflowException($"{Convert.ToUInt32(hexString, 16)} is out of range of the Int16 type.", e); + } + } - public int ToInt32(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - return Int32.Parse(hexString, NumberStyles.HexNumber); - else - try { - return Convert.ToInt32(UInt32.Parse(hexString, NumberStyles.HexNumber)); - } - catch (OverflowException e) { - throw new OverflowException(String.Format("{0} is out of range of the Int32 type.", Convert.ToUInt32(hexString, 16)), e); - } - } + public int ToInt32(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + return int.Parse(hexString, NumberStyles.HexNumber); + else + try + { + return Convert.ToInt32(uint.Parse(hexString, NumberStyles.HexNumber)); + } + catch (OverflowException e) + { + throw new OverflowException($"{Convert.ToUInt32(hexString, 16)} is out of range of the Int32 type.", e); + } + } - public long ToInt64(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - return Convert.ToInt64(Int32.Parse(hexString, NumberStyles.HexNumber)); - else - return Int64.Parse(hexString, NumberStyles.HexNumber); - } + public long ToInt64(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + return Convert.ToInt64(int.Parse(hexString, NumberStyles.HexNumber)); + else + return long.Parse(hexString, NumberStyles.HexNumber); + } - public sbyte ToSByte(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - try { - return Convert.ToSByte(Int32.Parse(hexString, NumberStyles.HexNumber)); - } - catch (OverflowException e) { - throw new OverflowException(String.Format("{0} is outside the range of the SByte type.", - Int32.Parse(hexString, NumberStyles.HexNumber), e)); - } - else - try { - return Convert.ToSByte(UInt32.Parse(hexString, NumberStyles.HexNumber)); - } - catch (OverflowException e) { - throw new OverflowException(String.Format("{0} is outside the range of the SByte type.", - UInt32.Parse(hexString, NumberStyles.HexNumber)), e); - } - } + public sbyte ToSByte(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + try + { + return Convert.ToSByte(int.Parse(hexString, NumberStyles.HexNumber)); + } + catch (OverflowException e) + { + throw new OverflowException(string.Format("{0} is outside the range of the SByte type.", + int.Parse(hexString, NumberStyles.HexNumber), e)); + } + else + try + { + return Convert.ToSByte(uint.Parse(hexString, NumberStyles.HexNumber)); + } + catch (OverflowException e) + { + throw new OverflowException($"{uint.Parse(hexString, NumberStyles.HexNumber)} is outside the range of the SByte type.", e); + } + } - public float ToSingle(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - return Convert.ToSingle(Int32.Parse(hexString, NumberStyles.HexNumber)); - else - return Convert.ToSingle(UInt32.Parse(hexString, NumberStyles.HexNumber)); - } + public float ToSingle(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + return Convert.ToSingle(int.Parse(hexString, NumberStyles.HexNumber)); + else + return Convert.ToSingle(uint.Parse(hexString, NumberStyles.HexNumber)); + } - public string ToString(IFormatProvider provider) - { - return "0x" + this.hexString; - } + public string ToString(IFormatProvider provider) => "0x" + this.hexString; - public object ToType(Type conversionType, IFormatProvider provider) - { - switch (Type.GetTypeCode(conversionType)) - { - case TypeCode.Boolean: - return this.ToBoolean(null); - case TypeCode.Byte: - return this.ToByte(null); - case TypeCode.Char: - return this.ToChar(null); - case TypeCode.DateTime: - return this.ToDateTime(null); - case TypeCode.Decimal: - return this.ToDecimal(null); - case TypeCode.Double: - return this.ToDouble(null); - case TypeCode.Int16: - return this.ToInt16(null); - case TypeCode.Int32: - return this.ToInt32(null); - case TypeCode.Int64: - return this.ToInt64(null); - case TypeCode.Object: - if (typeof(HexString).Equals(conversionType)) - return this; - else - throw new InvalidCastException(String.Format("Conversion to a {0} is not supported.", conversionType.Name)); - case TypeCode.SByte: - return this.ToSByte(null); - case TypeCode.Single: - return this.ToSingle(null); - case TypeCode.String: - return this.ToString(null); - case TypeCode.UInt16: - return this.ToUInt16(null); - case TypeCode.UInt32: - return this.ToUInt32(null); - case TypeCode.UInt64: - return this.ToUInt64(null); - default: - throw new InvalidCastException(String.Format("Conversion to {0} is not supported.", conversionType.Name)); - } - } + public object ToType(Type conversionType, IFormatProvider provider) + { + switch (Type.GetTypeCode(conversionType)) + { + case TypeCode.Boolean: + return this.ToBoolean(null); + case TypeCode.Byte: + return this.ToByte(null); + case TypeCode.Char: + return this.ToChar(null); + case TypeCode.DateTime: + return this.ToDateTime(null); + case TypeCode.Decimal: + return this.ToDecimal(null); + case TypeCode.Double: + return this.ToDouble(null); + case TypeCode.Int16: + return this.ToInt16(null); + case TypeCode.Int32: + return this.ToInt32(null); + case TypeCode.Int64: + return this.ToInt64(null); + case TypeCode.Object: + if (typeof(HexString).Equals(conversionType)) + return this; + else + throw new InvalidCastException($"Conversion to a {conversionType.Name} is not supported."); + case TypeCode.SByte: + return this.ToSByte(null); + case TypeCode.Single: + return this.ToSingle(null); + case TypeCode.String: + return this.ToString(null); + case TypeCode.UInt16: + return this.ToUInt16(null); + case TypeCode.UInt32: + return this.ToUInt32(null); + case TypeCode.UInt64: + return this.ToUInt64(null); + default: + throw new InvalidCastException($"Conversion to {conversionType.Name} is not supported."); + } + } - public ushort ToUInt16(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - throw new OverflowException(String.Format("{0} is outside the range of the UInt16 type.", - Int32.Parse(hexString, NumberStyles.HexNumber))); - else - try { - return Convert.ToUInt16(UInt32.Parse(hexString, NumberStyles.HexNumber)); - } - catch (OverflowException e) { - throw new OverflowException(String.Format("{0} is out of range of the UInt16 type.", Convert.ToUInt32(hexString, 16)), e); - } - } + public ushort ToUInt16(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + throw new OverflowException($"{int.Parse(hexString, NumberStyles.HexNumber)} is outside the range of the UInt16 type."); + else + try + { + return Convert.ToUInt16(uint.Parse(hexString, NumberStyles.HexNumber)); + } + catch (OverflowException e) + { + throw new OverflowException($"{Convert.ToUInt32(hexString, 16)} is out of range of the UInt16 type.", e); + } + } - public uint ToUInt32(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - throw new OverflowException(String.Format("{0} is outside the range of the UInt32 type.", - Int32.Parse(hexString, NumberStyles.HexNumber))); - else - return Convert.ToUInt32(hexString, 16); - } + public uint ToUInt32(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + throw new OverflowException($"{int.Parse(hexString, NumberStyles.HexNumber)} is outside the range of the UInt32 type."); + else + return Convert.ToUInt32(hexString, 16); + } - public ulong ToUInt64(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - throw new OverflowException(String.Format("{0} is outside the range of the UInt64 type.", - Int32.Parse(hexString, NumberStyles.HexNumber))); - else - return Convert.ToUInt64(hexString, 16); - } + public ulong ToUInt64(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + throw new OverflowException($"{int.Parse(hexString, NumberStyles.HexNumber)} is outside the range of the UInt64 type."); + else + return Convert.ToUInt64(hexString, 16); + } } // // public class Example { - public static void Main() - { - uint positiveValue = 320000000; - int negativeValue = -1; + public static void Main() + { + uint positiveValue = 320000000; + int negativeValue = -1; - HexString positiveString = new HexString(); - positiveString.Sign = (SignBit) Math.Sign(positiveValue); - positiveString.Value = positiveValue.ToString("X4"); + HexString positiveString = new() + { + Sign = (SignBit)Math.Sign(positiveValue), + Value = positiveValue.ToString("X4") + }; - HexString negativeString = new HexString(); - negativeString.Sign = (SignBit) Math.Sign(negativeValue); - negativeString.Value = negativeValue.ToString("X4"); + HexString negativeString = new() + { + Sign = (SignBit)Math.Sign(negativeValue), + Value = negativeValue.ToString("X4") + }; - try { - Console.WriteLine("0x{0} converts to {1}.", positiveString.Value, Convert.ToUInt32(positiveString)); - } - catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the UInt32 type.", - Int32.Parse(positiveString.Value, NumberStyles.HexNumber)); - } + try + { + Console.WriteLine($"0x{positiveString.Value} converts to {Convert.ToUInt32(positiveString)}."); + } + catch (OverflowException) + { + Console.WriteLine($"{int.Parse(positiveString.Value, NumberStyles.HexNumber)} is outside the range of the UInt32 type."); + } - try { - Console.WriteLine("0x{0} converts to {1}.", negativeString.Value, Convert.ToUInt32(negativeString)); - } - catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the UInt32 type.", - Int32.Parse(negativeString.Value, NumberStyles.HexNumber)); - } - } + try + { + Console.WriteLine($"0x{negativeString.Value} converts to {Convert.ToUInt32(negativeString)}."); + } + catch (OverflowException) + { + Console.WriteLine($"{int.Parse(negativeString.Value, NumberStyles.HexNumber)} is outside the range of the UInt32 type."); + } + } } // The example dosplays the following output: // 0x1312D000 converts to 320000000. diff --git a/snippets/csharp/System/Convert/ToUInt64/touint64_1.cs b/snippets/csharp/System/Convert/ToUInt64/touint64_1.cs index 4b42a716f45..d72b395410f 100644 --- a/snippets/csharp/System/Convert/ToUInt64/touint64_1.cs +++ b/snippets/csharp/System/Convert/ToUInt64/touint64_1.cs @@ -2,439 +2,428 @@ public class Example { - public static void Main() - { - ConvertBoolean(); - Console.WriteLine("-----"); - ConvertByte(); - Console.WriteLine("-----"); - ConvertChar(); - Console.WriteLine("-----"); - ConvertDecimal(); - Console.WriteLine("-----"); - ConvertDouble(); - Console.WriteLine("-----"); - ConvertInt16(); - Console.WriteLine("-----"); - ConvertInt32(); - Console.WriteLine("-----"); - ConvertInt64(); - Console.WriteLine("-----"); - ConvertObject(); - Console.WriteLine("-----"); - ConvertSByte(); - Console.WriteLine("-----"); - ConvertSingle(); - Console.WriteLine("----"); - ConvertString(); - Console.WriteLine("-----"); - ConvertUInt16(); - Console.WriteLine("-----"); - ConvertUInt32(); + public static void Main() + { + ConvertBoolean(); + Console.WriteLine("-----"); + ConvertByte(); + Console.WriteLine("-----"); + ConvertChar(); + Console.WriteLine("-----"); + ConvertDecimal(); + Console.WriteLine("-----"); + ConvertDouble(); + Console.WriteLine("-----"); + ConvertInt16(); + Console.WriteLine("-----"); + ConvertInt32(); + Console.WriteLine("-----"); + ConvertInt64(); + Console.WriteLine("-----"); + ConvertObject(); + Console.WriteLine("-----"); + ConvertSByte(); + Console.WriteLine("-----"); + ConvertSingle(); + Console.WriteLine("----"); + ConvertString(); + Console.WriteLine("-----"); + ConvertUInt16(); + Console.WriteLine("-----"); + ConvertUInt32(); } - private static void ConvertBoolean() - { - // - bool falseFlag = false; - bool trueFlag = true; + private static void ConvertBoolean() + { + // + bool falseFlag = false; + bool trueFlag = true; - Console.WriteLine("{0} converts to {1}.", falseFlag, - Convert.ToUInt64(falseFlag)); - Console.WriteLine("{0} converts to {1}.", trueFlag, - Convert.ToUInt64(trueFlag)); - // The example displays the following output: - // False converts to 0. - // True converts to 1. - // - } + Console.WriteLine($"{falseFlag} converts to {Convert.ToUInt64(falseFlag)}."); + Console.WriteLine($"{trueFlag} converts to {Convert.ToUInt64(trueFlag)}."); + // The example displays the following output: + // False converts to 0. + // True converts to 1. + // + } - private static void ConvertByte() - { - // - byte[] bytes = { Byte.MinValue, 14, 122, Byte.MaxValue}; - ulong result; + private static void ConvertByte() + { + // + byte[] bytes = { byte.MinValue, 14, 122, byte.MaxValue }; + ulong result; - foreach (byte byteValue in bytes) - { - result = Convert.ToUInt64(byteValue); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - byteValue.GetType().Name, byteValue, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the Byte value 0 to the UInt64 value 0. - // Converted the Byte value 14 to the UInt64 value 14. - // Converted the Byte value 122 to the UInt64 value 122. - // Converted the Byte value 255 to the UInt64 value 255. - // - } + foreach (byte byteValue in bytes) + { + result = Convert.ToUInt64(byteValue); + Console.WriteLine($"Converted the {byteValue.GetType().Name} value {byteValue} to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the Byte value 0 to the UInt64 value 0. + // Converted the Byte value 14 to the UInt64 value 14. + // Converted the Byte value 122 to the UInt64 value 122. + // Converted the Byte value 255 to the UInt64 value 255. + // + } - private static void ConvertChar() - { - // - char[] chars = { 'a', 'z', '\u0007', '\u03FF', + private static void ConvertChar() + { + // + char[] chars = { 'a', 'z', '\u0007', '\u03FF', '\u7FFF', '\uFFFE' }; - ulong result; + ulong result; - foreach (char ch in chars) - { - result = Convert.ToUInt64(ch); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - ch.GetType().Name, ch, - result.GetType().Name, result); - } - // The example displays the following output: - // Converted the Char value 'a' to the UInt64 value 97. - // Converted the Char value 'z' to the UInt64 value 122. - // Converted the Char value '' to the UInt64 value 7. - // Converted the Char value '?' to the UInt64 value 1023. - // Converted the Char value '?' to the UInt64 value 32767. - // Converted the Char value '?' to the UInt64 value 65534. - // - } + foreach (char ch in chars) + { + result = Convert.ToUInt64(ch); + Console.WriteLine($"Converted the {ch.GetType().Name} value '{ch}' to the {result.GetType().Name} value {result}."); + } + // The example displays the following output: + // Converted the Char value 'a' to the UInt64 value 97. + // Converted the Char value 'z' to the UInt64 value 122. + // Converted the Char value '' to the UInt64 value 7. + // Converted the Char value '?' to the UInt64 value 1023. + // Converted the Char value '?' to the UInt64 value 32767. + // Converted the Char value '?' to the UInt64 value 65534. + // + } - private static void ConvertDecimal() - { - // - decimal[] values= { Decimal.MinValue, -1034.23m, -12m, 0m, 147m, - 199.55m, 9214.16m, Decimal.MaxValue }; - ulong result; + private static void ConvertDecimal() + { + // + decimal[] values = { decimal.MinValue, -1034.23m, -12m, 0m, 147m, + 199.55m, 9214.16m, decimal.MaxValue }; + ulong result; - foreach (decimal value in values) - { - try { - result = Convert.ToUInt64(value); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - value.GetType().Name, value, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the UInt64 type.", - value); - } - } - // The example displays the following output: - // -79228162514264337593543950335 is outside the range of the UInt64 type. - // -1034.23 is outside the range of the UInt64 type. - // -12 is outside the range of the UInt64 type. - // Converted the Decimal value '0' to the UInt64 value 0. - // Converted the Decimal value '147' to the UInt64 value 147. - // Converted the Decimal value '199.55' to the UInt64 value 200. - // Converted the Decimal value '9214.16' to the UInt64 value 9214. - // 79228162514264337593543950335 is outside the range of the UInt64 type. - // - } + foreach (decimal value in values) + { + try + { + result = Convert.ToUInt64(value); + Console.WriteLine($"Converted the {value.GetType().Name} value '{value}' to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"{value} is outside the range of the UInt64 type."); + } + } + // The example displays the following output: + // -79228162514264337593543950335 is outside the range of the UInt64 type. + // -1034.23 is outside the range of the UInt64 type. + // -12 is outside the range of the UInt64 type. + // Converted the Decimal value '0' to the UInt64 value 0. + // Converted the Decimal value '147' to the UInt64 value 147. + // Converted the Decimal value '199.55' to the UInt64 value 200. + // Converted the Decimal value '9214.16' to the UInt64 value 9214. + // 79228162514264337593543950335 is outside the range of the UInt64 type. + // + } - private static void ConvertDouble() - { - // - double[] values= { Double.MinValue, -1.38e10, -1023.299, -12.98, - 0, 9.113e-16, 103.919, 17834.191, Double.MaxValue }; - ulong result; + private static void ConvertDouble() + { + // + double[] values = { double.MinValue, -1.38e10, -1023.299, -12.98, + 0, 9.113e-16, 103.919, 17834.191, double.MaxValue }; + ulong result; - foreach (double value in values) - { - try { - result = Convert.ToUInt64(value); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - value.GetType().Name, value, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the UInt64 type.", value); - } - } - // The example displays the following output: - // -1.79769313486232E+308 is outside the range of the UInt64 type. - // -13800000000 is outside the range of the UInt64 type. - // -1023.299 is outside the range of the UInt64 type. - // -12.98 is outside the range of the UInt64 type. - // Converted the Double value '0' to the UInt64 value 0. - // Converted the Double value '9.113E-16' to the UInt64 value 0. - // Converted the Double value '103.919' to the UInt64 value 104. - // Converted the Double value '17834.191' to the UInt64 value 17834. - // 1.79769313486232E+308 is outside the range of the UInt64 type. - // - } + foreach (double value in values) + { + try + { + result = Convert.ToUInt64(value); + Console.WriteLine($"Converted the {value.GetType().Name} value '{value}' to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"{value} is outside the range of the UInt64 type."); + } + } + // The example displays the following output: + // -1.79769313486232E+308 is outside the range of the UInt64 type. + // -13800000000 is outside the range of the UInt64 type. + // -1023.299 is outside the range of the UInt64 type. + // -12.98 is outside the range of the UInt64 type. + // Converted the Double value '0' to the UInt64 value 0. + // Converted the Double value '9.113E-16' to the UInt64 value 0. + // Converted the Double value '103.919' to the UInt64 value 104. + // Converted the Double value '17834.191' to the UInt64 value 17834. + // 1.79769313486232E+308 is outside the range of the UInt64 type. + // + } - private static void ConvertInt16() - { - // - short[] numbers= { Int16.MinValue, -1, 0, 121, 340, Int16.MaxValue }; - ulong result; + private static void ConvertInt16() + { + // + short[] numbers = { short.MinValue, -1, 0, 121, 340, short.MaxValue }; + ulong result; - foreach (short number in numbers) - { - try { - result = Convert.ToUInt64(number); - Console.WriteLine("Converted the {0} value {1} to a {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the UInt64 type.", number); - } - } - // The example displays the following output: - // -32768 is outside the range of the UInt64 type. - // -1 is outside the range of the UInt64 type. - // Converted the Int16 value 0 to a UInt64 value 0. - // Converted the Int16 value 121 to a UInt64 value 121. - // Converted the Int16 value 340 to a UInt64 value 340. - // Converted the Int16 value 32767 to a UInt64 value 32767. - // - } + foreach (short number in numbers) + { + try + { + result = Convert.ToUInt64(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to a {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"{number} is outside the range of the UInt64 type."); + } + } + // The example displays the following output: + // -32768 is outside the range of the UInt64 type. + // -1 is outside the range of the UInt64 type. + // Converted the Int16 value 0 to a UInt64 value 0. + // Converted the Int16 value 121 to a UInt64 value 121. + // Converted the Int16 value 340 to a UInt64 value 340. + // Converted the Int16 value 32767 to a UInt64 value 32767. + // + } - private static void ConvertInt32() - { - // - int[] numbers = { Int32.MinValue, -1, 0, 121, 340, Int32.MaxValue }; - ulong result; + private static void ConvertInt32() + { + // + int[] numbers = { int.MinValue, -1, 0, 121, 340, int.MaxValue }; + ulong result; - foreach (int number in numbers) - { - try { - result = Convert.ToUInt64(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the UInt64 type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // The Int32 value -2147483648 is outside the range of the UInt64 type. - // The Int32 value -1 is outside the range of the UInt64 type. - // Converted the Int32 value 0 to the UInt64 value 0. - // Converted the Int32 value 121 to the UInt64 value 121. - // Converted the Int32 value 340 to the UInt64 value 340. - // Converted the Int32 value 2147483647 to the UInt64 value 2147483647. - // - } + foreach (int number in numbers) + { + try + { + result = Convert.ToUInt64(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the UInt64 type."); + } + } + // The example displays the following output: + // The Int32 value -2147483648 is outside the range of the UInt64 type. + // The Int32 value -1 is outside the range of the UInt64 type. + // Converted the Int32 value 0 to the UInt64 value 0. + // Converted the Int32 value 121 to the UInt64 value 121. + // Converted the Int32 value 340 to the UInt64 value 340. + // Converted the Int32 value 2147483647 to the UInt64 value 2147483647. + // + } - private static void ConvertInt64() - { - // - long[] numbers = { Int64.MinValue, -19432, -18, 0, 121, 340, Int64.MaxValue }; - ulong result; - foreach (long number in numbers) - { - try { - result = Convert.ToUInt64(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the UInt64 type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // The Int64 value -9223372036854775808 is outside the range of the UInt64 type. - // The Int64 value -19432 is outside the range of the UInt64 type. - // The Int64 value -18 is outside the range of the UInt64 type. - // Converted the Int64 value 0 to the UInt64 value 0. - // Converted the Int64 value 121 to the UInt64 value 121. - // Converted the Int64 value 340 to the UInt64 value 340. - // Converted the Int64 value 9223372036854775807 to a UInt64 value 9223372036854775807. - // - } + private static void ConvertInt64() + { + // + long[] numbers = { long.MinValue, -19432, -18, 0, 121, 340, long.MaxValue }; + ulong result; + foreach (long number in numbers) + { + try + { + result = Convert.ToUInt64(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the UInt64 type."); + } + } + // The example displays the following output: + // The Int64 value -9223372036854775808 is outside the range of the UInt64 type. + // The Int64 value -19432 is outside the range of the UInt64 type. + // The Int64 value -18 is outside the range of the UInt64 type. + // Converted the Int64 value 0 to the UInt64 value 0. + // Converted the Int64 value 121 to the UInt64 value 121. + // Converted the Int64 value 340 to the UInt64 value 340. + // Converted the Int64 value 9223372036854775807 to a UInt64 value 9223372036854775807. + // + } - private static void ConvertObject() - { - // - object[] values = { true, -12, 163, 935, 'x', new DateTime(2009, 5, 12), + private static void ConvertObject() + { + // + object[] values = { true, -12, 163, 935, 'x', new DateTime(2009, 5, 12), "104", "103.0", "-1", "1.00e2", "One", 1.00e2, 16.3e42}; - ulong result; + ulong result; - foreach (object value in values) - { - try { - result = Convert.ToUInt64(value); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - value.GetType().Name, value, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the UInt64 type.", - value.GetType().Name, value); - } - catch (FormatException) { - Console.WriteLine("The {0} value {1} is not in a recognizable format.", - value.GetType().Name, value); - } - catch (InvalidCastException) { - Console.WriteLine("No conversion to a UInt64 exists for the {0} value {1}.", - value.GetType().Name, value); - } - } - // The example displays the following output: - // Converted the Boolean value True to the UInt64 value 1. - // The Int32 value -12 is outside the range of the UInt64 type. - // Converted the Int32 value 163 to the UInt64 value 163. - // Converted the Int32 value 935 to the UInt64 value 935. - // Converted the Char value x to the UInt64 value 120. - // No conversion to a UInt64 exists for the DateTime value 5/12/2009 12:00:00 AM. - // Converted the String value 104 to the UInt64 value 104. - // The String value 103.0 is not in a recognizable format. - // The String value -1 is outside the range of the UInt64 type. - // The String value 1.00e2 is not in a recognizable format. - // The String value One is not in a recognizable format. - // Converted the Double value 100 to the UInt64 value 100. - // The Double value 1.63E+43 is outside the range of the UInt64 type. - // - } + foreach (object value in values) + { + try + { + result = Convert.ToUInt64(value); + Console.WriteLine($"Converted the {value.GetType().Name} value {value} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {value.GetType().Name} value {value} is outside the range of the UInt64 type."); + } + catch (FormatException) + { + Console.WriteLine($"The {value.GetType().Name} value {value} is not in a recognizable format."); + } + catch (InvalidCastException) + { + Console.WriteLine($"No conversion to a UInt64 exists for the {value.GetType().Name} value {value}."); + } + } + // The example displays the following output: + // Converted the Boolean value True to the UInt64 value 1. + // The Int32 value -12 is outside the range of the UInt64 type. + // Converted the Int32 value 163 to the UInt64 value 163. + // Converted the Int32 value 935 to the UInt64 value 935. + // Converted the Char value x to the UInt64 value 120. + // No conversion to a UInt64 exists for the DateTime value 5/12/2009 12:00:00 AM. + // Converted the String value 104 to the UInt64 value 104. + // The String value 103.0 is not in a recognizable format. + // The String value -1 is outside the range of the UInt64 type. + // The String value 1.00e2 is not in a recognizable format. + // The String value One is not in a recognizable format. + // Converted the Double value 100 to the UInt64 value 100. + // The Double value 1.63E+43 is outside the range of the UInt64 type. + // + } - private static void ConvertSByte() - { - // - sbyte[] numbers = { SByte.MinValue, -1, 0, 10, SByte.MaxValue }; - ulong result; + private static void ConvertSByte() + { + // + sbyte[] numbers = { sbyte.MinValue, -1, 0, 10, sbyte.MaxValue }; + ulong result; - foreach (sbyte number in numbers) - { - try { - result = Convert.ToUInt64(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the UInt64 type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // The SByte value -128 is outside the range of the UInt64 type. - // The SByte value -1 is outside the range of the UInt64 type. - // Converted the SByte value 0 to the UInt64 value 0. - // Converted the SByte value 10 to the UInt64 value 10. - // Converted the SByte value 127 to the UInt64 value 127. - // - } + foreach (sbyte number in numbers) + { + try + { + result = Convert.ToUInt64(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the UInt64 type."); + } + } + // The example displays the following output: + // The SByte value -128 is outside the range of the UInt64 type. + // The SByte value -1 is outside the range of the UInt64 type. + // Converted the SByte value 0 to the UInt64 value 0. + // Converted the SByte value 10 to the UInt64 value 10. + // Converted the SByte value 127 to the UInt64 value 127. + // + } - private static void ConvertSingle() - { - // - float[] values= { Single.MinValue, -1.38e10f, -1023.299f, -12.98f, - 0f, 9.113e-16f, 103.919f, 17834.191f, Single.MaxValue }; - ulong result; + private static void ConvertSingle() + { + // + float[] values = { float.MinValue, -1.38e10f, -1023.299f, -12.98f, + 0f, 9.113e-16f, 103.919f, 17834.191f, float.MaxValue }; + ulong result; - foreach (float value in values) - { - try { - result = Convert.ToUInt64(value); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - value.GetType().Name, value, result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the UInt64 type.", value); - } - } - // The example displays the following output: - // -3.402823E+38 is outside the range of the UInt64 type. - // -1.38E+10 is outside the range of the UInt64 type. - // -1023.299 is outside the range of the UInt64 type. - // -12.98 is outside the range of the UInt64 type. - // Converted the Single value 0 to the UInt64 value 0. - // Converted the Single value 9.113E-16 to the UInt64 value 0. - // Converted the Single value 103.919 to the UInt64 value 104. - // Converted the Single value 17834.19 to the UInt64 value 17834. - // 3.402823E+38 is outside the range of the UInt64 type. - // - } + foreach (float value in values) + { + try + { + result = Convert.ToUInt64(value); + Console.WriteLine($"Converted the {value.GetType().Name} value {value} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"{value} is outside the range of the UInt64 type."); + } + } + // The example displays the following output: + // -3.402823E+38 is outside the range of the UInt64 type. + // -1.38E+10 is outside the range of the UInt64 type. + // -1023.299 is outside the range of the UInt64 type. + // -12.98 is outside the range of the UInt64 type. + // Converted the Single value 0 to the UInt64 value 0. + // Converted the Single value 9.113E-16 to the UInt64 value 0. + // Converted the Single value 103.919 to the UInt64 value 104. + // Converted the Single value 17834.19 to the UInt64 value 17834. + // 3.402823E+38 is outside the range of the UInt64 type. + // + } - private static void ConvertString() - { - // - string[] values = { "One", "1.34e28", "-26.87", "-18", "-6.00", - " 0", "137", "1601.9", Int32.MaxValue.ToString() }; - ulong result; + private static void ConvertString() + { + // + string[] values = { "One", "1.34e28", "-26.87", "-18", "-6.00", + " 0", "137", "1601.9", int.MaxValue.ToString() }; + ulong result; - foreach (string value in values) - { - try { - result = Convert.ToUInt64(value); - Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", - value.GetType().Name, value, result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the UInt64 type.", value); - } - catch (FormatException) { - Console.WriteLine("The {0} value '{1}' is not in a recognizable format.", - value.GetType().Name, value); - } - } - // The example displays the following output: - // The String value 'One' is not in a recognizable format. - // The String value '1.34e28' is not in a recognizable format. - // The String value '-26.87' is not in a recognizable format. - // -18 is outside the range of the UInt64 type. - // The String value '-6.00' is not in a recognizable format. - // Converted the String value ' 0' to the UInt64 value 0. - // Converted the String value '137' to the UInt64 value 137. - // The String value '1601.9' is not in a recognizable format. - // Converted the String value '2147483647' to the UInt64 value 2147483647. - // - } + foreach (string value in values) + { + try + { + result = Convert.ToUInt64(value); + Console.WriteLine($"Converted the {value.GetType().Name} value '{value}' to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"{value} is outside the range of the UInt64 type."); + } + catch (FormatException) + { + Console.WriteLine($"The {value.GetType().Name} value '{value}' is not in a recognizable format."); + } + } + // The example displays the following output: + // The String value 'One' is not in a recognizable format. + // The String value '1.34e28' is not in a recognizable format. + // The String value '-26.87' is not in a recognizable format. + // -18 is outside the range of the UInt64 type. + // The String value '-6.00' is not in a recognizable format. + // Converted the String value ' 0' to the UInt64 value 0. + // Converted the String value '137' to the UInt64 value 137. + // The String value '1601.9' is not in a recognizable format. + // Converted the String value '2147483647' to the UInt64 value 2147483647. + // + } - private static void ConvertUInt16() - { - // - ushort[] numbers = { UInt16.MinValue, 121, 340, UInt16.MaxValue }; - ulong result; + private static void ConvertUInt16() + { + // + ushort[] numbers = { ushort.MinValue, 121, 340, ushort.MaxValue }; + ulong result; - foreach (ushort number in numbers) - { - try { - result = Convert.ToUInt64(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the UInt64 type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // Converted the UInt16 value 0 to the UInt64 value 0. - // Converted the UInt16 value 121 to the UInt64 value 121. - // Converted the UInt16 value 340 to the UInt64 value 340. - // Converted the UInt16 value 65535 to the UInt64 value 65535. - // - } + foreach (ushort number in numbers) + { + try + { + result = Convert.ToUInt64(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the UInt64 type."); + } + } + // The example displays the following output: + // Converted the UInt16 value 0 to the UInt64 value 0. + // Converted the UInt16 value 121 to the UInt64 value 121. + // Converted the UInt16 value 340 to the UInt64 value 340. + // Converted the UInt16 value 65535 to the UInt64 value 65535. + // + } - private static void ConvertUInt32() - { - // - uint[] numbers = { UInt32.MinValue, 121, 340, UInt32.MaxValue }; - ulong result; + private static void ConvertUInt32() + { + // + uint[] numbers = { uint.MinValue, 121, 340, uint.MaxValue }; + ulong result; - foreach (uint number in numbers) - { - try { - result = Convert.ToUInt64(number); - Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", - number.GetType().Name, number, - result.GetType().Name, result); - } - catch (OverflowException) { - Console.WriteLine("The {0} value {1} is outside the range of the UInt64 type.", - number.GetType().Name, number); - } - } - // The example displays the following output: - // Converted the UInt32 value 0 to the UInt64 value 0. - // Converted the UInt32 value 121 to the UInt64 value 121. - // Converted the UInt32 value 340 to the UInt64 value 340. - // Converted the UInt32 value 4294967295 to the UInt64 value 4294967295. - // - } + foreach (uint number in numbers) + { + try + { + result = Convert.ToUInt64(number); + Console.WriteLine($"Converted the {number.GetType().Name} value {number} to the {result.GetType().Name} value {result}."); + } + catch (OverflowException) + { + Console.WriteLine($"The {number.GetType().Name} value {number} is outside the range of the UInt64 type."); + } + } + // The example displays the following output: + // Converted the UInt32 value 0 to the UInt64 value 0. + // Converted the UInt32 value 121 to the UInt64 value 121. + // Converted the UInt32 value 340 to the UInt64 value 340. + // Converted the UInt32 value 4294967295 to the UInt64 value 4294967295. + // + } } diff --git a/snippets/csharp/System/Convert/ToUInt64/touint64_2.cs b/snippets/csharp/System/Convert/ToUInt64/touint64_2.cs index a802d9b4773..d51c294dc77 100644 --- a/snippets/csharp/System/Convert/ToUInt64/touint64_2.cs +++ b/snippets/csharp/System/Convert/ToUInt64/touint64_2.cs @@ -4,33 +4,38 @@ public class Example { - public static void Main() - { - // Create a NumberFormatInfo object and set several properties. - NumberFormatInfo provider = new NumberFormatInfo(); - provider.PositiveSign = "pos "; - provider.NegativeSign = "neg "; + public static void Main() + { + // Create a NumberFormatInfo object and set several properties. + NumberFormatInfo provider = new() + { + PositiveSign = "pos ", + NegativeSign = "neg " + }; - // Define an array of numeric strings. - string[] values = { "123456789012", "+123456789012", + // Define an array of numeric strings. + string[] values = { "123456789012", "+123456789012", "pos 123456789012", "123456789012.", "123,456,789,012", "18446744073709551615", "18446744073709551616", "neg 1", "-1" }; - // Convert the strings using the format provider. - foreach (string value in values) - { - Console.Write("{0,-20} --> ", value); - try { - Console.WriteLine("{0,20}", Convert.ToUInt64(value, provider)); - } - catch (FormatException) { - Console.WriteLine("{0,20}", "Invalid Format"); - } - catch (OverflowException) { - Console.WriteLine("{0,20}", "Numeric Overflow"); - } - } - } + // Convert the strings using the format provider. + foreach (string value in values) + { + Console.Write($"{value,-20} --> "); + try + { + Console.WriteLine($"{Convert.ToUInt64(value, provider),20}"); + } + catch (FormatException) + { + Console.WriteLine($"{"Invalid Format",20}"); + } + catch (OverflowException) + { + Console.WriteLine($"{"Numeric Overflow",20}"); + } + } + } } // The example displays the following output: // 123456789012 --> 123456789012 diff --git a/snippets/csharp/System/Convert/ToUInt64/touint64_3.cs b/snippets/csharp/System/Convert/ToUInt64/touint64_3.cs index 81b954287ee..009f4f75d87 100644 --- a/snippets/csharp/System/Convert/ToUInt64/touint64_3.cs +++ b/snippets/csharp/System/Convert/ToUInt64/touint64_3.cs @@ -3,31 +3,35 @@ public class Example { - public static void Main() - { - string[] hexStrings = { "8000000000000000", "0FFFFFFFFFFFFFFF", + public static void Main() + { + string[] hexStrings = { "8000000000000000", "0FFFFFFFFFFFFFFF", "F000000000000000", "00A3000000000000", "D", "-13", "9AC61", "GAD", "FFFFFFFFFFFFFFFFF" }; - foreach (string hexString in hexStrings) - { - Console.Write("{0,-18} --> ", hexString); - try { - ulong number = Convert.ToUInt64(hexString, 16); - Console.WriteLine("{0,26:N0}", number); - } - catch (FormatException) { - Console.WriteLine("{0,26}", "Bad Format"); - } - catch (OverflowException) { - Console.WriteLine("{0,26}", "Numeric Overflow"); - } - catch (ArgumentException) { - Console.WriteLine("{0,26}", "Invalid in Base 16"); - } - } - } + foreach (string hexString in hexStrings) + { + Console.Write($"{hexString,-18} --> "); + try + { + ulong number = Convert.ToUInt64(hexString, 16); + Console.WriteLine($"{number,26:N0}"); + } + catch (FormatException) + { + Console.WriteLine($"{"Bad Format",26}"); + } + catch (OverflowException) + { + Console.WriteLine($"{"Numeric Overflow",26}"); + } + catch (ArgumentException) + { + Console.WriteLine($"{"Invalid in Base 16",26}"); + } + } + } } // The example displays the following output: // 8000000000000000 --> 9,223,372,036,854,775,808 diff --git a/snippets/csharp/System/Convert/ToUInt64/touint64_4.cs b/snippets/csharp/System/Convert/ToUInt64/touint64_4.cs index 95699b5cdd6..2150fe59326 100644 --- a/snippets/csharp/System/Convert/ToUInt64/touint64_4.cs +++ b/snippets/csharp/System/Convert/ToUInt64/touint64_4.cs @@ -3,296 +3,307 @@ using System.Globalization; using System.Text.RegularExpressions; -public enum SignBit { Negative=-1, Zero=0, Positive=1 }; +public enum SignBit { Negative = -1, Zero = 0, Positive = 1 }; public struct HexString : IConvertible { - private SignBit signBit; - private string hexString; + private SignBit signBit; + private string hexString; - public SignBit Sign + public SignBit Sign { - set { signBit = value; } - get { return signBit; } + set => signBit = value; + get => signBit; } - public string Value - { - set - { - if (value.Trim().Length > 16) - throw new ArgumentException("The hexadecimal representation of a 64-bit integer cannot have more than 16 characters."); - else if (!Regex.IsMatch(value, "([0-9,A-F]){1,8}", RegexOptions.IgnoreCase)) - throw new ArgumentException("The hexadecimal representation of a 64-bit integer contains invalid characters."); - else - hexString = value; - } - get { return hexString; } - } + public string Value + { + set + { + if (value.Trim().Length > 16) + throw new ArgumentException("The hexadecimal representation of a 64-bit integer cannot have more than 16 characters."); + else if (!Regex.IsMatch(value, "([0-9,A-F]){1,8}", RegexOptions.IgnoreCase)) + throw new ArgumentException("The hexadecimal representation of a 64-bit integer contains invalid characters."); + else + hexString = value; + } + get => hexString; + } - // IConvertible implementations. - public TypeCode GetTypeCode() - { - return TypeCode.Object; - } + // IConvertible implementations. + public TypeCode GetTypeCode() => TypeCode.Object; - public bool ToBoolean(IFormatProvider provider) - { - return signBit != SignBit.Zero; - } + public bool ToBoolean(IFormatProvider provider) => signBit != SignBit.Zero; - public byte ToByte(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - throw new OverflowException(String.Format("{0} is out of range of the Byte type.", Convert.ToInt64(hexString, 16))); - else - try { - return Byte.Parse(hexString, NumberStyles.HexNumber); - } - catch (OverflowException e) { - throw new OverflowException(String.Format("{0} is out of range of the Byte type.", Convert.ToUInt64(hexString, 16)), e); - } - } + public byte ToByte(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + throw new OverflowException($"{Convert.ToInt64(hexString, 16)} is out of range of the Byte type."); + else + try + { + return byte.Parse(hexString, NumberStyles.HexNumber); + } + catch (OverflowException e) + { + throw new OverflowException($"{Convert.ToUInt64(hexString, 16)} is out of range of the Byte type.", e); + } + } - public char ToChar(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - throw new OverflowException(String.Format("{0} is out of range of the Char type.", Convert.ToInt64(hexString, 16))); + public char ToChar(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + throw new OverflowException($"{Convert.ToInt64(hexString, 16)} is out of range of the Char type."); - try { - ushort codePoint = UInt16.Parse(this.hexString, NumberStyles.HexNumber); - return Convert.ToChar(codePoint); - } - catch (OverflowException) { - throw new OverflowException(String.Format("{0} is out of range of the Char type.", Convert.ToUInt64(hexString, 16))); - } - } + try + { + ushort codePoint = ushort.Parse(this.hexString, NumberStyles.HexNumber); + return Convert.ToChar(codePoint); + } + catch (OverflowException) + { + throw new OverflowException($"{Convert.ToUInt64(hexString, 16)} is out of range of the Char type."); + } + } - public DateTime ToDateTime(IFormatProvider provider) - { - throw new InvalidCastException("Hexadecimal to DateTime conversion is not supported."); - } + public DateTime ToDateTime(IFormatProvider provider) => throw new InvalidCastException("Hexadecimal to DateTime conversion is not supported."); - public decimal ToDecimal(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - { - long hexValue = Int64.Parse(hexString, NumberStyles.HexNumber); - return Convert.ToDecimal(hexValue); - } - else - { - ulong hexValue = UInt64.Parse(hexString, NumberStyles.HexNumber); - return Convert.ToDecimal(hexValue); - } - } + public decimal ToDecimal(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + { + long hexValue = long.Parse(hexString, NumberStyles.HexNumber); + return Convert.ToDecimal(hexValue); + } + else + { + ulong hexValue = ulong.Parse(hexString, NumberStyles.HexNumber); + return Convert.ToDecimal(hexValue); + } + } - public double ToDouble(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - return Convert.ToDouble(Int64.Parse(hexString, NumberStyles.HexNumber)); - else - return Convert.ToDouble(UInt64.Parse(hexString, NumberStyles.HexNumber)); - } + public double ToDouble(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + return Convert.ToDouble(long.Parse(hexString, NumberStyles.HexNumber)); + else + return Convert.ToDouble(ulong.Parse(hexString, NumberStyles.HexNumber)); + } - public short ToInt16(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - try { - return Convert.ToInt16(Int64.Parse(hexString, NumberStyles.HexNumber)); - } - catch (OverflowException e) { - throw new OverflowException(String.Format("{0} is out of range of the Int16 type.", Convert.ToInt64(hexString, 16)), e); - } - else - try { - return Convert.ToInt16(UInt64.Parse(hexString, NumberStyles.HexNumber)); - } - catch (OverflowException e) { - throw new OverflowException(String.Format("{0} is out of range of the Int16 type.", Convert.ToUInt64(hexString, 16)), e); - } - } + public short ToInt16(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + try + { + return Convert.ToInt16(long.Parse(hexString, NumberStyles.HexNumber)); + } + catch (OverflowException e) + { + throw new OverflowException($"{Convert.ToInt64(hexString, 16)} is out of range of the Int16 type.", e); + } + else + try + { + return Convert.ToInt16(ulong.Parse(hexString, NumberStyles.HexNumber)); + } + catch (OverflowException e) + { + throw new OverflowException($"{Convert.ToUInt64(hexString, 16)} is out of range of the Int16 type.", e); + } + } - public int ToInt32(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - try { - return Convert.ToInt32(Int64.Parse(hexString, NumberStyles.HexNumber)); - } - catch (OverflowException e) { - throw new OverflowException(String.Format("{0} is out of range of the Int32 type.", Convert.ToUInt64(hexString, 16)), e); - } - else - try { - return Convert.ToInt32(UInt64.Parse(hexString, NumberStyles.HexNumber)); - } - catch (OverflowException e) { - throw new OverflowException(String.Format("{0} is out of range of the Int32 type.", Convert.ToUInt64(hexString, 16)), e); - } - } + public int ToInt32(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + try + { + return Convert.ToInt32(long.Parse(hexString, NumberStyles.HexNumber)); + } + catch (OverflowException e) + { + throw new OverflowException($"{Convert.ToUInt64(hexString, 16)} is out of range of the Int32 type.", e); + } + else + try + { + return Convert.ToInt32(ulong.Parse(hexString, NumberStyles.HexNumber)); + } + catch (OverflowException e) + { + throw new OverflowException($"{Convert.ToUInt64(hexString, 16)} is out of range of the Int32 type.", e); + } + } - public long ToInt64(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - return Int64.Parse(hexString, NumberStyles.HexNumber); - else - try { - return Convert.ToInt64(UInt64.Parse(hexString, NumberStyles.HexNumber)); - } - catch (OverflowException e) { - throw new OverflowException(String.Format("{0} is out of range of the Int64 type.", Convert.ToUInt64(hexString, 16)), e); - } - } + public long ToInt64(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + return long.Parse(hexString, NumberStyles.HexNumber); + else + try + { + return Convert.ToInt64(ulong.Parse(hexString, NumberStyles.HexNumber)); + } + catch (OverflowException e) + { + throw new OverflowException($"{Convert.ToUInt64(hexString, 16)} is out of range of the Int64 type.", e); + } + } - public sbyte ToSByte(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - try { - return Convert.ToSByte(Int64.Parse(hexString, NumberStyles.HexNumber)); - } - catch (OverflowException e) { - throw new OverflowException(String.Format("{0} is outside the range of the SByte type.", - Int64.Parse(hexString, NumberStyles.HexNumber), e)); - } - else - try { - return Convert.ToSByte(UInt64.Parse(hexString, NumberStyles.HexNumber)); - } - catch (OverflowException e) { - throw new OverflowException(String.Format("{0} is outside the range of the SByte type.", - UInt64.Parse(hexString, NumberStyles.HexNumber)), e); - } - } + public sbyte ToSByte(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + try + { + return Convert.ToSByte(long.Parse(hexString, NumberStyles.HexNumber)); + } + catch (OverflowException e) + { + throw new OverflowException(string.Format("{0} is outside the range of the SByte type.", + long.Parse(hexString, NumberStyles.HexNumber), e)); + } + else + try + { + return Convert.ToSByte(ulong.Parse(hexString, NumberStyles.HexNumber)); + } + catch (OverflowException e) + { + throw new OverflowException($"{ulong.Parse(hexString, NumberStyles.HexNumber)} is outside the range of the SByte type.", e); + } + } - public float ToSingle(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - return Convert.ToSingle(Int64.Parse(hexString, NumberStyles.HexNumber)); - else - return Convert.ToSingle(UInt64.Parse(hexString, NumberStyles.HexNumber)); - } + public float ToSingle(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + return Convert.ToSingle(long.Parse(hexString, NumberStyles.HexNumber)); + else + return Convert.ToSingle(ulong.Parse(hexString, NumberStyles.HexNumber)); + } - public string ToString(IFormatProvider provider) - { - return "0x" + this.hexString; - } + public string ToString(IFormatProvider provider) => "0x" + this.hexString; - public object ToType(Type conversionType, IFormatProvider provider) - { - switch (Type.GetTypeCode(conversionType)) - { - case TypeCode.Boolean: - return this.ToBoolean(null); - case TypeCode.Byte: - return this.ToByte(null); - case TypeCode.Char: - return this.ToChar(null); - case TypeCode.DateTime: - return this.ToDateTime(null); - case TypeCode.Decimal: - return this.ToDecimal(null); - case TypeCode.Double: - return this.ToDouble(null); - case TypeCode.Int16: - return this.ToInt16(null); - case TypeCode.Int32: - return this.ToInt32(null); - case TypeCode.Int64: - return this.ToInt64(null); - case TypeCode.Object: - if (typeof(HexString).Equals(conversionType)) - return this; - else - throw new InvalidCastException(String.Format("Conversion to a {0} is not supported.", conversionType.Name)); - case TypeCode.SByte: - return this.ToSByte(null); - case TypeCode.Single: - return this.ToSingle(null); - case TypeCode.String: - return this.ToString(null); - case TypeCode.UInt16: - return this.ToUInt16(null); - case TypeCode.UInt32: - return this.ToUInt32(null); - case TypeCode.UInt64: - return this.ToUInt64(null); - default: - throw new InvalidCastException(String.Format("Conversion to {0} is not supported.", conversionType.Name)); - } - } + public object ToType(Type conversionType, IFormatProvider provider) + { + switch (Type.GetTypeCode(conversionType)) + { + case TypeCode.Boolean: + return this.ToBoolean(null); + case TypeCode.Byte: + return this.ToByte(null); + case TypeCode.Char: + return this.ToChar(null); + case TypeCode.DateTime: + return this.ToDateTime(null); + case TypeCode.Decimal: + return this.ToDecimal(null); + case TypeCode.Double: + return this.ToDouble(null); + case TypeCode.Int16: + return this.ToInt16(null); + case TypeCode.Int32: + return this.ToInt32(null); + case TypeCode.Int64: + return this.ToInt64(null); + case TypeCode.Object: + if (typeof(HexString).Equals(conversionType)) + return this; + else + throw new InvalidCastException($"Conversion to a {conversionType.Name} is not supported."); + case TypeCode.SByte: + return this.ToSByte(null); + case TypeCode.Single: + return this.ToSingle(null); + case TypeCode.String: + return this.ToString(null); + case TypeCode.UInt16: + return this.ToUInt16(null); + case TypeCode.UInt32: + return this.ToUInt32(null); + case TypeCode.UInt64: + return this.ToUInt64(null); + default: + throw new InvalidCastException($"Conversion to {conversionType.Name} is not supported."); + } + } - public ushort ToUInt16(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - throw new OverflowException(String.Format("{0} is outside the range of the UInt16 type.", - Int64.Parse(hexString, NumberStyles.HexNumber))); - else - try { - return Convert.ToUInt16(UInt64.Parse(hexString, NumberStyles.HexNumber)); - } - catch (OverflowException e) { - throw new OverflowException(String.Format("{0} is out of range of the UInt16 type.", Convert.ToUInt64(hexString, 16)), e); - } - } + public ushort ToUInt16(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + throw new OverflowException($"{long.Parse(hexString, NumberStyles.HexNumber)} is outside the range of the UInt16 type."); + else + try + { + return Convert.ToUInt16(ulong.Parse(hexString, NumberStyles.HexNumber)); + } + catch (OverflowException e) + { + throw new OverflowException($"{Convert.ToUInt64(hexString, 16)} is out of range of the UInt16 type.", e); + } + } - public uint ToUInt32(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - throw new OverflowException(String.Format("{0} is outside the range of the UInt32 type.", - Int64.Parse(hexString, NumberStyles.HexNumber))); - else - try { - return Convert.ToUInt32(UInt64.Parse(hexString, NumberStyles.HexNumber)); - } - catch (OverflowException) { - throw new OverflowException(String.Format("{0} is outside the range of the UInt32 type.", - UInt64.Parse(hexString, NumberStyles.HexNumber))); - } - } + public uint ToUInt32(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + throw new OverflowException($"{long.Parse(hexString, NumberStyles.HexNumber)} is outside the range of the UInt32 type."); + else + try + { + return Convert.ToUInt32(ulong.Parse(hexString, NumberStyles.HexNumber)); + } + catch (OverflowException) + { + throw new OverflowException($"{ulong.Parse(hexString, NumberStyles.HexNumber)} is outside the range of the UInt32 type."); + } + } - public ulong ToUInt64(IFormatProvider provider) - { - if (signBit == SignBit.Negative) - throw new OverflowException(String.Format("{0} is outside the range of the UInt64 type.", - Int64.Parse(hexString, NumberStyles.HexNumber))); - else - return Convert.ToUInt64(hexString, 16); - } + public ulong ToUInt64(IFormatProvider provider) + { + if (signBit == SignBit.Negative) + throw new OverflowException($"{long.Parse(hexString, NumberStyles.HexNumber)} is outside the range of the UInt64 type."); + else + return Convert.ToUInt64(hexString, 16); + } } // // public class Example { - public static void Main() - { - ulong positiveValue = UInt64.MaxValue - 100000; - long negativeValue = -1; + public static void Main() + { + ulong positiveValue = ulong.MaxValue - 100000; + long negativeValue = -1; - HexString positiveString = new HexString(); - positiveString.Sign = (SignBit) Math.Sign((decimal)positiveValue); - positiveString.Value = positiveValue.ToString("X"); + HexString positiveString = new() + { + Sign = (SignBit)Math.Sign((decimal)positiveValue), + Value = positiveValue.ToString("X") + }; - HexString negativeString = new HexString(); - negativeString.Sign = (SignBit) Math.Sign(negativeValue); - negativeString.Value = negativeValue.ToString("X"); + HexString negativeString = new() + { + Sign = (SignBit)Math.Sign(negativeValue), + Value = negativeValue.ToString("X") + }; - try { - Console.WriteLine("0x{0} converts to {1}.", positiveString.Value, Convert.ToUInt64(positiveString)); - } - catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the UInt64 type.", - Int64.Parse(positiveString.Value, NumberStyles.HexNumber)); - } + try + { + Console.WriteLine($"0x{positiveString.Value} converts to {Convert.ToUInt64(positiveString)}."); + } + catch (OverflowException) + { + Console.WriteLine($"{long.Parse(positiveString.Value, NumberStyles.HexNumber)} is outside the range of the UInt64 type."); + } - try { - Console.WriteLine("0x{0} converts to {1}.", negativeString.Value, Convert.ToUInt64(negativeString)); - } - catch (OverflowException) { - Console.WriteLine("{0} is outside the range of the UInt64 type.", - Int64.Parse(negativeString.Value, NumberStyles.HexNumber)); - } - } + try + { + Console.WriteLine($"0x{negativeString.Value} converts to {Convert.ToUInt64(negativeString)}."); + } + catch (OverflowException) + { + Console.WriteLine($"{long.Parse(negativeString.Value, NumberStyles.HexNumber)} is outside the range of the UInt64 type."); + } + } } // The example displays the following output: // 0xFFFFFFFFFFFE795F converts to 18446744073709451615. diff --git a/snippets/csharp/System/ConverterTInput,TOutput/Overview/source.cs b/snippets/csharp/System/ConverterTInput,TOutput/Overview/source.cs index c35e6bc964e..d003bce5b42 100644 --- a/snippets/csharp/System/ConverterTInput,TOutput/Overview/source.cs +++ b/snippets/csharp/System/ConverterTInput,TOutput/Overview/source.cs @@ -7,14 +7,15 @@ public class Example { public static void Main() { - List lpf = new List(); - - lpf.Add(new PointF(27.8F, 32.62F)); - lpf.Add(new PointF(99.3F, 147.273F)); - lpf.Add(new PointF(7.5F, 1412.2F)); + List lpf = new() + { + new PointF(27.8F, 32.62F), + new PointF(99.3F, 147.273F), + new PointF(7.5F, 1412.2F) + }; Console.WriteLine(); - foreach( PointF p in lpf ) + foreach (PointF p in lpf) { Console.WriteLine(p); } @@ -23,16 +24,13 @@ public static void Main() new Converter(PointFToPoint)); Console.WriteLine(); - foreach( Point p in lp ) + foreach (Point p in lp) { Console.WriteLine(p); } } - public static Point PointFToPoint(PointF pf) - { - return new Point(((int) pf.X), ((int) pf.Y)); - } + public static Point PointFToPoint(PointF pf) => new Point(((int)pf.X), ((int)pf.Y)); } /* This code example produces the following output: From 5592cb22c247abd4a47d5b4dd4f0462cbaa576ea Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Thu, 6 Aug 2026 17:38:22 -0700 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- snippets/csharp/System/Convert/ToInt64/toint64_3.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/csharp/System/Convert/ToInt64/toint64_3.cs b/snippets/csharp/System/Convert/ToInt64/toint64_3.cs index 6ab723f32ef..bf69aaa4ec3 100644 --- a/snippets/csharp/System/Convert/ToInt64/toint64_3.cs +++ b/snippets/csharp/System/Convert/ToInt64/toint64_3.cs @@ -34,7 +34,7 @@ public static void Main() Console.Write($" {numericString,-22} --> "); try { - Console.WriteLine($"{Convert.ToInt32(numericString, provider),22}"); + Console.WriteLine($"{Convert.ToInt64(numericString, provider),22}"); } catch (FormatException) {