Mega Code Archive

 
Categories / C# Tutorial / Data Type
 

Converts Decimal numbers to SByte values using the explicit Decimal to SByte conversion

using System; class MainClass {     public static string GetExceptionType(Exception ex)     {         string exceptionType = ex.GetType().ToString();         return exceptionType.Substring(exceptionType.LastIndexOf('.') + 1);     }     public static void DecimalToS_Byte(decimal argument)     {         object SByteValue;         object ByteValue;         try         {             SByteValue = (sbyte)argument;         }         catch (Exception ex)         {             SByteValue = GetExceptionType(ex);         }         try         {             ByteValue = (byte)argument;         }         catch (Exception ex)         {             ByteValue = GetExceptionType(ex);         }         Console.WriteLine(argument);         Console.WriteLine(SByteValue);         Console.WriteLine(ByteValue);     }     public static void Main(String[] a)     {         DecimalToS_Byte(7M);         DecimalToS_Byte(new decimal(7, 0, 0, false, 3));     } }