Decimal.ToSByte() Method in C#In this article, you will learn about the Decimal.ToSByte() method in C# with its syntax, parameters, and examples. What is the Decimal.ToSByte()?The System namespace in C# has the "Decimal.ToSByte()" method, which is used to convert a decimal number to a signed byte (sbyte). Syntax:It has the following syntax: Parametersa. Access Modifier public: The visibility of the method is specified by this access modifier. In this particular case, any other class or assembly can access it because the method is specified as public. b. Static Modifier static: The static keyword indicates the method as being part of the class and not an instance of the class. Calling it directly in the class without creating an instance is possible. c. Return Type sbyte: It is the return type of the method. It indicates that the method will return a type sbyte value, a signed 8-bit integer. d. Method Name ToSByte: It is the name of the method. It is the identifier used to call the method. In this case, the method name indicates that a signed byte (sbyte) is being converted from a decimal value. e. Parameter decimal value: The parameters that the method requires are declared here. In this particular case, it has a single decimal-type parameter called value. This parameter represents the decimal number the method will convert to a signed byte. Range LimitationsThe method can handle values between -128 and 127, the range of a signed byte. The method raises an OverflowException if the decimal value exceeds this range. Example 1:Let us take an example to illustrate the use of Decimal.ToSByte() method in C#. Output: Decimal value 63.8 and converted to SByte: 63 Explanation: 1. Namespace Import using System;: The System namespace, a standard C# namespace that provides base classes and fundamental types, is imported with this line. 2. Declaration of Class class Demo: Here, a class called Demo is declared. 3. Main Method static void Main(): It is the program's entry point. When the program executes, the Main method is automatically called. 4. Variable Declaration decimal valid_Decimal = 63.8m;: It declares and initializes the valid_Decimal decimal variable. The literal is a decimal, indicated by the 'm' suffix. 5. try-Catch Block try { ... } catch (OverflowException) { ... }: It is a try-catch block used for exception handling. The code inside the try block uses "Decimal.ToSByte()" to convert the valid_Decimal variable to a sbyte. The conversion is expected to be successful because the number 63.8 is inside the valid range for a sbyte (-128 to 127). If there is no overflow, the program prints the conversion's result using "Console.WriteLine()". 6. Console Output If the conversion is successful, a message with the original decimal value and the converted value is printed to the Console by the program. If an overflow occurs, the catch block catches an OverflowException and prints a message indicating that the conversion resulted in an overflow. Example 2:Let us take another example to illustrate the use of Decimal.ToSByte() method in C#. Output: Conversion resulted in an overflow due to out of SByte range. Explanation: 1. Namespace Import using System;: The System namespace, a standard C# namespace that provides base classes and fundamental types, is imported with this line. 2. Declaration of Class class Demo: Here, a class called Demo is declared. 3. Main Method static void Main(): It is the program's entry point. When the program executes, the Main method is automatically called. 4. Declaration of Variables decimal overflow_Decimal = 128.5m;: It declares and initializes the overflow_Decimal decimal variable. The literal is a decimal, indicated by the 'm' suffix. 5. Try-Catch Block try { ... } catch (OverflowException) { ... }: This try-catch block is used in the handling of exceptions. The code inside the try block uses "Decimal.ToSByte()" to convert the overflow_Decimal variable to a sbyte. The catch block is called if the conversion causes an overflow (because the value is outside of sbyte's valid range). The catch block catches a message indicating to the reader that the conversion caused an overflow and catches an OverflowException. 6. Console Output The result of the conversion or the overflow message is printed to the Console using Console.WriteLine(). Next TopicDefault Interface Methods in C# |