Exemple #1
0
        public void TestUInt16ToSByteNullable()
        {
            // Test conversion of source type minimum value
            UInt16 source = UInt16.MinValue;

            Assert.IsInstanceOfType(source, typeof(UInt16));
            SByte?result = source.ToSByteNullable();

            Assert.AreEqual((sbyte)0, result);
            Assert.IsInstanceOfType(result, typeof(SByte));

            // Test conversion of source type value 42 to target type
            source = (ushort)42;
            Assert.IsInstanceOfType(source, typeof(UInt16));
            result = source.ToSByteNullable();
            Assert.AreEqual((sbyte)42, result);
            Assert.IsInstanceOfType(result, typeof(SByte));

            // Test conversion of source type maximum value
            source = UInt16.MaxValue;
            Assert.IsInstanceOfType(source, typeof(UInt16));
            result = source.ToSByteNullable();
            // Here we would expect this conversion to fail (and return null),
            // since the source type's maximum value (65535) is greater than the target type's maximum value (127).
            Assert.IsNull(result);
        }
Exemple #2
0
 /// <summary>
 /// Converts this UInt16 to SByte or returns the specified default value
 /// </summary>
 /// <returns>This UInt16 converted to SByte</returns>
 /// <remarks>
 /// Source type: UInt16
 /// Min value: 0
 /// Max value: 65535
 ///
 /// Target type: SByte
 /// Min value: -128
 /// Max value: 127
 /// </remarks>
 public static SByte ToSByteOrDefault(this UInt16 thisUInt16, SByte defaultValue = default(SByte))
 {
     return(thisUInt16.ToSByteNullable().GetValueOrDefault(defaultValue));
 }